package it.vfsfitvnm.vimusic.utils import android.content.Context import android.content.SharedPreferences import androidx.compose.runtime.Composable import androidx.compose.runtime.MutableState import androidx.compose.runtime.SnapshotMutationPolicy import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.platform.LocalContext import androidx.core.content.edit const val colorPaletteNameKey = "colorPaletteName" const val colorPaletteModeKey = "colorPaletteMode" const val thumbnailRoundnessKey = "thumbnailRoundness" const val coilDiskCacheMaxSizeKey = "coilDiskCacheMaxSize" const val exoPlayerDiskCacheMaxSizeKey = "exoPlayerDiskCacheMaxSize" const val isInvincibilityEnabledKey = "isInvincibilityEnabled" const val isFirstLaunchKey = "isFirstLaunch" const val songSortOrderKey = "songSortOrder" const val songSortByKey = "songSortBy" const val playlistSortOrderKey = "playlistSortOrder" const val playlistSortByKey = "playlistSortBy" const val albumSortOrderKey = "albumSortOrder" const val albumSortByKey = "albumSortBy" const val artistSortOrderKey = "artistSortOrder" const val artistSortByKey = "artistSortBy" const val repeatModeKey = "repeatMode" const val skipSilenceKey = "skipSilence" const val volumeNormalizationKey = "volumeNormalization" const val persistentQueueKey = "persistentQueue" const val isShowingSynchronizedLyricsKey = "isShowingSynchronizedLyrics" const val isShowingThumbnailInLockscreenKey = "isShowingThumbnailInLockscreen" const val homeScreenTabIndexKey = "homeScreenTabIndex" const val searchResultScreenTabIndexKey = "searchResultScreenTabIndex" const val artistScreenTabIndexKey = "artistScreenTabIndex" const val pauseSearchHistoryKey = "pauseSearchHistory" inline fun > SharedPreferences.getEnum( key: String, defaultValue: T ): T = getString(key, null)?.let { try { enumValueOf(it) } catch (e: IllegalArgumentException) { null } } ?: defaultValue inline fun > SharedPreferences.Editor.putEnum( key: String, value: T ): SharedPreferences.Editor = putString(key, value.name) val Context.preferences: SharedPreferences get() = getSharedPreferences("preferences", Context.MODE_PRIVATE) @Composable fun rememberPreference(key: String, defaultValue: Boolean): MutableState { val context = LocalContext.current return remember { mutableStatePreferenceOf(context.preferences.getBoolean(key, defaultValue)) { context.preferences.edit { putBoolean(key, it) } } } } @Composable fun rememberPreference(key: String, defaultValue: Int): MutableState { val context = LocalContext.current return remember { mutableStatePreferenceOf(context.preferences.getInt(key, defaultValue)) { context.preferences.edit { putInt(key, it) } } } } @Composable fun rememberPreference(key: String, defaultValue: String): MutableState { val context = LocalContext.current return remember { mutableStatePreferenceOf(context.preferences.getString(key, null) ?: defaultValue) { context.preferences.edit { putString(key, it) } } } } @Composable inline fun > rememberPreference(key: String, defaultValue: T): MutableState { val context = LocalContext.current return remember { mutableStatePreferenceOf(context.preferences.getEnum(key, defaultValue)) { context.preferences.edit { putEnum(key, it) } } } } inline fun mutableStatePreferenceOf( value: T, crossinline onStructuralInequality: (newValue: T) -> Unit ) = mutableStateOf( value = value, policy = object : SnapshotMutationPolicy { override fun equivalent(a: T, b: T): Boolean { val areEquals = a == b if (!areEquals) onStructuralInequality(b) return areEquals } })