Skip to content

Commit

Permalink
refactor: preference manager
Browse files Browse the repository at this point in the history
  • Loading branch information
aliernfrog authored Sep 14, 2024
1 parent ae9c813 commit c2aa90a
Show file tree
Hide file tree
Showing 26 changed files with 273 additions and 229 deletions.
44 changes: 4 additions & 40 deletions app/src/main/java/com/aliernfrog/pftool/Constant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,15 @@ val supportsPerAppLanguagePreferences = Build.VERSION.SDK_INT >= Build.VERSION_C
val folderPickerSupportsInitialUri = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
val hasAndroidDataRestrictions = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R

object ConfigKey {
const val PREF_NAME = "APP_CONFIG"
const val KEY_APP_LANGUAGE = "appLanguage"
const val KEY_APP_THEME = "appTheme"
const val KEY_APP_MATERIAL_YOU = "materialYou"
const val KEY_APP_AUTO_UPDATES = "autoUpdates"
const val KEY_APP_UPDATES_URL = "updatesUrl"
const val KEY_SHOW_CHOSEN_MAP_THUMBNAIL = "showChosenMapThumbnail"
const val KEY_SHOW_MAP_THUMBNAILS_LIST = "showMapThumbnailsList"
const val KEY_MAPS_DIR = "mapsDir"
const val KEY_EXPORTED_MAPS_DIR = "mapsExportDir"
const val DEFAULT_UPDATES_URL = "https://aliernfrog.github.io/pftool/latest.json"
val RECOMMENDED_MAPS_DIR = "${Environment.getExternalStorageDirectory()}/Android/data/com.MA.Polyfield/files/editor"
val RECOMMENDED_EXPORTED_MAPS_DIR = "${Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)}/PFTool/exported"
}

object SettingsConstant {
val folders = listOf(
PrefEditItem(
labelResourceId = R.string.settings_storage_folders_maps,
getValue = { it.pfMapsDir },
setValue = { newValue, prefs ->
prefs.pfMapsDir = newValue
},
default = ConfigKey.RECOMMENDED_MAPS_DIR
preference = { it.pfMapsDir },
label = { R.string.settings_storage_folders_maps }
),
PrefEditItem(
labelResourceId = R.string.settings_storage_folders_exportedMaps,
getValue = { it.exportedMapsDir },
setValue = { newValue, prefs ->
prefs.exportedMapsDir = newValue
},
default = ConfigKey.RECOMMENDED_EXPORTED_MAPS_DIR
preference = { it.exportedMapsDir },
label = { R.string.settings_storage_folders_exportedMaps }
)
)

Expand Down Expand Up @@ -110,18 +86,6 @@ object SettingsConstant {
link = "https://github.com/ReVanced/revanced-manager"
)
)

val experimentalPrefOptions = listOf(
PrefEditItem(
labelResourceId = R.string.settings_experimental_updatesURL,
getValue = { it.updatesURL },
setValue = { newValue, prefs ->
prefs.updatesURL = newValue
},
default = ConfigKey.DEFAULT_UPDATES_URL
),
*folders.toTypedArray()
)
}

val languages = BuildConfig.LANGUAGES.sorted().map { langCode ->
Expand Down
15 changes: 10 additions & 5 deletions app/src/main/java/com/aliernfrog/pftool/data/PermissionData.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.aliernfrog.pftool.data

import android.net.Uri
import android.os.Environment
import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
import com.aliernfrog.pftool.util.manager.base.BasePreferenceManager

data class PermissionData(
@StringRes val title: Int,
val recommendedPath: String?,
val pref: BasePreferenceManager.Preference<String>,
val recommendedPath: String? = pref.defaultValue,
@StringRes val recommendedPathDescription: Int?,
@StringRes val recommendedPathWarning: Int? = null,
@StringRes val useUnrecommendedAnywayDescription: Int? = null,
val forceRecommendedPath: Boolean = true,
val getUri: () -> String,
val onUriUpdate: (Uri) -> Unit,
val content: @Composable () -> Unit
)
)

val PermissionData.requiresAndroidData: Boolean
get() = forceRecommendedPath && recommendedPath?.startsWith(
"${Environment.getExternalStorageDirectory()}/Android/data"
) == true
11 changes: 5 additions & 6 deletions app/src/main/java/com/aliernfrog/pftool/data/PrefEditItem.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.aliernfrog.pftool.data

import com.aliernfrog.pftool.util.manager.PreferenceManager
import com.aliernfrog.pftool.util.manager.base.BasePreferenceManager

data class PrefEditItem(
val labelResourceId: Int,
val getValue: (prefs: PreferenceManager) -> String,
val setValue: (newValue: String, prefs: PreferenceManager) -> Unit,
val default: String = ""
)
data class PrefEditItem<T>(
val preference: (PreferenceManager) -> BasePreferenceManager.Preference<T>,
val label: (PreferenceManager) -> Any = { preference(it).key }
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ enum class StorageAccessType(
label = R.string.settings_storage_storageAccessType_saf,
description = R.string.settings_storage_storageAccessType_saf_description,
enable = {
it.storageAccessType = SAF.ordinal
it.pfMapsDir = FileUtil.getTreeUriForPath(it.pfMapsDir).toString()
it.exportedMapsDir = FileUtil.getTreeUriForPath(it.exportedMapsDir).toString()
it.storageAccessType.value = SAF.ordinal
it.pfMapsDir.value = FileUtil.getTreeUriForPath(it.pfMapsDir.value).toString()
it.exportedMapsDir.value = FileUtil.getTreeUriForPath(it.exportedMapsDir.value).toString()
}
),

Expand All @@ -28,9 +28,9 @@ enum class StorageAccessType(
description = R.string.settings_storage_storageAccessType_shizuku_description,
minSDK = Build.VERSION_CODES.M,
enable = {
it.storageAccessType = SHIZUKU.ordinal
it.pfMapsDir = FileUtil.getFilePath(it.pfMapsDir)
it.exportedMapsDir = FileUtil.getFilePath(it.exportedMapsDir)
it.storageAccessType.value = SHIZUKU.ordinal
it.pfMapsDir.value = FileUtil.getFilePath(it.pfMapsDir.value)
it.exportedMapsDir.value = FileUtil.getFilePath(it.exportedMapsDir.value)
}
),

Expand All @@ -39,9 +39,9 @@ enum class StorageAccessType(
description = R.string.settings_storage_storageAccessType_allFiles_description,
maxSDK = Build.VERSION_CODES.N_MR1,
enable = {
it.storageAccessType = ALL_FILES.ordinal
it.pfMapsDir = FileUtil.getFilePath(it.pfMapsDir)
it.exportedMapsDir = FileUtil.getFilePath(it.exportedMapsDir)
it.storageAccessType.value = ALL_FILES.ordinal
it.pfMapsDir.value = FileUtil.getFilePath(it.pfMapsDir.value)
it.exportedMapsDir.value = FileUtil.getFilePath(it.exportedMapsDir.value)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ class MainActivity : AppCompatActivity() {
val context = LocalContext.current
val view = LocalView.current
val scope = rememberCoroutineScope()
val useDarkTheme = shouldUseDarkTheme(mainViewModel.prefs.theme)
val useDarkTheme = shouldUseDarkTheme(mainViewModel.prefs.theme.value)

@Composable
fun AppTheme(content: @Composable () -> Unit) {
PFToolTheme(
darkTheme = useDarkTheme,
dynamicColors = mainViewModel.prefs.materialYou,
pitchBlack = mainViewModel.prefs.pitchBlack,
dynamicColors = mainViewModel.prefs.materialYou.value,
pitchBlack = mainViewModel.prefs.pitchBlack.value,
content = content
)
}
Expand All @@ -69,7 +69,7 @@ class MainActivity : AppCompatActivity() {
mainViewModel.topToastState.setComposeView(view)
mainViewModel.topToastState.setAppTheme { AppTheme(it) }

if (mainViewModel.prefs.autoCheckUpdates) mainViewModel.checkUpdates()
if (mainViewModel.prefs.autoCheckUpdates.value) mainViewModel.checkUpdates()

this@MainActivity.intent?.let {
mainViewModel.handleIntent(it, context = context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ fun MapsListScreen(

MapButton(
map = map,
showMapThumbnail = mapsListViewModel.prefs.showMapThumbnailsInList,
showMapThumbnail = mapsListViewModel.prefs.showMapThumbnailsInList.value,
modifier = Modifier.animateItem(),
trailingComponent = {
if (isMultiSelecting) Checkbox(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import com.aliernfrog.pftool.ConfigKey
import com.aliernfrog.pftool.R
import com.aliernfrog.pftool.data.PermissionData
import com.aliernfrog.pftool.ui.dialog.CustomMessageDialog
Expand All @@ -27,27 +26,19 @@ fun MapsPermissionsScreen(
val permissions = remember { arrayOf(
PermissionData(
title = R.string.permissions_maps,
recommendedPath = ConfigKey.RECOMMENDED_MAPS_DIR,
pref = mapsViewModel.prefs.pfMapsDir,
recommendedPathDescription = R.string.permissions_maps_recommended,
recommendedPathWarning = R.string.permissions_maps_openPFToCreate,
useUnrecommendedAnywayDescription = R.string.permissions_maps_useUnrecommendedAnyway,
getUri = { mapsViewModel.prefs.pfMapsDir },
onUriUpdate = {
mapsViewModel.prefs.pfMapsDir = it.toString()
},
content = {
Text(stringResource(R.string.permissions_maps_description))
}
),
PermissionData(
title = R.string.permissions_exportedMaps,
recommendedPath = ConfigKey.RECOMMENDED_EXPORTED_MAPS_DIR,
pref = mapsViewModel.prefs.exportedMapsDir,
recommendedPathDescription = R.string.permissions_exportedMaps_recommended,
forceRecommendedPath = false,
getUri = { mapsViewModel.prefs.exportedMapsDir },
onUriUpdate = {
mapsViewModel.prefs.exportedMapsDir = it.toString()
},
content = {
Text(stringResource(R.string.permissions_exportedMaps_description))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fun MapsScreen(
Column(Modifier.fillMaxSize().verticalScroll(mapsViewModel.scrollState)) {
PickMapButton(
chosenMap = mapsViewModel.chosenMap,
showMapThumbnail = mapsViewModel.prefs.showChosenMapThumbnail
showMapThumbnail = mapsViewModel.prefs.showChosenMapThumbnail.value
) {
mapsViewModel.mapListShown = true
}
Expand Down Expand Up @@ -114,9 +114,9 @@ private fun Actions(
.clip(AppComponentShape)
)

FadeVisibility(visible = mapsViewModel.prefs.showMapNameFieldGuide) {
FadeVisibility(visible = mapsViewModel.prefs.showMapNameFieldGuide.value) {
OutlinedCard(
onClick = { mapsViewModel.prefs.showMapNameFieldGuide = false },
onClick = { mapsViewModel.prefs.showMapNameFieldGuide.value = false },
shape = AppComponentShape,
modifier = Modifier.padding(
horizontal = 8.dp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fun PermissionsScreen(
}

AnimatedContent(
StorageAccessType.entries[permissionsViewModel.prefs.storageAccessType]
StorageAccessType.entries[permissionsViewModel.prefs.storageAccessType.value]
) { method ->
AnimatedContent(permissionsGranted) { showContent ->
if (showContent) content()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.aliernfrog.pftool.R
import com.aliernfrog.pftool.data.PermissionData
import com.aliernfrog.pftool.data.requiresAndroidData
import com.aliernfrog.pftool.enum.StorageAccessType
import com.aliernfrog.pftool.enum.isCompatible
import com.aliernfrog.pftool.ui.component.CardWithActions
Expand All @@ -41,7 +42,6 @@ import com.aliernfrog.pftool.ui.component.form.FormSection
import com.aliernfrog.pftool.ui.dialog.ChooseFolderIntroDialog
import com.aliernfrog.pftool.ui.dialog.UnrecommendedFolderDialog
import com.aliernfrog.pftool.ui.viewmodel.PermissionsViewModel
import com.aliernfrog.pftool.util.extension.requiresAndroidData
import com.aliernfrog.pftool.util.extension.toPath
import com.aliernfrog.pftool.util.extension.takePersistablePermissions
import com.aliernfrog.pftool.util.staticutil.FileUtil
Expand Down Expand Up @@ -119,7 +119,7 @@ private fun SAFPermissionsList(

fun takePersistableUriPermissions(uri: Uri) {
uri.takePersistablePermissions(context)
activePermissionData?.onUriUpdate?.invoke(uri)
activePermissionData?.pref?.value = uri.toString()
onUpdateState()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ fun AboutPage(
SwitchRow(
title = stringResource(R.string.settings_about_autoCheckUpdates),
description = stringResource(R.string.settings_about_autoCheckUpdates_description),
checked = settingsViewModel.prefs.autoCheckUpdates,
checked = settingsViewModel.prefs.autoCheckUpdates.value,
modifier = Modifier.padding(top = 8.dp)
) {
settingsViewModel.prefs.autoCheckUpdates = it
settingsViewModel.prefs.autoCheckUpdates.value = it
}

FormSection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ fun AppearancePage(
) {
SegmentedButtons(
options = Theme.entries.map { stringResource(it.label) },
selectedIndex = settingsViewModel.prefs.theme,
selectedIndex = settingsViewModel.prefs.theme.value,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp)
) {
settingsViewModel.prefs.theme = it
settingsViewModel.prefs.theme.value = it
}
}
FormSection(
Expand All @@ -52,18 +52,18 @@ fun AppearancePage(
else R.string.settings_appearance_materialYou_unavailable
),
painter = rememberVectorPainter(Icons.Outlined.Brush),
checked = settingsViewModel.prefs.materialYou,
checked = settingsViewModel.prefs.materialYou.value,
enabled = supportsMaterialYou
) {
settingsViewModel.prefs.materialYou = it
settingsViewModel.prefs.materialYou.value = it
}
SwitchRow(
title = stringResource(R.string.settings_appearance_pitchBlack),
description = stringResource(R.string.settings_appearance_pitchBlack_description),
painter = rememberVectorPainter(Icons.Outlined.Contrast),
checked = settingsViewModel.prefs.pitchBlack
checked = settingsViewModel.prefs.pitchBlack.value
) {
settingsViewModel.prefs.pitchBlack = it
settingsViewModel.prefs.pitchBlack.value = it
}
}
}
Expand Down
Loading

0 comments on commit c2aa90a

Please sign in to comment.