mirror of
https://github.com/SimpleMobileTools/Simple-Gallery.git
synced 2025-06-05 21:59:19 +02:00
74 lines
2.7 KiB
Kotlin
74 lines
2.7 KiB
Kotlin
package com.gallery.raw.dialogs
|
|
|
|
import android.view.ViewGroup
|
|
import android.widget.RadioButton
|
|
import android.widget.RadioGroup
|
|
import com.simplemobiletools.commons.activities.BaseSimpleActivity
|
|
import com.simplemobiletools.commons.extensions.beVisibleIf
|
|
import com.simplemobiletools.commons.extensions.getAlertDialogBuilder
|
|
import com.simplemobiletools.commons.extensions.getBasePath
|
|
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
|
import com.gallery.raw.R
|
|
import com.gallery.raw.extensions.config
|
|
import kotlinx.android.synthetic.main.dialog_exclude_folder.view.*
|
|
|
|
class ExcludeFolderDialog(val activity: BaseSimpleActivity, val selectedPaths: List<String>, val callback: () -> Unit) {
|
|
private val alternativePaths = getAlternativePathsList()
|
|
private var radioGroup: RadioGroup? = null
|
|
|
|
init {
|
|
val view = activity.layoutInflater.inflate(R.layout.dialog_exclude_folder, null).apply {
|
|
exclude_folder_parent.beVisibleIf(alternativePaths.size > 1)
|
|
|
|
radioGroup = exclude_folder_radio_group
|
|
exclude_folder_radio_group.beVisibleIf(alternativePaths.size > 1)
|
|
}
|
|
|
|
alternativePaths.forEachIndexed { index, value ->
|
|
val radioButton = (activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton).apply {
|
|
text = alternativePaths[index]
|
|
isChecked = index == 0
|
|
id = index
|
|
}
|
|
radioGroup!!.addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
|
|
}
|
|
|
|
activity.getAlertDialogBuilder()
|
|
.setPositiveButton(R.string.ok) { dialog, which -> dialogConfirmed() }
|
|
.setNegativeButton(R.string.cancel, null)
|
|
.apply {
|
|
activity.setupDialogStuff(view, this)
|
|
}
|
|
}
|
|
|
|
private fun dialogConfirmed() {
|
|
val path = if (alternativePaths.isEmpty()) selectedPaths[0] else alternativePaths[radioGroup!!.checkedRadioButtonId]
|
|
activity.config.addExcludedFolder(path)
|
|
callback()
|
|
}
|
|
|
|
private fun getAlternativePathsList(): List<String> {
|
|
val pathsList = ArrayList<String>()
|
|
if (selectedPaths.size > 1)
|
|
return pathsList
|
|
|
|
val path = selectedPaths[0]
|
|
var basePath = path.getBasePath(activity)
|
|
val relativePath = path.substring(basePath.length)
|
|
val parts = relativePath.split("/").filter(String::isNotEmpty)
|
|
if (parts.isEmpty())
|
|
return pathsList
|
|
|
|
pathsList.add(basePath)
|
|
if (basePath == "/")
|
|
basePath = ""
|
|
|
|
for (part in parts) {
|
|
basePath += "/$part"
|
|
pathsList.add(basePath)
|
|
}
|
|
|
|
return pathsList.reversed()
|
|
}
|
|
}
|