Skip to content

Instantly share code, notes, and snippets.

@lighttigerXIV
Last active December 1, 2024 16:45
Show Gist options
  • Save lighttigerXIV/46a3e9f23c6e4863984c9c0cacff978f to your computer and use it in GitHub Desktop.
Save lighttigerXIV/46a3e9f23c6e4863984c9c0cacff978f to your computer and use it in GitHub Desktop.
Settings With Datastore
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
@Inject
lateinit var settingsRepository: SettingsRepository
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
val settings = settingsRepository.settings.collectAsState().value
val hasInitiated = settingsRepository.hasInitiated.collectAsState().value
if (hasInitiated) {
PurrTheme(
settings = settings
) {
val rootNavController = rememberNavController()
NavHost(
modifier = Modifier
.background(MaterialTheme.colorScheme.background)
.padding(16.dp),
navController = rootNavController,
startDestination = if (settings.setupCompleted) AppDestination.Main.Root else AppDestination.Setup.Root
) {
composable<AppDestination.Setup.Root> {
SetupScreen(rootNavController)
}
composable<AppDestination.Main.Root> {
MainScreenRoot(rootNavController)
}
composable<AppDestination.Settings.Root> {
}
composable<AppDestination.Album> {
val args = it.toRoute<AppDestination.Album>()
}
composable<AppDestination.Artist> {
val args = it.toRoute<AppDestination.Artist>()
}
composable<AppDestination.Playlist> {
val args = it.toRoute<AppDestination.Playlist>()
}
}
}
}
}
}
}
package com.whiskersapps.purr.features.settings.domain
import android.app.Application
import android.content.Context
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
private val Context.dataStore by preferencesDataStore("settings")
class SettingsRepository(
app: Application
) {
private val dataStore = app.dataStore
private val _settings = MutableStateFlow(AppSettings())
val settings = _settings.asStateFlow()
private val _hasInitiated = MutableStateFlow(false)
val hasInitiated = _hasInitiated.asStateFlow()
private val _settingsFlow: Flow<AppSettings> = dataStore.data.catch {
AppSettings()
}.map { preferences ->
val newSettings = AppSettings(
colorScheme = preferences[AppSettingsKeys.COLOR_SCHEME]
?: AppSettingsDefaults.COLOR_SCHEME,
darkTheme = preferences[AppSettingsKeys.DARK_THEME] ?: AppSettingsDefaults.DARK_THEME,
lightTheme = preferences[AppSettingsKeys.LIGHT_THEME]
?: AppSettingsDefaults.LIGHT_THEME,
audioFilter = preferences[AppSettingsKeys.AUDIO_FILTER]
?: AppSettingsDefaults.AUDIO_FILTER,
blacklist = Json.decodeFromString(
preferences[AppSettingsKeys.BLACKLIST] ?: AppSettingsDefaults.BLACKLIST
),
keepScreenOnCarPlayer = preferences[AppSettingsKeys.KEEP_SCREEN_ON_CAR_PLAYER]
?: AppSettingsDefaults.KEEP_SCREEN_ON_CAR_PLAYER,
hidePodcasts = preferences[AppSettingsKeys.HIDE_PODCASTS]
?: AppSettingsDefaults.HIDE_PODCASTS,
setupCompleted = preferences[AppSettingsKeys.SETUP_COMPLETED]
?: AppSettingsDefaults.SETUP_COMPLETED,
viewType = preferences[AppSettingsKeys.VIEW_TYPE] ?: AppSettingsDefaults.VIEW_TYPE
)
newSettings
}
init {
CoroutineScope(Dispatchers.Default).launch {
_settingsFlow.collect { settings ->
_settings.update { settings }
_hasInitiated.update { true }
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Setters
////////////////////////////////////////////////////////////////////////////////////////////////
suspend fun setSetupCompleted(completed: Boolean) {
dataStore.edit { it[AppSettingsKeys.SETUP_COMPLETED] = completed }
}
suspend fun setColorScheme(colorScheme: String) {
dataStore.edit { it[AppSettingsKeys.COLOR_SCHEME] = colorScheme }
}
suspend fun setDarkTheme(themeId: String) {
dataStore.edit { it[AppSettingsKeys.DARK_THEME] = themeId }
}
suspend fun setLightTheme(themeId: String) {
dataStore.edit { it[AppSettingsKeys.LIGHT_THEME] = themeId }
}
suspend fun setAudioFilter(filter: Int) {
dataStore.edit { it[AppSettingsKeys.AUDIO_FILTER] = filter }
}
suspend fun setBlacklist(blacklist: List<String>) {
dataStore.edit { it[AppSettingsKeys.BLACKLIST] = Json.encodeToString(blacklist) }
}
suspend fun setKeepScreenOnCarPlayer(keepOpen: Boolean) {
dataStore.edit { it[AppSettingsKeys.KEEP_SCREEN_ON_CAR_PLAYER] = keepOpen }
}
suspend fun setHidePodcasts(hide: Boolean) {
dataStore.edit { it[AppSettingsKeys.HIDE_PODCASTS] = hide }
}
suspend fun setViewType(viewType: String){
dataStore.edit { it[AppSettingsKeys.VIEW_TYPE] = viewType }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment