diff --git a/components/src/main/java/dev/arkbuilders/components/filepicker/ArkFilePickerFragment.kt b/components/src/main/java/dev/arkbuilders/components/filepicker/ArkFilePickerFragment.kt index 414526d..0d7e0a5 100644 --- a/components/src/main/java/dev/arkbuilders/components/filepicker/ArkFilePickerFragment.kt +++ b/components/src/main/java/dev/arkbuilders/components/filepicker/ArkFilePickerFragment.kt @@ -1,5 +1,6 @@ package dev.arkbuilders.components.filepicker +import android.app.AlertDialog import android.content.Context import android.graphics.Color import android.graphics.drawable.ColorDrawable @@ -38,6 +39,7 @@ import dev.arkbuilders.arklib.utils.DeviceStorageUtils import dev.arkbuilders.arklib.utils.INTERNAL_STORAGE import org.orbitmvi.orbit.viewmodel.observe import dev.arkbuilders.components.R +import dev.arkbuilders.components.databinding.ArkFilePickerDialogNewFolderBinding import dev.arkbuilders.components.databinding.ArkFilePickerHostFragmentBinding import dev.arkbuilders.components.databinding.ArkFilePickerItemFileBinding import dev.arkbuilders.components.databinding.ArkFilePickerItemFilesRootsPageBinding @@ -47,6 +49,7 @@ import dev.arkbuilders.components.utils.dpToPx import dev.arkbuilders.components.utils.formatSize import dev.arkbuilders.components.utils.iconForExtension import dev.arkbuilders.components.utils.setDragSensitivity +import java.io.File import java.lang.Exception import java.nio.file.Path import kotlin.io.path.Path @@ -60,20 +63,20 @@ import kotlin.io.path.name open class ArkFilePickerFragment : DialogFragment(R.layout.ark_file_picker_host_fragment) { - var titleStringId by args() - var pickButtonStringId by args() - var cancelButtonStringId by args() - var internalStorageStringId by args() - var itemsPluralId by args() - var themeId by args() - var accessDeniedStringId by args() - var mode by args() - var initialPath by args() - var showRoots by args() - var pathPickedRequestKey by args() - var rootsFirstPage by args() - - var currentFolder: Path? = null + private var titleStringId by args() + private var pickButtonStringId by args() + private var cancelButtonStringId by args() + private var internalStorageStringId by args() + private var itemsPluralId by args() + private var themeId by args() + private var accessDeniedStringId by args() + private var mode by args() + private var initialPath by args() + private var showRoots by args() + private var pathPickedRequestKey by args() + private var rootsFirstPage by args() + + private var currentFolder: Path? = null val binding by viewBinding(ArkFilePickerHostFragmentBinding::bind) private val viewModel by viewModels { ArkFilePickerViewModelFactory( @@ -148,6 +151,41 @@ open class ArkFilePickerFragment : } else { tabs.isVisible = false } + + if (mode == ArkFilePickerMode.FOLDER.ordinal) { + binding.ivNewFolder.visibility = View.VISIBLE + binding.ivNewFolder.setOnClickListener { + showCreateFolderDialog() + } + } else { + binding.ivNewFolder.visibility = View.GONE + } + } + + private fun showCreateFolderDialog() { + val builder = AlertDialog.Builder(activity, android.R.style.ThemeOverlay_Material_Dialog_Alert) + builder.setTitle(R.string.ark_file_picker_new_folder) + val binding = ArkFilePickerDialogNewFolderBinding.inflate(layoutInflater) + builder.setView(binding.root) + builder.setPositiveButton(android.R.string.ok, null) + + builder.setNegativeButton(android.R.string.cancel) { _, _ -> } + val dialog = builder.create() + dialog.show() + dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { + val newFolder = File(currentFolder.toString(), binding.editTextFolderName.text.toString().trim()) + if (newFolder.exists()) { + binding.inputLayoutFolderName.error = getString(R.string.ark_file_picker_folder_existing) + return@setOnClickListener + } + + if (newFolder.mkdirs()) { + //Reload current files tree + currentFolder?.let { viewModel.onItemClick(it) } + dialog.dismiss() + } + } + } private fun render(state: FilePickerState) = binding.apply { diff --git a/components/src/main/res/drawable/ark_file_picker_ic_new_folder.xml b/components/src/main/res/drawable/ark_file_picker_ic_new_folder.xml new file mode 100644 index 0000000..99c60fa --- /dev/null +++ b/components/src/main/res/drawable/ark_file_picker_ic_new_folder.xml @@ -0,0 +1,5 @@ + + + \ No newline at end of file diff --git a/components/src/main/res/layout/ark_file_picker_dialog_new_folder.xml b/components/src/main/res/layout/ark_file_picker_dialog_new_folder.xml new file mode 100644 index 0000000..634d78f --- /dev/null +++ b/components/src/main/res/layout/ark_file_picker_dialog_new_folder.xml @@ -0,0 +1,24 @@ + + + + + + + \ No newline at end of file diff --git a/components/src/main/res/layout/ark_file_picker_host_fragment.xml b/components/src/main/res/layout/ark_file_picker_host_fragment.xml index f8ab9fc..fab9cbb 100644 --- a/components/src/main/res/layout/ark_file_picker_host_fragment.xml +++ b/components/src/main/res/layout/ark_file_picker_host_fragment.xml @@ -9,13 +9,9 @@ + android:text="@string/ark_file_picker_pick_title" + style="@style/ArkFileDialogTitle" + android:padding="@dimen/padding_text_dialog_title"/> + + + + 18sp + 8dp + \ No newline at end of file diff --git a/components/src/main/res/values/strings.xml b/components/src/main/res/values/strings.xml index fcbe9a1..68ec484 100644 --- a/components/src/main/res/values/strings.xml +++ b/components/src/main/res/values/strings.xml @@ -14,6 +14,9 @@ Add favorite Forget root Forget favorite + New folder + Folder already exists! + Name your folder %d item %d items diff --git a/components/src/main/res/values/styles.xml b/components/src/main/res/values/styles.xml index ecef88f..23a34dd 100644 --- a/components/src/main/res/values/styles.xml +++ b/components/src/main/res/values/styles.xml @@ -4,4 +4,13 @@ @anim/ark_file_picker_fade_in @anim/ark_file_pickerfade_out + + + \ No newline at end of file diff --git a/sample/src/main/java/dev/arkbuilders/sample/MainActivity.kt b/sample/src/main/java/dev/arkbuilders/sample/MainActivity.kt index a4b39fb..2ba7c5e 100644 --- a/sample/src/main/java/dev/arkbuilders/sample/MainActivity.kt +++ b/sample/src/main/java/dev/arkbuilders/sample/MainActivity.kt @@ -41,10 +41,17 @@ class MainActivity : AppCompatActivity() { .newInstance() .show(supportFragmentManager, null) } + + findViewById(R.id.btn_open_file_mode).setOnClickListener { + resolvePermissions() + ArkFilePickerFragment + .newInstance(getFilePickerConfig(mode = ArkFilePickerMode.FILE)) + .show(supportFragmentManager, null) + } } - private fun getFilePickerConfig() = ArkFilePickerConfig( - mode = ArkFilePickerMode.FOLDER, + private fun getFilePickerConfig(mode: ArkFilePickerMode? = null) = ArkFilePickerConfig( + mode = mode ?: ArkFilePickerMode.FOLDER, titleStringId = R.string.file_picker_title, showRoots = true, rootsFirstPage = false diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index a11ce7a..bf2ee0c 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -1,7 +1,6 @@ @@ -9,7 +8,7 @@ android:id="@+id/btn_open" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="Open file picker" + android:text="@string/file_picker_open_folder" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" @@ -24,4 +23,13 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/btn_open" /> + + \ No newline at end of file diff --git a/sample/src/main/res/values/strings.xml b/sample/src/main/res/values/strings.xml index 4f10c20..4da38f8 100644 --- a/sample/src/main/res/values/strings.xml +++ b/sample/src/main/res/values/strings.xml @@ -1,4 +1,6 @@ Ark Components Pick favorite folder + Open folder picker + Open file picker \ No newline at end of file