-
Notifications
You must be signed in to change notification settings - Fork 1
Move RootPicker to filepicker module #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
99792cd
48caf42
15b624d
beda289
c17719b
5f98d2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package dev.arkbuilders.components.filepicker | ||
|
|
||
| import androidx.core.os.bundleOf | ||
| import androidx.fragment.app.setFragmentResult | ||
| import androidx.lifecycle.lifecycleScope | ||
| import dev.arkbuilders.arklib.data.folders.FoldersRepo | ||
| import dev.arkbuilders.arklib.utils.INTERNAL_STORAGE | ||
| import dev.arkbuilders.components.utils.hasNestedRoot | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.launch | ||
| import java.nio.file.Path | ||
|
|
||
| class ArkRootPickerFragment : ArkFilePickerFragment() { | ||
| private var rootNotFavorite = false | ||
|
|
||
| override fun onFolderChanged(currentFolder: Path) { | ||
| lifecycleScope.launch { | ||
| val folders = FoldersRepo.instance.provideFolders() | ||
| val roots = folders.keys | ||
|
|
||
| if (currentFolder == INTERNAL_STORAGE || currentFolder.hasNestedRoot(roots)) { | ||
| rootNotFavorite = true | ||
| binding.btnPick.text = getString(R.string.ark_file_picker_root) | ||
| binding.btnPick.isEnabled = false | ||
| return@launch | ||
| } | ||
|
|
||
| val root = roots.find { root -> currentFolder.startsWith(root) } | ||
| val favorites = folders[root]?.flatten() | ||
| root?.let { | ||
| if (root == currentFolder) { | ||
| rootNotFavorite = true | ||
| binding.btnPick.text = getString(R.string.ark_file_picker_root) | ||
| binding.btnPick.isEnabled = false | ||
| } else { | ||
| val foundAsFavorite = favorites?.any { currentFolder.endsWith(it) } ?: false | ||
| rootNotFavorite = false | ||
| binding.btnPick.text = getString(R.string.ark_file_picker_favorite) | ||
| binding.btnPick.isEnabled = !foundAsFavorite | ||
| } | ||
| } ?: let { | ||
| rootNotFavorite = true | ||
| binding.btnPick.text = getString(R.string.ark_file_picker_root) | ||
| binding.btnPick.isEnabled = true | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun onPick(pickedPath: Path) { | ||
| lifecycleScope.launch(Dispatchers.IO) { | ||
| addRootOrFavorite(pickedPath) | ||
| setFragmentResult( | ||
| ROOT_PICKED_REQUEST_KEY, | ||
| bundleOf().apply { | ||
| putString(PICKED_PATH_BUNDLE_KEY, pickedPath.toString()) | ||
| putBoolean(ROOT_NOT_FAV_BUNDLE_KEY, rootNotFavorite) | ||
| } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private suspend fun addRootOrFavorite(pickedPath: Path) { | ||
| val folders = FoldersRepo.instance.provideFolders() | ||
| if (rootNotFavorite) { | ||
| FoldersRepo.instance.addRoot(pickedPath) | ||
| } else { | ||
| val root = folders.keys.find { pickedPath.startsWith(it) } | ||
| ?: throw IllegalStateException( | ||
| "Can't add favorite if it's root is not added" | ||
| ) | ||
| val favoriteRelativePath = root.relativize(pickedPath) | ||
| FoldersRepo.instance.addFavorite(root, favoriteRelativePath) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| const val ROOT_PICKED_REQUEST_KEY = "rootPicked" | ||
| const val PICKED_PATH_BUNDLE_KEY = "pickedPath" | ||
| const val ROOT_NOT_FAV_BUNDLE_KEY = "rootNotFav" | ||
|
|
||
| fun newInstance( | ||
| config: ArkFilePickerConfig = ArkFilePickerConfig() | ||
| ) = ArkRootPickerFragment().apply { | ||
| setConfig( | ||
| config.copy( | ||
| showRoots = false, | ||
| mode = ArkFilePickerMode.FOLDER, | ||
| pathPickedRequestKey = "notUsed" | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,4 +20,21 @@ fun FragmentManager.onArkPathPicked( | |||||
| ) | ||||||
| ) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fun FragmentManager.onArkRootOrFavPicked( | ||||||
| lifecycleOwner: LifecycleOwner, | ||||||
| listener: (path: Path, rootNotFavorite: Boolean) -> Unit | ||||||
| ) { | ||||||
| setFragmentResultListener( | ||||||
| ArkRootPickerFragment.ROOT_PICKED_REQUEST_KEY, | ||||||
| lifecycleOwner | ||||||
| ) { _, bundle -> | ||||||
| listener( | ||||||
| Path( | ||||||
| bundle.getString(ArkRootPickerFragment.PICKED_PATH_BUNDLE_KEY)!! | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of asserting the returned string value, what about giving it a non-null fallback value?
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is key for selected path from dialog. If we return fragment result bundle without selected path, then something goes terribly wrong. We want to crash in this case. |
||||||
| ), | ||||||
| bundle.getBoolean(ArkRootPickerFragment.ROOT_NOT_FAV_BUNDLE_KEY) | ||||||
| ) | ||||||
| } | ||||||
| } | ||||||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mdrlzy By checking if a folder
hasNestedOrParentalRoot, we are prohibiting pinning current folder ifor
By changing from
hasNestedOrParentalRoottohasNestedRoot, we're ignoring every root folder that mightcontaincurrently folder, and still allow pining the current folder.Is that our expectation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If folder has parental root, we should still be able to pin it as favorite. Now this is not possible, check the main branch yourself.
Sorry, my bad, I forgot about it when I was reviewing your PR