refactor preferences

This commit is contained in:
X1nto 2021-11-10 18:55:30 +04:00
parent f3ebe85e30
commit f93210b62b
15 changed files with 63 additions and 76 deletions

View File

@ -14,22 +14,19 @@ class MusicDownloader(
private val context: Context, private val context: Context,
) : AppDownloader() { ) : AppDownloader() {
private val version by musicVersionPref
private val variant by managerVariantPref
private lateinit var absoluteVersion: String private lateinit var absoluteVersion: String
override suspend fun download( override suspend fun download(
appVersions: List<String>?, appVersions: List<String>?,
onStatus: (DownloadStatus) -> Unit onStatus: (DownloadStatus) -> Unit
) { ) {
absoluteVersion = getLatestOrProvidedAppVersion(version, appVersions) absoluteVersion = getLatestOrProvidedAppVersion(musicVersionPref, appVersions)
downloadFiles( downloadFiles(
downloadFiles = arrayOf(DownloadFile( downloadFiles = arrayOf(DownloadFile(
call = musicAPI.getFiles( call = musicAPI.getFiles(
version = absoluteVersion, version = absoluteVersion,
variant = variant, variant = managerVariantPref,
), ),
fileName = "music.apk" fileName = "music.apk"
)), )),
@ -53,7 +50,7 @@ class MusicDownloader(
override fun getSavedFilePath(): String { override fun getSavedFilePath(): String {
val directory = val directory =
File(context.getExternalFilesDir("vancedmusic")!!.path + "$absoluteVersion/$variant") File(context.getExternalFilesDir("vancedmusic")!!.path + "$absoluteVersion/$managerVariantPref")
if (!directory.exists()) if (!directory.exists())
directory.mkdirs() directory.mkdirs()

View File

@ -17,29 +17,24 @@ class VancedDownloader(
private val context: Context, private val context: Context,
) : AppDownloader() { ) : AppDownloader() {
private val theme by vancedThemePref
private val version by vancedVersionPref
private val variant by managerVariantPref
private val languages by vancedLanguagesPref
private lateinit var absoluteVersion: String private lateinit var absoluteVersion: String
override suspend fun download( override suspend fun download(
appVersions: List<String>?, appVersions: List<String>?,
onStatus: (DownloadStatus) -> Unit onStatus: (DownloadStatus) -> Unit
) { ) {
absoluteVersion = getLatestOrProvidedAppVersion(version, appVersions) absoluteVersion = getLatestOrProvidedAppVersion(vancedVersionPref, appVersions)
val files = arrayOf( val files = arrayOf(
getFile( getFile(
type = "Theme", type = "Theme",
apkName = "$theme.apk", apkName = "$vancedThemePref.apk",
), ),
getFile( getFile(
type = "Arch", type = "Arch",
apkName = "split_config.$arch.apk", apkName = "split_config.$arch.apk",
) )
) + languages.map { language -> ) + vancedLanguagesPref.map { language ->
getFile( getFile(
type = "Language", type = "Language",
apkName = "split_config.$language.apk", apkName = "split_config.$language.apk",
@ -68,7 +63,7 @@ class VancedDownloader(
override fun getSavedFilePath(): String { override fun getSavedFilePath(): String {
val directory = val directory =
File(context.getExternalFilesDir("vanced")!!.path + "/$absoluteVersion/$variant") File(context.getExternalFilesDir("vanced")!!.path + "/$absoluteVersion/$managerVariantPref")
if (!directory.exists()) if (!directory.exists())
directory.mkdirs() directory.mkdirs()
@ -82,7 +77,7 @@ class VancedDownloader(
) = DownloadFile( ) = DownloadFile(
call = vancedAPI.getFiles( call = vancedAPI.getFiles(
version = absoluteVersion, version = absoluteVersion,
variant = variant, variant = managerVariantPref,
type = type, type = type,
apkName = apkName apkName = apkName
), ),

View File

@ -12,10 +12,9 @@ class MusicInstaller(
) : AppInstaller() { ) : AppInstaller() {
override fun install(appVersions: List<String>?) { override fun install(appVersions: List<String>?) {
val version by musicVersionPref val musicApk = File(
val variant by managerVariantPref context.getExternalFilesDir("music/$musicVersionPref/$managerVariantPref/music.apk")!!.path
)
val musicApk = File(context.getExternalFilesDir("music/$version/$variant/music.apk")!!.path)
installApp(musicApk, context) installApp(musicApk, context)
} }

View File

@ -12,13 +12,10 @@ class VancedInstaller(
) : AppInstaller() { ) : AppInstaller() {
override fun install(appVersions: List<String>?) { override fun install(appVersions: List<String>?) {
val version by vancedVersionPref val absoluteVersion = getLatestOrProvidedAppVersion(vancedVersionPref, appVersions)
val variant by managerVariantPref
val absoluteVersion = getLatestOrProvidedAppVersion(version, appVersions)
val apks = context val apks = context
.getExternalFilesDir("vanced/$absoluteVersion/$variant")!! .getExternalFilesDir("vanced/$absoluteVersion/$managerVariantPref")!!
.listFiles { file -> .listFiles { file ->
file.extension == "apk" file.extension == "apk"
} }

View File

@ -6,18 +6,17 @@ import com.vanced.manager.core.preferences.managerStringPreference
import com.vanced.manager.core.preferences.managerStringSetPreference import com.vanced.manager.core.preferences.managerStringSetPreference
import com.vanced.manager.ui.theme.defAccentColor import com.vanced.manager.ui.theme.defAccentColor
val useCustomTabsPref = managerBooleanPreference(USE_CUSTOM_TABS_KEY) var useCustomTabsPref by managerBooleanPreference(USE_CUSTOM_TABS_KEY)
val managerVariantPref = managerStringPreference(MANAGER_VARIANT_KEY, MANAGER_VARIANT_DEFAULT_VALUE) var managerVariantPref by managerStringPreference(MANAGER_VARIANT_KEY, MANAGER_VARIANT_DEFAULT_VALUE)
val managerThemePref = managerStringPreference(MANAGER_THEME_KEY, MANAGER_THEME_DEFAULT_VALUE) var managerThemePref by managerStringPreference(MANAGER_THEME_KEY, MANAGER_THEME_DEFAULT_VALUE)
val managerAccentColorPref = managerLongPreference(MANAGER_ACCENT_COLOR_KEY, defAccentColor) var managerAccentColorPref by managerLongPreference(MANAGER_ACCENT_COLOR_KEY, defAccentColor)
val vancedThemePref = managerStringPreference(APP_VANCED_THEME_KEY, VANCED_THEME_DEFAULT_VALUE) var vancedThemePref by managerStringPreference(APP_VANCED_THEME_KEY, VANCED_THEME_DEFAULT_VALUE)
val vancedVersionPref = managerStringPreference(APP_VANCED_VERSION_KEY, APP_VERSION_DEFAULT_VALUE) var vancedVersionPref by managerStringPreference(APP_VANCED_VERSION_KEY, APP_VERSION_DEFAULT_VALUE)
val vancedLanguagesPref = var vancedLanguagesPref by managerStringSetPreference(APP_VANCED_LANGUAGE_KEY, VANCED_LANGUAGE_DEFAULT_VALUE)
managerStringSetPreference(APP_VANCED_LANGUAGE_KEY, VANCED_LANGUAGE_DEFAULT_VALUE)
val musicVersionPref = managerStringPreference(APP_MUSIC_VERSION_KEY, APP_VERSION_DEFAULT_VALUE) var musicVersionPref by managerStringPreference(APP_MUSIC_VERSION_KEY, APP_VERSION_DEFAULT_VALUE)
val vancedEnabled = managerBooleanPreference(VANCED_ENABLED_KEY, APP_ENABLED_DEFAULT_VALUE) var vancedEnabled by managerBooleanPreference(VANCED_ENABLED_KEY, APP_ENABLED_DEFAULT_VALUE)
val musicEnabled = managerBooleanPreference(MUSIC_ENABLED_KEY, APP_ENABLED_DEFAULT_VALUE) var musicEnabled by managerBooleanPreference(MUSIC_ENABLED_KEY, APP_ENABLED_DEFAULT_VALUE)

View File

@ -77,9 +77,9 @@ class AppDtoMapper(
VANCED_NAME -> listOf( VANCED_NAME -> listOf(
InstallationOption.SingleSelect( InstallationOption.SingleSelect(
titleId = R.string.app_installation_options_theme, titleId = R.string.app_installation_options_theme,
getOption = { vancedThemePref.value.value }, getOption = { vancedThemePref },
setOption = { setOption = {
vancedThemePref.save(it) vancedThemePref = it
}, },
items = appThemes?.map { theme -> items = appThemes?.map { theme ->
InstallationOptionItem( InstallationOptionItem(
@ -94,9 +94,9 @@ class AppDtoMapper(
), ),
InstallationOption.SingleSelect( InstallationOption.SingleSelect(
titleId = R.string.app_installation_options_version, titleId = R.string.app_installation_options_version,
getOption = { vancedVersionPref.value.value }, getOption = { vancedVersionPref },
setOption = { setOption = {
vancedVersionPref.save(it) vancedVersionPref = it
}, },
items = appVersions?.map { version -> items = appVersions?.map { version ->
InstallationOptionItem( InstallationOptionItem(
@ -107,12 +107,12 @@ class AppDtoMapper(
), ),
InstallationOption.MultiSelect( InstallationOption.MultiSelect(
titleId = R.string.app_installation_options_language, titleId = R.string.app_installation_options_language,
getOption = { vancedLanguagesPref.value.value }, getOption = { vancedLanguagesPref },
addOption = { addOption = {
vancedLanguagesPref.save(vancedLanguagesPref.value.value + it) vancedLanguagesPref = vancedLanguagesPref + it
}, },
removeOption = { removeOption = {
vancedLanguagesPref.save(vancedLanguagesPref.value.value - it) vancedLanguagesPref = vancedLanguagesPref - it
}, },
items = appLanguages?.map { language -> items = appLanguages?.map { language ->
InstallationOptionItem( InstallationOptionItem(
@ -128,9 +128,9 @@ class AppDtoMapper(
MUSIC_NAME -> listOf( MUSIC_NAME -> listOf(
InstallationOption.SingleSelect( InstallationOption.SingleSelect(
titleId = R.string.app_installation_options_version, titleId = R.string.app_installation_options_version,
getOption = { musicVersionPref.value.value }, getOption = { musicVersionPref },
setOption = { setOption = {
musicVersionPref.save(it) musicVersionPref = it
}, },
items = appVersions?.map { version -> items = appVersions?.map { version ->
InstallationOptionItem( InstallationOptionItem(

View File

@ -19,12 +19,11 @@ fun ManagerLinkCard(
val customTabs = remember { CustomTabsIntent.Builder().build() } val customTabs = remember { CustomTabsIntent.Builder().build() }
val uri = remember { Uri.parse(link) } val uri = remember { Uri.parse(link) }
val intent = remember { Intent(Intent.ACTION_VIEW, uri) } val intent = remember { Intent(Intent.ACTION_VIEW, uri) }
val useCustomTabs by useCustomTabsPref
ManagerItemCard( ManagerItemCard(
title = title, title = title,
icon = icon icon = icon
) { ) {
if (useCustomTabs) { if (useCustomTabsPref) {
customTabs.launchUrl(context, uri) customTabs.launchUrl(context, uri)
} else { } else {
context.startActivity(intent) context.startActivity(intent)

View File

@ -17,8 +17,7 @@ fun contentColorForColor(color: Color) =
@Composable @Composable
fun managerAccentColor(): Color { fun managerAccentColor(): Color {
val accentColor by managerAccentColorPref return Color(managerAccentColorPref)
return Color(accentColor)
} }
@Composable @Composable

View File

@ -11,18 +11,13 @@ import kotlinx.coroutines.launch
fun CheckboxPreference( fun CheckboxPreference(
preferenceTitle: String, preferenceTitle: String,
preferenceDescription: String? = null, preferenceDescription: String? = null,
preference: ManagerPreference<Boolean>, isChecked: Boolean,
onCheckedChange: (isChecked: Boolean) -> Unit = {} onCheckedChange: (isChecked: Boolean) -> Unit = {}
) { ) {
var isChecked by preference
val coroutineScope = rememberCoroutineScope()
val onClick: () -> Unit = { val onClick: () -> Unit = {
coroutineScope.launch { onCheckedChange(!isChecked)
isChecked = !isChecked
onCheckedChange(isChecked)
}
} }
Preference( Preference(
preferenceTitle = preferenceTitle, preferenceTitle = preferenceTitle,
preferenceDescription = preferenceDescription, preferenceDescription = preferenceDescription,

View File

@ -72,7 +72,7 @@ private val DarkThemeColors = darkColorScheme(
) )
@Composable @Composable
fun isDark(): Boolean = when (managerThemePref.value.value) { fun isDark(): Boolean = when (managerThemePref) {
"Dark" -> true "Dark" -> true
"Light" -> false "Light" -> false
"System Default" -> isSystemInDarkTheme() "System Default" -> isSystemInDarkTheme()

View File

@ -32,9 +32,9 @@ class MainViewModel(
fun fetch() { fun fetch() {
viewModelScope.launch { viewModelScope.launch {
val vancedEnabled = vancedEnabled.value.value val vancedEnabled = vancedEnabled
val musicEnabled = musicEnabled.value.value val musicEnabled = musicEnabled
val isNonroot = managerVariantPref.value.value == "nonroot" val isNonroot = managerVariantPref == "nonroot"
var appsCount = 0 var appsCount = 0

View File

@ -11,6 +11,9 @@ fun SettingsCustomTabsItem() {
CheckboxPreference( CheckboxPreference(
preferenceTitle = stringResource(id = R.string.settings_preference_use_custom_tabs_title), preferenceTitle = stringResource(id = R.string.settings_preference_use_custom_tabs_title),
preferenceDescription = stringResource(id = R.string.settings_preference_use_custom_tabs_summary), preferenceDescription = stringResource(id = R.string.settings_preference_use_custom_tabs_summary),
preference = useCustomTabsPref isChecked = useCustomTabsPref,
onCheckedChange = {
useCustomTabsPref = it
}
) )
} }

View File

@ -10,12 +10,12 @@ import com.vanced.manager.ui.resources.managerString
@Composable @Composable
fun SettingsManagerVariantItem() { fun SettingsManagerVariantItem() {
var showDialog by remember { mutableStateOf(false) } var showDialog by remember { mutableStateOf(false) }
var selectedKey by remember { mutableStateOf(managerVariantPref.value.value) } var selectedKey by remember { mutableStateOf(managerVariantPref) }
RadiobuttonDialogPreference( RadiobuttonDialogPreference(
preferenceTitle = managerString( preferenceTitle = managerString(
stringId = R.string.settings_preference_variant_title stringId = R.string.settings_preference_variant_title
), ),
preferenceDescription = managerVariantPref.value.value, preferenceDescription = managerVariantPref,
isDialogVisible = showDialog, isDialogVisible = showDialog,
currentSelectedKey = selectedKey, currentSelectedKey = selectedKey,
buttons = listOf( buttons = listOf(
@ -33,13 +33,13 @@ fun SettingsManagerVariantItem() {
}, },
onDismissRequest = { onDismissRequest = {
showDialog = false showDialog = false
selectedKey = managerVariantPref.value.value selectedKey = managerVariantPref
}, },
onItemClick = { onItemClick = {
selectedKey = it selectedKey = it
}, },
onSave = { onSave = {
managerVariantPref.save(selectedKey) managerVariantPref = selectedKey
showDialog = false showDialog = false
} }
) )

View File

@ -7,15 +7,19 @@ import com.vanced.manager.ui.component.preference.CheckboxPreference
@Composable @Composable
fun SettingsNotificationsItem() { fun SettingsNotificationsItem() {
notificationApps.forEach { notificationApps.forEach { notificationApp ->
with(it) { with(notificationApp) {
var appNotificationsPref by managerBooleanPreference(
key = "${prefKey}_notifications",
defaultValue = true
)
CheckboxPreference( CheckboxPreference(
preferenceTitle = "$app Push Notifications", preferenceTitle = "$app Push Notifications",
preferenceDescription = "Receive push notifications when an update for $app is released", preferenceDescription = "Receive push notifications when an update for $app is released",
preference = managerBooleanPreference( isChecked = appNotificationsPref,
key = "${prefKey}_notifications", onCheckedChange = {
defaultValue = true appNotificationsPref = it
) }
) )
} }
} }

View File

@ -10,10 +10,10 @@ import com.vanced.manager.ui.resources.managerString
@Composable @Composable
fun ThemeSettingsItem() { fun ThemeSettingsItem() {
var showDialog by remember { mutableStateOf(false) } var showDialog by remember { mutableStateOf(false) }
var selectedKey by remember { mutableStateOf(managerThemePref.value.value) } var selectedKey by remember { mutableStateOf(managerThemePref) }
RadiobuttonDialogPreference( RadiobuttonDialogPreference(
preferenceTitle = managerString(stringId = R.string.settings_preference_theme_title), preferenceTitle = managerString(stringId = R.string.settings_preference_theme_title),
preferenceDescription = managerThemePref.value.value, preferenceDescription = managerThemePref,
isDialogVisible = showDialog, isDialogVisible = showDialog,
currentSelectedKey = selectedKey, currentSelectedKey = selectedKey,
buttons = listOf( buttons = listOf(
@ -35,13 +35,13 @@ fun ThemeSettingsItem() {
}, },
onDismissRequest = { onDismissRequest = {
showDialog = false showDialog = false
selectedKey = managerThemePref.value.value selectedKey = managerThemePref
}, },
onItemClick = { onItemClick = {
selectedKey = it selectedKey = it
}, },
onSave = { onSave = {
managerThemePref.save(selectedKey) managerThemePref = selectedKey
showDialog = false showDialog = false
} }
) )