Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugins {
alias(libs.plugins.junit5)
alias(libs.plugins.kotlinx.serialization)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.protobuf)
alias(libs.plugins.square.wire)
}

val packageName = "com.mitch.template"
Expand Down Expand Up @@ -246,21 +246,9 @@ tasks.withType<dev.detekt.gradle.DetektCreateBaselineTask>().configureEach {
jvmTarget = JvmTarget.JVM_17.target
}

protobuf {
protoc {
artifact = libs.protobuf.protoc.get().toString()
}
generateProtoTasks {
all().forEach { task ->
task.builtins {
register("java") {
option("lite")
}
register("kotlin") {
option("lite")
}
}
}
wire {
kotlin {
android = true
}
}

Expand Down Expand Up @@ -308,7 +296,6 @@ dependencies {

// Datastore (previously SharedPreferences)
implementation(libs.datastore.core)
implementation(libs.protobuf.kotlin.lite)

// Logging
implementation(libs.timber)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class DefaultUserSettingsRepository(
.map { protoPreferences ->
TemplateUserPreferences(
theme = protoPreferences.theme.toDomainModel(),
language = if (protoPreferences.locale.isEmpty()) {
language = if (protoPreferences.locale?.isEmpty() == true) {
TemplateLanguagePreference.FollowSystem
} else {
val locale = Locale.forLanguageTag(protoPreferences.locale)
val locale = Locale.forLanguageTag(protoPreferences.locale.orEmpty())
locale.toDomainModel()
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,19 @@ class UserPreferencesLocalDataSource(

suspend fun setTheme(theme: TemplateThemePreferenceProto) {
userPreferences.updateData {
it.copy {
this.theme = theme
}
it.copy(theme = theme)
}
}

suspend fun setLocale(locale: Locale) {
userPreferences.updateData {
it.copy {
this.locale = locale.toLanguageTag()
}
it.copy(locale = locale.toLanguageTag())
}
}

suspend fun resetLocale() {
userPreferences.updateData {
it.copy {
clearLocale()
}
it.copy(locale = null)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.mitch.template.data.userprefs

import androidx.datastore.core.CorruptionException
import androidx.datastore.core.Serializer
import kotlinx.serialization.SerializationException
import kotlinx.io.IOException
import java.io.InputStream
import java.io.OutputStream

Expand All @@ -13,20 +13,19 @@ import java.io.OutputStream
* see more at [Proto Datastore](https://developer.android.com/topic/libraries/architecture/datastore#proto-create)
*/
object UserPreferencesSerializer : Serializer<UserPreferencesProtoModel> {
override val defaultValue: UserPreferencesProtoModel =
UserPreferencesProtoModel.getDefaultInstance()
override val defaultValue: UserPreferencesProtoModel = UserPreferencesProtoModel()

override suspend fun readFrom(input: InputStream): UserPreferencesProtoModel {
return try {
// readFrom is already called on the data store background thread
UserPreferencesProtoModel.parseFrom(input)
} catch (exception: SerializationException) {
UserPreferencesProtoModel.ADAPTER.decode(input)
} catch (exception: IOException) {
throw CorruptionException("Cannot read proto. $exception")
}
}

override suspend fun writeTo(t: UserPreferencesProtoModel, output: OutputStream) {
// writeTo is already called on the data store background thread
t.writeTo(output)
UserPreferencesProtoModel.ADAPTER.encode(output, t)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ fun TemplateThemePreference.toProtoModel(): TemplateThemePreferenceProto = when
fun TemplateThemePreferenceProto.toDomainModel(): TemplateThemePreference = when (this) {
TemplateThemePreferenceProto.LIGHT -> TemplateThemePreference.Light
TemplateThemePreferenceProto.DARK -> TemplateThemePreference.Dark
TemplateThemePreferenceProto.UNRECOGNIZED,
TemplateThemePreferenceProto.FOLLOW_SYSTEM -> TemplateThemePreference.FollowSystem
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ plugins {
alias(libs.plugins.kotlinx.serialization) apply false
alias(libs.plugins.compose.compiler) apply false
alias(libs.plugins.android.lint) apply false
alias(libs.plugins.protobuf) apply false
alias(libs.plugins.square.wire) apply false
}
4 changes: 0 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,3 @@ kotlin.code.style=official

# Enable R8 full mode.
android.enableR8.fullMode=true

# Blockers:
# - https://github.com/google/protobuf-gradle-plugin/issues/787
android.newDsl=false
9 changes: 4 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ room = "2.8.4"

# Datastore
datastore = "1.2.1"
protobuf = "4.34.0"
protobufPlugin = "0.9.6"

# Proto
square-wire = "6.0.0"

# Logging
timber = "5.0.1"
Expand Down Expand Up @@ -115,8 +116,6 @@ room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "

# Datastore
datastore-core = { group = "androidx.datastore", name = "datastore", version.ref = "datastore" }
protobuf-kotlin-lite = { group = "com.google.protobuf", name = "protobuf-kotlin-lite", version.ref = "protobuf" }
protobuf-protoc = { group = "com.google.protobuf", name = "protoc", version.ref = "protobuf" }

# Logging
timber = { group = "com.jakewharton.timber", name = "timber", version.ref = "timber" }
Expand Down Expand Up @@ -173,4 +172,4 @@ junit5 = { id = "de.mannodermaus.android-junit5", version.ref = "junit5-plugin"
android-test = { id = "com.android.test", version.ref = "android-gradle-plugin" }
kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
android-lint = { id = "com.android.lint", version.ref = "android-gradle-plugin" }
protobuf = { id = "com.google.protobuf", version.ref = "protobufPlugin" }
square-wire = { id = "com.squareup.wire", version.ref = "square-wire" }
Loading