0
0
Fork 0
mirror of https://github.com/YTVanced/VancedManager synced 2024-11-29 14:33:01 +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:$composeVersion")
implementation("androidx.preference:preference-ktx:1.1.1")
implementation("androidx.activity:activity-compose:1.3.0-beta02")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.4.0-alpha02")
implementation("androidx.navigation:navigation-compose:2.4.0-alpha03")
@ -112,8 +114,6 @@ dependencies {
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-androidx-compose:$koinVersion")

View file

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

View file

@ -3,9 +3,7 @@ package com.vanced.manager.ui.layouts
import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.*
import androidx.compose.ui.unit.dp
import androidx.datastore.preferences.core.booleanPreferencesKey
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.preference.CheckboxPreference
import com.vanced.manager.ui.components.preference.Preference
@ -62,7 +60,7 @@ fun SettingsLayout() {
preferenceTitle = "$app Push Notifications",
preferenceDescription = "Receive push notifications when an update for $app is released",
preference = managerBooleanPreference(
key = booleanPreferencesKey("${prefKey}_notifications"),
key = "${prefKey}_notifications",
defaultValue = true
)
)

View file

@ -1,23 +1,23 @@
package com.vanced.manager.ui.preferences
import android.content.SharedPreferences
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import kotlinx.coroutines.flow.map
import androidx.core.content.edit
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import kotlin.reflect.KProperty
class ManagerPreference<T>(
val key: Preferences.Key<T>,
val defaultValue: T,
private val key: String,
private val defaultValue: T,
private val getter: (SharedPreferences) -> T,
private val setter: (SharedPreferences.Editor, key: String, newValue: T) -> Unit
) : KoinComponent {
private val dataStore: DataStore<Preferences> by inject()
private val sharedPreferences: SharedPreferences by inject()
private val _value = mutableStateOf(defaultValue)
val value: State<T> = _value
@ -27,43 +27,83 @@ class ManagerPreference<T>(
operator fun getValue(thisRef: Any?, property: KProperty<*>) = value.value
suspend fun save(newValue: T) {
fun save(newValue: T) {
_value.value = newValue
dataStore.edit {
it[key] = value.value
sharedPreferences.edit {
setter(this, key, newValue)
}
}
//It's Chewsday innit - © Bri'ish ppl
init {
dataStore.data.map {
_value.value = it[key] ?: defaultValue
}
_value.value = getter(sharedPreferences)
}
}
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(
key: Preferences.Key<String>,
key: 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(
key: Preferences.Key<Set<String>>,
key: String,
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(
key: Preferences.Key<Boolean>,
key: String,
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(
key: Preferences.Key<Int>,
key: String,
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(
key: Preferences.Key<Long>,
key: String,
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.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 managerAccentColorPref = managerLongPreference(managerAccentColorKey, defAccentColor)
val managerThemePref = managerStringPreference(MANAGER_THEME_KEY, "System Default")
val managerAccentColorPref = managerLongPreference(MANAGER_ACCENT_COLOR_KEY, defAccentColor)
val vancedThemePref = managerStringPreference(vancedThemeKey, "Dark")
val vancedVersionPref = managerStringPreference(vancedVersionKey, "latest")
val vancedLanguagesPref = managerStringSetPreference(vancedLanguageKey, setOf("en"))
val vancedThemePref = managerStringPreference(APP_VANCED_THEME_KEY, "Dark")
val vancedVersionPref = managerStringPreference(APP_VANCED_VERSION_KEY, "latest")
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 musicEnabled = managerBooleanPreference(musicEnabledKey, MUSIC_ENABLED_DEFAULT_VALUE)
val vancedEnabled = managerBooleanPreference(VANCED_ENABLED_KEY, VANCED_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
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.core.stringSetPreferencesKey
const val USE_CUSTOM_TABS_KEY = "use_custom_tabs"
const val MANAGER_VARIANT_KEY = "manager_variant"
val useCustomTabsKey = booleanPreferencesKey(USE_CUSTOM_TABS_KEY)
val managerVariantKey = stringPreferencesKey(MANAGER_VARIANT_KEY)
val managerThemeKey = stringPreferencesKey("manager_theme")
val managerAccentColorKey = longPreferencesKey("manager_accent_color")
const val MANAGER_THEME_KEY = "manager_theme"
const val MANAGER_ACCENT_COLOR_KEY = "manager_accent_color"
val vancedThemeKey = stringPreferencesKey(APP_VANCED_THEME_KEY)
val vancedVersionKey = stringPreferencesKey(APP_VANCED_VERSION_KEY)
val vancedLanguageKey = stringSetPreferencesKey(APP_VANCED_LANGUAGE_KEY)
val musicVersionKey = stringPreferencesKey(APP_MUSIC_VERSION_KEY)
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"
val vancedEnabledKey = booleanPreferencesKey(VANCED_ENABLED_KEY)
val musicEnabledKey = booleanPreferencesKey(MUSIC_ENABLED_KEY)
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"

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"