0
0
Fork 0
mirror of https://github.com/YTVanced/VancedManager synced 2024-11-26 05:05:15 +00:00

switched back to AndroidX preferences

This commit is contained in:
X1nto 2021-06-27 13:28:18 +04:00
parent 19b93febcb
commit bb237d754d
7 changed files with 88 additions and 69 deletions

View file

@ -101,6 +101,8 @@ dependencies {
implementation("androidx.compose.ui:ui-util:$composeVersion") implementation("androidx.compose.ui:ui-util:$composeVersion")
implementation("androidx.compose.ui:ui:$composeVersion") implementation("androidx.compose.ui:ui:$composeVersion")
implementation("androidx.preference:preference-ktx:1.1.1")
implementation("androidx.activity:activity-compose:1.3.0-beta02") implementation("androidx.activity:activity-compose:1.3.0-beta02")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.4.0-alpha02") implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.4.0-alpha02")
implementation("androidx.navigation:navigation-compose:2.4.0-alpha03") implementation("androidx.navigation:navigation-compose:2.4.0-alpha03")
@ -112,8 +114,6 @@ dependencies {
implementation("com.github.skydoves:orchestra-colorpicker:$orchestraVersion") implementation("com.github.skydoves:orchestra-colorpicker:$orchestraVersion")
implementation("androidx.datastore:datastore-preferences:1.0.0-beta02")
implementation("io.insert-koin:koin-android:$koinVersion") implementation("io.insert-koin:koin-android:$koinVersion")
implementation("io.insert-koin:koin-androidx-compose:$koinVersion") implementation("io.insert-koin:koin-androidx-compose:$koinVersion")

View file

@ -1,15 +1,12 @@
package com.vanced.manager.di package com.vanced.manager.di
import android.content.Context import android.content.Context
import androidx.datastore.preferences.preferencesDataStore
import org.koin.dsl.module import org.koin.dsl.module
val Context.dataStore by preferencesDataStore("manager_settings")
val preferenceModule = module { val preferenceModule = module {
fun provideDatastore( fun provideDatastore(
context: Context context: Context
) = context.dataStore ) = context.getSharedPreferences("manager_settings", Context.MODE_PRIVATE)
single { provideDatastore(get()) } single { provideDatastore(get()) }
} }

View file

@ -3,9 +3,7 @@ package com.vanced.manager.ui.layouts
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.datastore.preferences.core.booleanPreferencesKey
import com.vanced.manager.R import com.vanced.manager.R
import com.vanced.manager.ui.components.*
import com.vanced.manager.ui.components.layout.ManagerScrollableColumn import com.vanced.manager.ui.components.layout.ManagerScrollableColumn
import com.vanced.manager.ui.components.preference.CheckboxPreference import com.vanced.manager.ui.components.preference.CheckboxPreference
import com.vanced.manager.ui.components.preference.Preference import com.vanced.manager.ui.components.preference.Preference
@ -62,7 +60,7 @@ fun SettingsLayout() {
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( preference = managerBooleanPreference(
key = booleanPreferencesKey("${prefKey}_notifications"), key = "${prefKey}_notifications",
defaultValue = true defaultValue = true
) )
) )

View file

@ -1,23 +1,23 @@
package com.vanced.manager.ui.preferences package com.vanced.manager.ui.preferences
import android.content.SharedPreferences
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.State import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.datastore.core.DataStore import androidx.core.content.edit
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import kotlinx.coroutines.flow.map
import org.koin.core.component.KoinComponent import org.koin.core.component.KoinComponent
import org.koin.core.component.inject import org.koin.core.component.inject
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
class ManagerPreference<T>( class ManagerPreference<T>(
val key: Preferences.Key<T>, private val key: String,
val defaultValue: T, private val defaultValue: T,
private val getter: (SharedPreferences) -> T,
private val setter: (SharedPreferences.Editor, key: String, newValue: T) -> Unit
) : KoinComponent { ) : KoinComponent {
private val dataStore: DataStore<Preferences> by inject() private val sharedPreferences: SharedPreferences by inject()
private val _value = mutableStateOf(defaultValue) private val _value = mutableStateOf(defaultValue)
val value: State<T> = _value val value: State<T> = _value
@ -27,43 +27,83 @@ class ManagerPreference<T>(
operator fun getValue(thisRef: Any?, property: KProperty<*>) = value.value operator fun getValue(thisRef: Any?, property: KProperty<*>) = value.value
suspend fun save(newValue: T) { fun save(newValue: T) {
_value.value = newValue _value.value = newValue
dataStore.edit { sharedPreferences.edit {
it[key] = value.value setter(this, key, newValue)
} }
} }
//It's Chewsday innit - © Bri'ish ppl //It's Chewsday innit - © Bri'ish ppl
init { init {
dataStore.data.map { _value.value = getter(sharedPreferences)
_value.value = it[key] ?: defaultValue
}
} }
} }
fun <T> managerPreference(
key: String,
defaultValue: T,
getter: (SharedPreferences) -> T,
setter: (SharedPreferences.Editor, key: String, newValue: T) -> Unit
) = ManagerPreference(key, defaultValue, getter, setter)
fun managerStringPreference( fun managerStringPreference(
key: Preferences.Key<String>, key: String,
defaultValue: String = "" defaultValue: String = ""
) = ManagerPreference(key, defaultValue) ) = managerPreference(
key = key,
defaultValue = defaultValue,
getter = { sharedPreferences ->
SharedPreferences::getString.invoke(sharedPreferences, key, defaultValue) ?: defaultValue
},
setter = SharedPreferences.Editor::putString
)
fun managerStringSetPreference( fun managerStringSetPreference(
key: Preferences.Key<Set<String>>, key: String,
defaultValue: Set<String> = setOf() defaultValue: Set<String> = setOf()
) = ManagerPreference(key, defaultValue) ) = managerPreference(
key = key,
defaultValue = defaultValue,
getter = { sharedPreferences ->
SharedPreferences::getStringSet.invoke(sharedPreferences, key, defaultValue) ?: defaultValue
},
setter = SharedPreferences.Editor::putStringSet
)
fun managerBooleanPreference( fun managerBooleanPreference(
key: Preferences.Key<Boolean>, key: String,
defaultValue: Boolean = false defaultValue: Boolean = false
) = ManagerPreference(key, defaultValue) ) =managerPreference(
key = key,
defaultValue = defaultValue,
getter = { sharedPreferences ->
SharedPreferences::getBoolean.invoke(sharedPreferences, key, defaultValue)
},
setter = SharedPreferences.Editor::putBoolean
)
fun managerIntPreference( fun managerIntPreference(
key: Preferences.Key<Int>, key: String,
defaultValue: Int = 0 defaultValue: Int = 0
) = ManagerPreference(key, defaultValue) ) = managerPreference(
key = key,
defaultValue = defaultValue,
getter = { sharedPreferences ->
SharedPreferences::getInt.invoke(sharedPreferences, key, defaultValue)
},
setter = SharedPreferences.Editor::putInt
)
fun managerLongPreference( fun managerLongPreference(
key: Preferences.Key<Long>, key: String,
defaultValue: Long = 0 defaultValue: Long = 0
) = ManagerPreference(key, defaultValue) ) = managerPreference(
key = key,
defaultValue = defaultValue,
getter = { sharedPreferences ->
SharedPreferences::getLong.invoke(sharedPreferences, key, defaultValue)
},
setter = SharedPreferences.Editor::putLong
)

View file

@ -6,18 +6,18 @@ import com.vanced.manager.ui.preferences.managerStringPreference
import com.vanced.manager.ui.preferences.managerStringSetPreference import com.vanced.manager.ui.preferences.managerStringSetPreference
import com.vanced.manager.ui.theme.defAccentColor import com.vanced.manager.ui.theme.defAccentColor
val useCustomTabsPref = managerBooleanPreference(useCustomTabsKey) val useCustomTabsPref = managerBooleanPreference(USE_CUSTOM_TABS_KEY)
val managerVariantPref = managerStringPreference(managerVariantKey, MANAGER_VARIANT_DEFAULT_VALUE) val managerVariantPref = managerStringPreference(MANAGER_VARIANT_KEY, MANAGER_VARIANT_DEFAULT_VALUE)
val managerThemePref = managerStringPreference(managerThemeKey, "System Default") val managerThemePref = managerStringPreference(MANAGER_THEME_KEY, "System Default")
val managerAccentColorPref = managerLongPreference(managerAccentColorKey, defAccentColor) val managerAccentColorPref = managerLongPreference(MANAGER_ACCENT_COLOR_KEY, defAccentColor)
val vancedThemePref = managerStringPreference(vancedThemeKey, "Dark") val vancedThemePref = managerStringPreference(APP_VANCED_THEME_KEY, "Dark")
val vancedVersionPref = managerStringPreference(vancedVersionKey, "latest") val vancedVersionPref = managerStringPreference(APP_VANCED_VERSION_KEY, "latest")
val vancedLanguagesPref = managerStringSetPreference(vancedLanguageKey, setOf("en")) val vancedLanguagesPref = managerStringSetPreference(APP_VANCED_LANGUAGE_KEY, setOf("en"))
val musicVersionPref = managerStringPreference(musicVersionKey, "latest") val musicVersionPref = managerStringPreference(APP_MUSIC_VERSION_KEY, "latest")
val vancedEnabled = managerBooleanPreference(vancedEnabledKey, VANCED_ENABLED_DEFAULT_VALUE) val vancedEnabled = managerBooleanPreference(VANCED_ENABLED_KEY, VANCED_ENABLED_DEFAULT_VALUE)
val musicEnabled = managerBooleanPreference(musicEnabledKey, MUSIC_ENABLED_DEFAULT_VALUE) val musicEnabled = managerBooleanPreference(MUSIC_ENABLED_KEY, MUSIC_ENABLED_DEFAULT_VALUE)

View file

@ -1,19 +1,16 @@
package com.vanced.manager.ui.preferences.holder package com.vanced.manager.ui.preferences.holder
import androidx.datastore.preferences.core.booleanPreferencesKey const val USE_CUSTOM_TABS_KEY = "use_custom_tabs"
import androidx.datastore.preferences.core.longPreferencesKey const val MANAGER_VARIANT_KEY = "manager_variant"
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.core.stringSetPreferencesKey
val useCustomTabsKey = booleanPreferencesKey(USE_CUSTOM_TABS_KEY) const val MANAGER_THEME_KEY = "manager_theme"
val managerVariantKey = stringPreferencesKey(MANAGER_VARIANT_KEY) const val MANAGER_ACCENT_COLOR_KEY = "manager_accent_color"
val managerThemeKey = stringPreferencesKey("manager_theme")
val managerAccentColorKey = longPreferencesKey("manager_accent_color")
val vancedThemeKey = stringPreferencesKey(APP_VANCED_THEME_KEY) const val APP_VANCED_THEME_KEY = "app_vanced_theme"
val vancedVersionKey = stringPreferencesKey(APP_VANCED_VERSION_KEY) const val APP_VANCED_VERSION_KEY = "app_vanced_version"
val vancedLanguageKey = stringSetPreferencesKey(APP_VANCED_LANGUAGE_KEY) const val APP_VANCED_LANGUAGE_KEY = "app_vanced_language"
val musicVersionKey = stringPreferencesKey(APP_MUSIC_VERSION_KEY)
val vancedEnabledKey = booleanPreferencesKey(VANCED_ENABLED_KEY) const val APP_MUSIC_VERSION_KEY = "app_music_version"
val musicEnabledKey = booleanPreferencesKey(MUSIC_ENABLED_KEY)
const val VANCED_ENABLED_KEY = "manager_vanced_enabled"
const val MUSIC_ENABLED_KEY = "manager_music_enabled"

View file

@ -1,13 +0,0 @@
package com.vanced.manager.ui.preferences.holder
const val USE_CUSTOM_TABS_KEY = "use_custom_tabs"
const val MANAGER_VARIANT_KEY = "manager_variant"
const val APP_VANCED_THEME_KEY = "app_vanced_theme"
const val APP_VANCED_VERSION_KEY = "app_vanced_version"
const val APP_VANCED_LANGUAGE_KEY = "app_vanced_language"
const val APP_MUSIC_VERSION_KEY = "app_music_version"
const val VANCED_ENABLED_KEY = "manager_vanced_enabled"
const val MUSIC_ENABLED_KEY = "manager_music_enabled"