Skip to content

Commit

Permalink
chore/Refactor usage of flow for settings viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthonyy232 committed Nov 4, 2024
1 parent 68e6544 commit 11d72a2
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 701 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.anthonyla.paperize.data.settings

import kotlinx.coroutines.flow.Flow

/**
* Interface for the data store that handles the settings for application
*/
Expand All @@ -10,6 +12,9 @@ interface SettingsDataStore {
suspend fun getBoolean(key: String): Boolean?
suspend fun getString(key: String): String?
suspend fun getInt(key: String): Int?
fun getBooleanFlow(key: String): Flow<Boolean?>
fun getStringFlow(key: String): Flow<String?>
fun getIntFlow(key: String): Flow<Int?>
suspend fun deleteBoolean(key: String)
suspend fun deleteString(key: String)
suspend fun deleteInt(key: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.emptyPreferences
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.anthonyla.paperize.core.SettingsConstants.SETTINGS_DATASTORE
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = SETTINGS_DATASTORE)

Expand All @@ -28,9 +33,9 @@ class SettingsDataStoreImpl(private val context: Context) : SettingsDataStore {
}

override suspend fun putInt(key: String, value: Int) {
val preferencesKey = stringPreferencesKey(key)
val preferencesKey = intPreferencesKey(key)
context.dataStore.edit {
it[preferencesKey] = value.toString()
it[preferencesKey] = value
}
}

Expand All @@ -54,14 +59,41 @@ class SettingsDataStoreImpl(private val context: Context) : SettingsDataStore {
override suspend fun getInt(key: String): Int? {
return try {
val head = context.dataStore.data.first()
val preferencesKey = stringPreferencesKey(key)
val preferencesKey = intPreferencesKey(key)
head[preferencesKey]?.toInt()
} catch (exception: Exception) {
exception.printStackTrace()
null
}
}

override fun getBooleanFlow(key: String): Flow<Boolean?> = context.dataStore.data
.catch { exception ->
exception.printStackTrace()
emit(emptyPreferences())
}
.map { preferences ->
preferences[booleanPreferencesKey(key)]
}

override fun getStringFlow(key: String): Flow<String?> = context.dataStore.data
.catch { exception ->
exception.printStackTrace()
emit(emptyPreferences())
}
.map { preferences ->
preferences[stringPreferencesKey(key)]
}

override fun getIntFlow(key: String): Flow<Int?> = context.dataStore.data
.catch { exception ->
exception.printStackTrace()
emit(emptyPreferences())
}
.map { preferences ->
preferences[intPreferencesKey(key)]
}

override suspend fun deleteBoolean(key: String) {
val preferencesKey = booleanPreferencesKey(key)
context.dataStore.edit {
Expand All @@ -81,7 +113,7 @@ class SettingsDataStoreImpl(private val context: Context) : SettingsDataStore {
}

override suspend fun deleteInt(key: String) {
val preferencesKey = stringPreferencesKey(key)
val preferencesKey = intPreferencesKey(key)
context.dataStore.edit {
if (it.contains(preferencesKey)) {
it.remove(preferencesKey)
Expand All @@ -104,6 +136,9 @@ class SettingsDataStoreImpl(private val context: Context) : SettingsDataStore {
else if (preferences.contains(stringPreferencesKey(key))) {
preferences.remove(stringPreferencesKey(key))
}
else if (preferences.contains(intPreferencesKey(key))) {
preferences.remove(intPreferencesKey(key))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ class MainActivity : ComponentActivity() {

if (shouldScheduleAlarm) {
scheduleWallpaperAlarm(settings, scheduler)
updateCurrentWallpapers(settings)
}
}

Expand Down Expand Up @@ -174,54 +173,6 @@ class MainActivity : ComponentActivity() {
settingsViewModel.onEvent(SettingsEvent.RefreshNextSetTime)
}

private fun updateCurrentWallpapers(settings: SettingsState) {
val selectedState = wallpaperScreenViewModel.state.value
val currentHomeAlbum = selectedState.selectedAlbum?.find {
it.album.initialAlbumName == settings.wallpaperSettings.homeAlbumName
}
val currentLockAlbum = selectedState.selectedAlbum?.find {
it.album.initialAlbumName == settings.wallpaperSettings.lockAlbumName
}

val wallpaperSettings = settings.wallpaperSettings
val scheduleSettings = settings.scheduleSettings

when {
scheduleSettings.scheduleSeparately && wallpaperSettings.setHomeWallpaper && wallpaperSettings.setLockWallpaper -> {
if (currentHomeAlbum != null && currentLockAlbum != null) {
settingsViewModel.onEvent(SettingsEvent.SetCurrentWallpaper(
currentHomeWallpaper = currentHomeAlbum.album.homeWallpapersInQueue.firstOrNull() ?: currentHomeAlbum.wallpapers.firstOrNull()?.wallpaperUri,
currentLockWallpaper = currentLockAlbum.album.lockWallpapersInQueue.firstOrNull() ?: currentLockAlbum.wallpapers.firstOrNull()?.wallpaperUri
))
}
}
!scheduleSettings.scheduleSeparately && wallpaperSettings.setHomeWallpaper && wallpaperSettings.setLockWallpaper -> {
if (currentHomeAlbum != null && currentLockAlbum != null) {
settingsViewModel.onEvent(SettingsEvent.SetCurrentWallpaper(
currentHomeWallpaper = currentHomeAlbum.album.homeWallpapersInQueue.firstOrNull() ?: currentHomeAlbum.wallpapers.firstOrNull()?.wallpaperUri,
currentLockWallpaper = currentHomeAlbum.album.homeWallpapersInQueue.firstOrNull() ?: currentHomeAlbum.wallpapers.firstOrNull()?.wallpaperUri
))
}
}
wallpaperSettings.setHomeWallpaper -> {
if (currentHomeAlbum != null) {
settingsViewModel.onEvent(SettingsEvent.SetCurrentWallpaper(
currentHomeWallpaper = currentHomeAlbum.album.homeWallpapersInQueue.firstOrNull() ?: currentHomeAlbum.wallpapers.firstOrNull()?.wallpaperUri,
currentLockWallpaper = if (scheduleSettings.scheduleSeparately) null else currentHomeAlbum.album.homeWallpapersInQueue.firstOrNull() ?: currentHomeAlbum.wallpapers.firstOrNull()?.wallpaperUri
))
}
}
wallpaperSettings.setLockWallpaper -> {
if (currentLockAlbum != null) {
settingsViewModel.onEvent(SettingsEvent.SetCurrentWallpaper(
currentHomeWallpaper = if (scheduleSettings.scheduleSeparately) null else currentLockAlbum.album.lockWallpapersInQueue.firstOrNull() ?: currentLockAlbum.wallpapers.firstOrNull()?.wallpaperUri,
currentLockWallpaper = currentLockAlbum.album.lockWallpapersInQueue.firstOrNull() ?: currentLockAlbum.wallpapers.firstOrNull()?.wallpaperUri
))
}
}
}
}

override fun onResume() {
super.onResume()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
Expand Down
Loading

0 comments on commit 11d72a2

Please sign in to comment.