From 7ec6a6de13d2df4198393111fa82a3136db1cf3b Mon Sep 17 00:00:00 2001 From: Kirill Taran Date: Tue, 18 Apr 2023 10:53:29 +0300 Subject: [PATCH] #333: Root folder suggestions --- .../rootsscan/RootsScanDialogFragment.kt | 61 ++++++++++++++++++- app/src/main/res/layout/dialog_roots_scan.xml | 30 +++++++-- 2 files changed, 85 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/dev/arkbuilders/navigator/presentation/dialog/rootsscan/RootsScanDialogFragment.kt b/app/src/main/java/dev/arkbuilders/navigator/presentation/dialog/rootsscan/RootsScanDialogFragment.kt index d4c3c003..9b1dd1bd 100644 --- a/app/src/main/java/dev/arkbuilders/navigator/presentation/dialog/rootsscan/RootsScanDialogFragment.kt +++ b/app/src/main/java/dev/arkbuilders/navigator/presentation/dialog/rootsscan/RootsScanDialogFragment.kt @@ -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 @@ -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 @@ -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, @@ -48,6 +57,33 @@ class RootsScanDialogFragment : MvpBottomSheetDialogFragment(), RootsScanView { btnScan.setOnClickListener { presenter.onScanBtnClick() } + initDefaultRootsInfo(defaultRootsFolders) + } + + private fun initDefaultRootsInfo(rootsList: List) { + 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) { @@ -77,10 +113,33 @@ class RootsScanDialogFragment : MvpBottomSheetDialogFragment(), RootsScanView { override fun closeDialog() = dismiss() override fun notifyRootsFound(roots: List) { + val finalRoots: MutableList = mutableListOf() + val checkedFolders: MutableList = 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) diff --git a/app/src/main/res/layout/dialog_roots_scan.xml b/app/src/main/res/layout/dialog_roots_scan.xml index 0fe2af87..6a187547 100644 --- a/app/src/main/res/layout/dialog_roots_scan.xml +++ b/app/src/main/res/layout/dialog_roots_scan.xml @@ -30,11 +30,32 @@ 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" /> - + + + app:layout_constraintTop_toBottomOf="@id/layout_default_root" + app:layout_constraintEnd_toStartOf="@id/btn_scan" />