Skip to content

Commit

Permalink
Prohibit creating nested roots: #68
Browse files Browse the repository at this point in the history
  • Loading branch information
tuancoltech committed Mar 18, 2024
1 parent c425a64 commit 492b448
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 2 deletions.
3 changes: 2 additions & 1 deletion filepicker/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ dependencies {
implementation(libraries.viewbinding.property.delegate)
}

val libVersion: String = scmVersion.version
val libVersion: String = /*scmVersion.version*/"0.1.1-SNAPSHOT-04"

publishing {
publications {
Expand All @@ -95,5 +95,6 @@ publishing {
password = System.getenv("GITHUB_TOKEN")
}
}
mavenLocal()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ open class ArkFilePickerFragment :
FilePickerSideEffect.CannotPinFile -> {
activity?.toast(R.string.ark_file_picker_pin_folder_only)
}

FilePickerSideEffect.NestedRootProhibited -> {
activity?.toast(R.string.ark_file_nested_root_inside)
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.arkbuilders.components.filepicker

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
Expand Down Expand Up @@ -44,6 +45,7 @@ internal sealed class FilePickerSideEffect {
data object AlreadyFavorite : FilePickerSideEffect()
data object PinAsFirstRoot : FilePickerSideEffect()
data object CannotPinFile : FilePickerSideEffect()
data object NestedRootProhibited : FilePickerSideEffect()

}

Expand Down Expand Up @@ -190,6 +192,24 @@ internal class ArkFilePickerViewModel(
val roots = rootsWithFavorites.keys
val root = roots.find { root -> file.startsWith(root) }
val favorites = rootsWithFavorites[root]?.flatten()

val hasNestedRoot = roots.contains(file)
|| (roots.indexOfFirst { path ->
val index = path.toString().indexOf(file.toString())
(index >= 0) && (path.toString()[index] == '/') } >= 0)

Log.d("tuancoltech", "rootsWithFavorites: " + rootsWithFavorites
+ "\nroots: " + roots
+ "\nfavorites: " + favorites
+ "\nPining file: " + file.toString()
+ "\nhasNestedRoot: " + hasNestedRoot)

if (hasNestedRoot) {
Log.v("tuancoltech", "NestedRootProhibited file: " + file)
postSideEffect(FilePickerSideEffect.NestedRootProhibited)
return@intent
}

val haveRoot = haveRoot()

root?.let {
Expand Down Expand Up @@ -232,4 +252,9 @@ internal class ArkFilePickerViewModelFactory(

override fun <T : ViewModel> create(modelClass: Class<T>): T =
ArkFilePickerViewModel(deviceStorageUtils, mode, initialPath) as T
}

fun Path.isChildOf(path: Path): Boolean {
val index = path.toString().indexOf(this.toString())
return (index >= 0) && (path.toString()[index] == '/')
}
1 change: 1 addition & 0 deletions filepicker/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<string name="ark_file_picker_already_a_favorite">Already be a Favorite folder!</string>
<string name="ark_file_picker_already_be_a_root">Already be a Root folder!</string>
<string name="ark_file_picker_pin_folder_only">Only folder can be pinned.</string>
<string name="ark_file_nested_root_inside">There\'s already nested root(s) inside.</string>
<string name="ark_file_picker_pin">Pin</string>
<plurals name="ark_file_picker_items">
<item quantity="one">%d item</item>
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ arkComponentUtils = "0.0.9-SNAPSHOT-01"
akrComponentFoldersTree = "0.0.9-SNAPSHOT-02"
akrComponentTagSelector = "0.0.9-SNAPSHOT-02"
akrComponentScoreWidget = "0.0.9-SNAPSHOT-02"
arkComponentFilePicker = "0.1.0-SNAPSHOT"
arkComponentFilePicker = "0.1.1-SNAPSHOT-04"
coil = "2.4.0"
androidxFragmentKtx = "1.6.1"
fastadapter = "5.7.0"
Expand Down
6 changes: 6 additions & 0 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ android {
}
}

buildscript {
repositories {
mavenLocal()
}
}

dependencies {
implementation(libraries.ark.component.filepicker)
implementation(libraries.arklib)
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pluginManagement {
repositories {
google()
mavenCentral()
mavenLocal()
gradlePluginPortal()
}
}
Expand All @@ -12,6 +13,7 @@ dependencyResolutionManagement {
repositories {
google()
mavenCentral()
mavenLocal()
maven {
url = URI("https://jitpack.io")
}
Expand Down

0 comments on commit 492b448

Please sign in to comment.