Skip to content

Commit

Permalink
#333: Root folder suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillt committed Feb 12, 2024
1 parent 8bc81cd commit 7ec6a6d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package dev.arkbuilders.navigator.presentation.dialog.rootsscan

import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.setFragmentResult
import moxy.MvpBottomSheetDialogFragment
import moxy.ktx.moxyPresenter
Expand All @@ -12,6 +17,8 @@ import dev.arkbuilders.navigator.databinding.DialogRootsScanBinding
import dev.arkbuilders.navigator.presentation.App
import dev.arkbuilders.navigator.presentation.utils.toast
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.io.path.exists

class RootsScanDialogFragment : MvpBottomSheetDialogFragment(), RootsScanView {
private lateinit var binding: DialogRootsScanBinding
Expand All @@ -20,6 +27,8 @@ class RootsScanDialogFragment : MvpBottomSheetDialogFragment(), RootsScanView {
App.instance.appComponent.inject(this)
}
}
private val defaultRootsFolders =
listOf("DCIM/Camera", "Documents", "Pictures", "Downloads")

override fun onCreateView(
inflater: LayoutInflater,
Expand Down Expand Up @@ -48,6 +57,33 @@ class RootsScanDialogFragment : MvpBottomSheetDialogFragment(), RootsScanView {
btnScan.setOnClickListener {
presenter.onScanBtnClick()
}
initDefaultRootsInfo(defaultRootsFolders)
}

private fun initDefaultRootsInfo(rootsList: List<String>) {
rootsList.forEach { root ->
val view = CheckBox(context)
val params = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
view.layoutParams = params
view.text = root
view.isChecked = true
val selectColor =
ContextCompat.getColor(requireContext(), R.color.colorPrimary)
val normalColor = ContextCompat.getColor(requireContext(), R.color.gray)
view.setTextColor(selectColor)
view.buttonTintList = ColorStateList.valueOf(Color.BLUE)
view.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
view.setTextColor(selectColor)
} else {
view.setTextColor(normalColor)
}
}
binding.layoutDefaultRoot.addView(view)
}
}

override fun startScan() = with(binding) {
Expand Down Expand Up @@ -77,10 +113,33 @@ class RootsScanDialogFragment : MvpBottomSheetDialogFragment(), RootsScanView {
override fun closeDialog() = dismiss()

override fun notifyRootsFound(roots: List<Path>) {
val finalRoots: MutableList<Path> = mutableListOf()
val checkedFolders: MutableList<String> = mutableListOf()
val count = binding.layoutDefaultRoot.childCount
for (i in 0 until count) {
val child = binding.layoutDefaultRoot.getChildAt(i) as CheckBox
if (child.isChecked) {
checkedFolders.add(defaultRootsFolders[i])
}
}
roots.forEach { root ->
var foundDefaultFolder = false
checkedFolders.forEach {
val defaultRoot = Paths.get(root.toString() + '/' + it)
if (defaultRoot.exists()) {
finalRoots.add(defaultRoot)
foundDefaultFolder = true
}
}
if (!foundDefaultFolder) {
finalRoots.add(root)
}
}

val bundle = Bundle().apply {
putStringArray(
RESULT_KEY_ROOTS,
roots.map(Path::toString).toTypedArray()
finalRoots.map(Path::toString).toTypedArray()
)
}
setFragmentResult(REQUEST_KEY_ROOTS_FOUND, bundle)
Expand Down
30 changes: 25 additions & 5 deletions app/src/main/res/layout/dialog_roots_scan.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,41 @@
android:text="@string/root_scanning_description"
android:textColor="@color/black"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@id/btn_scan"
app:layout_constraintBottom_toTopOf="@id/tv_default_root"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_header" />

<TextView
android:id="@+id/tv_default_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:text="default Root folder"
android:textColor="#7A000000"
android:textSize="15sp"
app:layout_constraintTop_toBottomOf="@id/tv_description"/>
<LinearLayout
android:id="@+id/layout_default_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:paddingLeft="30dp"
android:paddingRight="16dp"
android:orientation="vertical"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="@+id/btn_skip"
app:layout_constraintTop_toBottomOf="@id/tv_default_root"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_skip"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:text="@string/skip"
app:layout_constraintBottom_toBottomOf="@id/btn_scan"
app:layout_constraintEnd_toStartOf="@id/btn_scan"
app:layout_constraintTop_toTopOf="@id/btn_scan" />
app:layout_constraintTop_toBottomOf="@id/layout_default_root"
app:layout_constraintEnd_toStartOf="@id/btn_scan" />

<com.google.android.material.button.MaterialButton
android:id="@+id/btn_scan"
Expand Down

0 comments on commit 7ec6a6d

Please sign in to comment.