Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

WIP: [feature] Folder creation capability #29

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.arkbuilders.arkfilepicker.presentation.filepicker

import android.app.AlertDialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
Expand All @@ -17,7 +18,6 @@ import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResult
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import by.kirich1409.viewbindingdelegate.viewBinding
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
Expand All @@ -27,13 +27,13 @@ import com.mikepenz.fastadapter.FastAdapter
import com.mikepenz.fastadapter.GenericItem
import com.mikepenz.fastadapter.adapters.ItemAdapter
import com.mikepenz.fastadapter.binding.AbstractBindingItem
import kotlinx.coroutines.launch
import org.orbitmvi.orbit.viewmodel.observe
import dev.arkbuilders.arkfilepicker.ArkFilePickerConfig
import dev.arkbuilders.arkfilepicker.presentation.DevicesPopup
import dev.arkbuilders.arkfilepicker.FileUtils
import dev.arkbuilders.arkfilepicker.INTERNAL_STORAGE
import dev.arkbuilders.arkfilepicker.R
import dev.arkbuilders.arkfilepicker.databinding.ArkFilePickerDialogNewFolderBinding
import dev.arkbuilders.arkfilepicker.databinding.ArkFilePickerHostFragmentBinding
import dev.arkbuilders.arkfilepicker.databinding.ArkFilePickerItemFileBinding
import dev.arkbuilders.arkfilepicker.databinding.ArkFilePickerItemFilesRootsPageBinding
Expand All @@ -43,8 +43,8 @@ import dev.arkbuilders.arkfilepicker.iconForExtension
import dev.arkbuilders.arkfilepicker.listChildren
import dev.arkbuilders.arkfilepicker.presentation.args
import dev.arkbuilders.arkfilepicker.presentation.folderstree.FolderTreeView
import dev.arkbuilders.arkfilepicker.folders.FoldersRepo
import dev.arkbuilders.arkfilepicker.setDragSensitivity
import java.io.File
import java.lang.Exception
import java.nio.file.Path
import kotlin.io.path.Path
Expand Down Expand Up @@ -144,6 +144,43 @@ 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())
if (newFolder.exists()) {
binding.inputLayoutFolderName.error = getString(R.string.ark_file_picker_folder_existing)
return@setOnClickListener
}
val isSuccess = newFolder.mkdirs()

if (isSuccess) {
//Reload current files tree
currentFolder?.let { viewModel.onItemClick(it) }
dialog.dismiss()
}
}

}

private fun render(state: FilePickerState) = binding.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ internal class ArkFilePickerViewModel(
onPathPicked(path)
}

fun onPickBtnClick() = intent { onPathPicked(state.currentPath) }
fun onPickBtnClick() = intent {
onPathPicked(state.currentPath)
}

fun onDeviceSelected(selectedDevicePos: Int) = intent {
val selectedDevice = state.devices[selectedDevicePos]
Expand Down
5 changes: 5 additions & 0 deletions lib/src/main/res/drawable/ark_file_picker_ic_new_folder.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M20,6h-8l-2,-2L4,4c-1.11,0 -1.99,0.89 -1.99,2L2,18c0,1.11 0.89,2 2,2h16c1.11,0 2,-0.89 2,-2L22,8c0,-1.11 -0.89,-2 -2,-2zM19,14h-3v3h-2v-3h-3v-2h3L14,9h2v3h3v2z"/>
</vector>
22 changes: 22 additions & 0 deletions lib/src/main/res/layout/ark_file_picker_dialog_new_folder.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.textfield.TextInputLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="20dp"
android:id="@+id/input_layout_folder_name"
android:hint="@string/ark_file_picker_new_folder_hint">

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text_folder_name"
android:background="@android:color/transparent"/>


</com.google.android.material.textfield.TextInputLayout>
20 changes: 13 additions & 7 deletions lib/src/main/res/layout/ark_file_picker_host_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@

<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pick"
android:textAlignment="center"
android:padding="8dp"
android:textColor="@color/ark_file_picker_black"
android:textSize="18sp" />
android:text="@string/ark_file_picker_pick_title"
style="@style/ArkFileDialogTitle"
android:padding="@dimen/padding_text_dialog_title"/>

<HorizontalScrollView
android:id="@+id/scroll_path"
Expand All @@ -40,6 +36,16 @@
</LinearLayout>
</HorizontalScrollView>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ark_file_picker_ic_new_folder"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:id="@+id/iv_new_folder"
app:tint="@color/ark_file_picker_gray"
android:background="?attr/selectableItemBackgroundBorderless"/>

<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
Expand Down
5 changes: 5 additions & 0 deletions lib/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size_dialog_title">18sp</dimen>
<dimen name="padding_text_dialog_title">8dp</dimen>
</resources>
3 changes: 3 additions & 0 deletions lib/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<string name="ark_file_picker_add_favorite">Add favorite</string>
<string name="ark_file_picker_forget_root">Forget root</string>
<string name="ark_file_picker_forget_favorite">Forget favorite</string>
<string name="ark_file_picker_new_folder">New folder</string>
<string name="ark_file_picker_folder_existing">Folder already exists!</string>
<string name="ark_file_picker_new_folder_hint">Name your folder</string>
<plurals name="ark_file_picker_items">
<item quantity="one">%d item</item>
<item quantity="other">%d items</item>
Expand Down
8 changes: 8 additions & 0 deletions lib/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@
<item name="android:windowEnterAnimation">@anim/ark_file_picker_fade_in</item>
<item name="android:windowExitAnimation">@anim/ark_file_pickerfade_out</item>
</style>

<style name="ArkFileDialogTitle">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">@dimen/text_size_dialog_title</item>
<item name="android:textColor">@color/ark_file_picker_black</item>
<item name="android:gravity">center</item>
</style>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,17 @@ class MainActivity : AppCompatActivity() {
.newInstance()
.show(supportFragmentManager, null)
}

findViewById<MaterialButton>(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
Expand Down
11 changes: 10 additions & 1 deletion sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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"
Expand All @@ -25,4 +25,13 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_open" />

<com.google.android.material.button.MaterialButton
android:id="@+id/btn_open_file_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/file_picker_open_file"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_root_picker"/>

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<resources>
<string name="app_name">Sample</string>
<string name="file_picker_title">Pick favorite folder</string>
<string name="file_picker_open_folder">Open folder picker</string>
<string name="file_picker_open_file">Open file picker</string>
</resources>
Loading