From a8858c1c12ce63333dd5fafc78d6934b08825f62 Mon Sep 17 00:00:00 2001 From: machiav3lli Date: Fri, 24 Jan 2025 01:33:16 +0100 Subject: [PATCH] Revert: Encapsuling dialogs --- .../backup/entity/PreferenceItem.kt | 84 ++++++------------- .../backup/preferences/ServicePreferences.kt | 37 ++++++-- .../backup/preferences/UserPreferences.kt | 56 +++++++++++-- 3 files changed, 102 insertions(+), 75 deletions(-) diff --git a/src/main/java/com/machiav3lli/backup/entity/PreferenceItem.kt b/src/main/java/com/machiav3lli/backup/entity/PreferenceItem.kt index 1ad37cea70..5f52123bf6 100644 --- a/src/main/java/com/machiav3lli/backup/entity/PreferenceItem.kt +++ b/src/main/java/com/machiav3lli/backup/entity/PreferenceItem.kt @@ -5,15 +5,10 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateMapOf import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.vector.ImageVector import com.machiav3lli.backup.OABX -import com.machiav3lli.backup.dialogs.BaseDialog -import com.machiav3lli.backup.dialogs.EnumPrefDialogUI -import com.machiav3lli.backup.dialogs.ListPrefDialogUI -import com.machiav3lli.backup.dialogs.StringPrefDialogUI import com.machiav3lli.backup.handler.LogsHandler import com.machiav3lli.backup.preferences.publicPreferences import com.machiav3lli.backup.preferences.traceDebug @@ -399,18 +394,12 @@ open class StringPref( summaryId = summaryId, summary = summary, UI = UI ?: { pref, onDialogUI, index, groupSize -> - val openDialog = remember { mutableStateOf(false) } - StringPreference(pref = pref as StringPref, index = index, groupSize = groupSize) { - openDialog.value = true - } - if (openDialog.value) { - BaseDialog(onDismiss = { openDialog.value = false }) { - StringPrefDialogUI( - pref = pref, - openDialogCustom = openDialog - ) - } - } + StringPreference( + pref = pref as StringPref, + index = index, + groupSize = groupSize, + onClick = { onDialogUI(pref) }, + ) }, icon = icon, iconTint = iconTint, @@ -451,7 +440,7 @@ class StringEditPref( titleId = titleId, summaryId = summaryId, summary = summary, - UI = UI ?: { pref, onDialogUI, index, groupSize -> + UI = UI ?: { pref, _, index, groupSize -> StringEditPreference(pref = pref as StringEditPref, index = index, groupSize = groupSize) }, icon = icon, @@ -481,20 +470,12 @@ class PasswordPref( summaryId = summaryId, summary = summary, UI = UI ?: { pref, onDialogUI, index, groupSize -> - val openDialog = remember { mutableStateOf(false) } - PasswordPreference(pref = pref as PasswordPref, index = index, groupSize = groupSize) { - openDialog.value = true - } - if (openDialog.value) { - BaseDialog(onDismiss = { openDialog.value = false }) { - StringPrefDialogUI( - pref = pref, - isPrivate = true, - confirm = true, - openDialogCustom = openDialog - ) - } - } + PasswordPreference( + pref = pref as PasswordPref, + index = index, + groupSize = groupSize, + onClick = { onDialogUI(pref) }, + ) }, icon = icon, iconTint = iconTint, @@ -524,18 +505,12 @@ class ListPref( summaryId = summaryId, summary = summary, UI = UI ?: { pref, onDialogUI, index, groupSize -> - val openDialog = remember { mutableStateOf(false) } - ListPreference(pref = pref as ListPref, index = index, groupSize = groupSize) { - openDialog.value = true - } - if (openDialog.value) { - BaseDialog(onDismiss = { openDialog.value = false }) { - ListPrefDialogUI( - pref = pref, - openDialogCustom = openDialog, - ) - } - } + ListPreference( + pref = pref as ListPref, + index = index, + groupSize = groupSize, + onClick = { onDialogUI(pref) }, + ) }, icon = icon, iconTint = iconTint, @@ -543,7 +518,6 @@ class ListPref( onChanged = onChanged ) - class EnumPref( key: String, private: Boolean = false, @@ -565,18 +539,12 @@ class EnumPref( summaryId = summaryId, summary = summary, UI = UI ?: { pref, onDialogUI, index, groupSize -> - val openDialog = remember { mutableStateOf(false) } - EnumPreference(pref = pref as EnumPref, index = index, groupSize = groupSize) { - openDialog.value = true - } - if (openDialog.value) { - BaseDialog(onDismiss = { openDialog.value = false }) { - EnumPrefDialogUI( - pref = pref as EnumPref, - openDialogCustom = openDialog, - ) - } - } + EnumPreference( + pref = pref as EnumPref, + index = index, + groupSize = groupSize, + onClick = { onDialogUI(pref) }, + ) }, icon = icon, iconTint = iconTint, @@ -644,7 +612,7 @@ class LaunchPref( titleId = titleId, summaryId = summaryId, summary = summary, - UI = UI ?: { pref, onDialogUI, index, groupSize -> + UI = UI ?: { pref, _, index, groupSize -> LaunchPreference( pref = pref as LaunchPref, index = index, diff --git a/src/main/java/com/machiav3lli/backup/preferences/ServicePreferences.kt b/src/main/java/com/machiav3lli/backup/preferences/ServicePreferences.kt index d6afa669a9..2964e76dd9 100644 --- a/src/main/java/com/machiav3lli/backup/preferences/ServicePreferences.kt +++ b/src/main/java/com/machiav3lli/backup/preferences/ServicePreferences.kt @@ -44,15 +44,6 @@ import com.machiav3lli.backup.ui.compose.icons.phosphor.ShieldStar import com.machiav3lli.backup.ui.compose.icons.phosphor.TagSimple import com.machiav3lli.backup.ui.compose.icons.phosphor.Textbox import com.machiav3lli.backup.ui.compose.recycler.InnerBackground -import com.machiav3lli.backup.ui.compose.theme.ColorAPK -import com.machiav3lli.backup.ui.compose.theme.ColorData -import com.machiav3lli.backup.ui.compose.theme.ColorDeData -import com.machiav3lli.backup.ui.compose.theme.ColorExodus -import com.machiav3lli.backup.ui.compose.theme.ColorExtDATA -import com.machiav3lli.backup.ui.compose.theme.ColorMedia -import com.machiav3lli.backup.ui.compose.theme.ColorOBB -import com.machiav3lli.backup.ui.compose.theme.ColorSpecial -import com.machiav3lli.backup.ui.compose.theme.ColorUpdated import com.machiav3lli.backup.utils.SystemUtils import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.toPersistentList @@ -74,6 +65,34 @@ fun ServicePrefsPage() { } } } + + if (openDialog.value) { + BaseDialog(onDismiss = { openDialog.value = false }) { + when (dialogsPref) { + is ListPref -> ListPrefDialogUI( + pref = dialogsPref as ListPref, + openDialogCustom = openDialog, + ) + + is EnumPref -> EnumPrefDialogUI( + pref = dialogsPref as EnumPref, + openDialogCustom = openDialog + ) + + is PasswordPref -> StringPrefDialogUI( + pref = dialogsPref as PasswordPref, + isPrivate = true, + confirm = true, + openDialogCustom = openDialog + ) + + is StringPref -> StringPrefDialogUI( + pref = dialogsPref as StringPref, + openDialogCustom = openDialog + ) + } + } + } } fun LazyListScope.ServicePrefGroups(onPrefDialog: (Pref) -> Unit) { diff --git a/src/main/java/com/machiav3lli/backup/preferences/UserPreferences.kt b/src/main/java/com/machiav3lli/backup/preferences/UserPreferences.kt index cfb6cb5bc4..df94528264 100644 --- a/src/main/java/com/machiav3lli/backup/preferences/UserPreferences.kt +++ b/src/main/java/com/machiav3lli/backup/preferences/UserPreferences.kt @@ -25,6 +25,9 @@ import com.machiav3lli.backup.R import com.machiav3lli.backup.THEME_DYNAMIC import com.machiav3lli.backup.THEME_SYSTEM import com.machiav3lli.backup.accentColorItems +import com.machiav3lli.backup.dialogs.BaseDialog +import com.machiav3lli.backup.dialogs.EnumPrefDialogUI +import com.machiav3lli.backup.dialogs.ListPrefDialogUI import com.machiav3lli.backup.entity.BooleanPref import com.machiav3lli.backup.entity.EnumPref import com.machiav3lli.backup.entity.IntPref @@ -52,12 +55,6 @@ import com.machiav3lli.backup.ui.compose.icons.phosphor.TextAa import com.machiav3lli.backup.ui.compose.icons.phosphor.Translate import com.machiav3lli.backup.ui.compose.item.StringEditPreference import com.machiav3lli.backup.ui.compose.recycler.InnerBackground -import com.machiav3lli.backup.ui.compose.theme.ColorDeData -import com.machiav3lli.backup.ui.compose.theme.ColorExodus -import com.machiav3lli.backup.ui.compose.theme.ColorOBB -import com.machiav3lli.backup.ui.compose.theme.ColorSpecial -import com.machiav3lli.backup.ui.compose.theme.ColorSystem -import com.machiav3lli.backup.ui.compose.theme.ColorUpdated import com.machiav3lli.backup.utils.StorageLocationNotConfiguredException import com.machiav3lli.backup.utils.SystemUtils import com.machiav3lli.backup.utils.backupDirConfigured @@ -83,6 +80,29 @@ fun UserPrefsPage() { val prefs = Pref.prefGroups["user"]?.toPersistentList() ?: persistentListOf() + val launcher = + rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + if (result.data != null && result.resultCode == Activity.RESULT_OK) { + result.data?.let { + val uri = it.data ?: return@let + val oldDir = try { + backupDirConfigured + } catch (e: StorageLocationNotConfiguredException) { + "" // Can be ignored, this is about to set the path + } + if (oldDir != uri.toString()) { + val flags = it.flags and ( + Intent.FLAG_GRANT_READ_URI_PERMISSION or + Intent.FLAG_GRANT_WRITE_URI_PERMISSION + ) + context.contentResolver.takePersistableUriPermission(uri, flags) + Timber.i("setting uri $uri") + backupDir = setBackupDir(uri) + } + } + } + } + InnerBackground( modifier = Modifier.fillMaxSize() ) { @@ -99,6 +119,26 @@ fun UserPrefsPage() { } } } + + if (openDialog.value) { + BaseDialog(onDismiss = { openDialog.value = false }) { + when (dialogsPref) { + //pref_languages, + is ListPref -> ListPrefDialogUI( + pref = dialogsPref as ListPref, + openDialogCustom = openDialog, + ) + + //pref_appTheme, + //pref_appAccentColor, + //pref_appSecondaryColor, + is EnumPref -> EnumPrefDialogUI( + pref = dialogsPref as EnumPref, + openDialogCustom = openDialog, + ) + } + } + } } fun onThemeChanged(pref: Pref) { @@ -176,7 +216,7 @@ val pref_pathBackupFolder = StringEditPref( else if (backupFolderExists(pref.value)) Color.Green.copy(alpha = alpha) else Color.Red.copy(alpha = alpha) }, - UI = { it, onDialogUI, index, groupSize -> + UI = { it, _, index, groupSize -> val pref = it as StringEditPref val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> @@ -192,7 +232,7 @@ val pref_pathBackupFolder = StringEditPref( val flags = it.flags and ( Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION -) + ) //TODO hg42 check and remember if flags are read only and implement appropriate actions elsewhere OABX.context.contentResolver.takePersistableUriPermission(uri, flags) Timber.i("setting uri $uri")