This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use DataStore to store user preferences
- Loading branch information
Showing
7 changed files
with
212 additions
and
6 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
app/src/main/kotlin/com/sanmer/geomag/datastore/UserData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.sanmer.geomag.datastore | ||
|
||
import androidx.compose.foundation.isSystemInDarkTheme | ||
import androidx.compose.runtime.Composable | ||
import com.sanmer.geomag.app.Const | ||
import com.sanmer.geomag.ui.theme.Colors | ||
|
||
data class UserData( | ||
val darkMode: DarkMode, | ||
val themeColor: Int, | ||
val enableNavigationAnimation: Boolean | ||
) { | ||
companion object { | ||
fun default() = UserData( | ||
darkMode = DarkMode.FOLLOW_SYSTEM, | ||
themeColor = if (Const.atLeastS) Colors.Dynamic.id else Colors.Sakura.id, | ||
enableNavigationAnimation = false | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
fun UserData.isDarkMode() = when (darkMode) { | ||
DarkMode.ALWAYS_OFF -> false | ||
DarkMode.ALWAYS_ON -> true | ||
else -> isSystemInDarkTheme() | ||
} | ||
|
||
fun UserData.toPreferences(): UserPreferences = UserPreferences.newBuilder() | ||
.setDarkMode(darkMode) | ||
.setThemeColor(themeColor) | ||
.setEnableNavigationAnimation(enableNavigationAnimation) | ||
.build() | ||
|
||
fun UserPreferences.toUserData() = UserData( | ||
darkMode = darkMode, | ||
themeColor = themeColor, | ||
enableNavigationAnimation = enableNavigationAnimation | ||
) |
37 changes: 37 additions & 0 deletions
37
app/src/main/kotlin/com/sanmer/geomag/datastore/UserPreferencesDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.sanmer.geomag.datastore | ||
|
||
import androidx.datastore.core.DataStore | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
|
||
class UserPreferencesDataSource @Inject constructor( | ||
private val userPreferences: DataStore<UserPreferences> | ||
) { | ||
val userData get() = userPreferences.data.map { it.toUserData() } | ||
|
||
suspend fun setDarkTheme(value: DarkMode) = withContext(Dispatchers.IO) { | ||
userPreferences.updateData { | ||
it.copy { | ||
darkMode = value | ||
} | ||
} | ||
} | ||
|
||
suspend fun setThemeColor(value: Int) = withContext(Dispatchers.IO) { | ||
userPreferences.updateData { | ||
it.copy { | ||
themeColor = value | ||
} | ||
} | ||
} | ||
|
||
suspend fun setEnableNavigationAnimation(value: Boolean) = withContext(Dispatchers.IO) { | ||
userPreferences.updateData { | ||
it.copy { | ||
enableNavigationAnimation = value | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
app/src/main/kotlin/com/sanmer/geomag/datastore/UserPreferencesSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.sanmer.geomag.datastore | ||
|
||
import androidx.datastore.core.CorruptionException | ||
import androidx.datastore.core.Serializer | ||
import com.google.protobuf.InvalidProtocolBufferException | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
import javax.inject.Inject | ||
|
||
|
||
class UserPreferencesSerializer @Inject constructor() : Serializer<UserPreferences> { | ||
override val defaultValue: UserPreferences = UserData.default().toPreferences() | ||
|
||
override suspend fun readFrom(input: InputStream): UserPreferences = | ||
try { | ||
UserPreferences.parseFrom(input) | ||
} catch (exception: InvalidProtocolBufferException) { | ||
throw CorruptionException("cannot read proto", exception) | ||
} | ||
|
||
override suspend fun writeTo(t: UserPreferences, output: OutputStream) { | ||
t.writeTo(output) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/kotlin/com/sanmer/geomag/datastore/di/DataStoreModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.sanmer.geomag.datastore.di | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.core.DataStoreFactory | ||
import androidx.datastore.dataStoreFile | ||
import com.sanmer.geomag.datastore.UserPreferences | ||
import com.sanmer.geomag.datastore.UserPreferencesSerializer | ||
import com.sanmer.geomag.di.ApplicationScope | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.coroutines.CoroutineScope | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object DataStoreModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun providesUserPreferencesDataStore( | ||
@ApplicationContext context: Context, | ||
userPreferencesSerializer: UserPreferencesSerializer, | ||
@ApplicationScope applicationScope: CoroutineScope | ||
): DataStore<UserPreferences> = | ||
DataStoreFactory.create( | ||
serializer = userPreferencesSerializer, | ||
scope = applicationScope, | ||
) { | ||
context.dataStoreFile("user_preferences.pb") | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
app/src/main/kotlin/com/sanmer/geomag/repository/UserDataRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.sanmer.geomag.repository | ||
|
||
import com.sanmer.geomag.datastore.DarkMode | ||
import com.sanmer.geomag.datastore.UserData | ||
import com.sanmer.geomag.datastore.UserPreferencesDataSource | ||
import com.sanmer.geomag.di.ApplicationScope | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class UserDataRepository @Inject constructor( | ||
private val userPreferencesDataSource: UserPreferencesDataSource, | ||
@ApplicationScope private val applicationScope: CoroutineScope | ||
) { | ||
val userData get() = userPreferencesDataSource.userData | ||
|
||
private var _value = UserData.default() | ||
val value get() = _value | ||
|
||
init { | ||
userPreferencesDataSource.userData | ||
.distinctUntilChanged() | ||
.onEach { | ||
_value = it | ||
}.launchIn(applicationScope) | ||
} | ||
|
||
fun setDarkTheme(value: DarkMode) = applicationScope.launch { | ||
userPreferencesDataSource.setDarkTheme(value) | ||
} | ||
|
||
fun setThemeColor(value: Int) = applicationScope.launch { | ||
userPreferencesDataSource.setThemeColor(value) | ||
} | ||
|
||
fun setEnableNavigationAnimation(value: Boolean) = applicationScope.launch { | ||
userPreferencesDataSource.setEnableNavigationAnimation(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
app/src/main/proto/com/sanmer/mrepo/datastore/UserPreferences.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
syntax = "proto3"; | ||
|
||
option java_package = "com.sanmer.geomag.datastore"; | ||
option java_multiple_files = true; | ||
|
||
enum DarkMode { | ||
FOLLOW_SYSTEM = 0; | ||
ALWAYS_OFF = 1; | ||
ALWAYS_ON = 2; | ||
} | ||
|
||
message UserPreferences { | ||
DarkMode darkMode = 1; | ||
int32 themeColor = 2; | ||
bool enableNavigationAnimation = 3; // TODO: Waiting for https://github.com/google/accompanist/issues/1487 to be fixed | ||
} |