Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Optimize datastore (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev authored May 22, 2024
1 parent 46742a0 commit 0fa0608
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 62 deletions.
17 changes: 0 additions & 17 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import com.android.build.gradle.internal.api.ApkVariantOutputImpl
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(libs.plugins.self.application)
Expand Down Expand Up @@ -108,22 +107,6 @@ protobuf {
register("java") {
option("lite")
}
register("kotlin") {
option("lite")
}
}
}
}
}

// Workaround for https://github.com/google/ksp/issues/1590
androidComponents {
onVariants(selector().all()) { variant ->
afterEvaluate {
@Suppress("DEPRECATION")
val capName = variant.name.capitalize()
tasks.getByName<KotlinCompile>("ksp${capName}Kotlin") {
setSource(tasks.getByName("generate${capName}Proto").outputs)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ data class UserPreferencesCompat(
deleteZipFile = original.deleteZipFile,
useDoh = original.useDoh,
downloadPath = original.downloadPath.ifEmpty{ Const.PUBLIC_DOWNLOADS.absolutePath }.let(::File),
repositoryMenu = original.repositoryMenuOrNull?.let(::RepositoryMenuCompat)
?: RepositoryMenuCompat.default(),
modulesMenu = original.modulesMenuOrNull?.let(::ModulesMenuCompat)
?: ModulesMenuCompat.default()
repositoryMenu = when {
original.hasRepositoryMenu() -> RepositoryMenuCompat(original.repositoryMenu)
else -> RepositoryMenuCompat.default()
},
modulesMenu = when {
original.hasModulesMenu() -> ModulesMenuCompat(original.modulesMenu)
else -> ModulesMenuCompat.default()
}
)

@Composable
Expand All @@ -48,7 +52,7 @@ data class UserPreferencesCompat(
.setThemeColor(themeColor)
.setDeleteZipFile(deleteZipFile)
.setUseDoh(useDoh)
.setDownloadPath(downloadPath.absolutePath)
.setDownloadPath(downloadPath.path)
.setRepositoryMenu(repositoryMenu.toProto())
.setModulesMenu(modulesMenu.toProto())
.build()
Expand All @@ -64,11 +68,5 @@ data class UserPreferencesCompat(
repositoryMenu = RepositoryMenuCompat.default(),
modulesMenu = ModulesMenuCompat.default()
)

fun UserPreferences.new(
block: UserPreferencesKt.Dsl.() -> Unit
) = UserPreferencesCompat(this)
.toProto()
.copy(block)
}
}
Original file line number Diff line number Diff line change
@@ -1,80 +1,79 @@
package com.sanmer.mrepo.datastore

import androidx.datastore.core.DataStore
import com.sanmer.mrepo.datastore.UserPreferencesCompat.Companion.new
import com.sanmer.mrepo.datastore.modules.ModulesMenuCompat
import com.sanmer.mrepo.datastore.repository.RepositoryMenuCompat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import java.io.File
import javax.inject.Inject

class UserPreferencesDataSource @Inject constructor(
private val userPreferences: DataStore<UserPreferences>
private val userPreferences: DataStore<UserPreferencesCompat>
) {
val data get() = userPreferences.data.map { UserPreferencesCompat(it) }
val data get() = userPreferences.data

suspend fun setWorkingMode(value: WorkingMode) = withContext(Dispatchers.IO) {
userPreferences.updateData {
it.new {
it.copy(
workingMode = value
}
)
}
}

suspend fun setDarkTheme(value: DarkMode) = withContext(Dispatchers.IO) {
userPreferences.updateData {
it.new {
it.copy(
darkMode = value
}
)
}
}

suspend fun setThemeColor(value: Int) = withContext(Dispatchers.IO) {
userPreferences.updateData {
it.new {
it.copy(
themeColor = value
}
)
}
}

suspend fun setDeleteZipFile(value: Boolean) = withContext(Dispatchers.IO) {
userPreferences.updateData {
it.new {
it.copy(
deleteZipFile = value
}
)
}
}

suspend fun setUseDoh(value: Boolean) = withContext(Dispatchers.IO) {
userPreferences.updateData {
it.new {
it.copy(
useDoh = value
}
)
}
}

suspend fun setDownloadPath(value: String) = withContext(Dispatchers.IO) {
suspend fun setDownloadPath(value: File) = withContext(Dispatchers.IO) {
userPreferences.updateData {
it.new {
it.copy(
downloadPath = value
}
)
}
}

suspend fun setRepositoryMenu(value: RepositoryMenuCompat) = withContext(Dispatchers.IO) {
userPreferences.updateData {
it.new {
repositoryMenu = value.toProto()
}
it.copy(
repositoryMenu = value
)
}
}

suspend fun setModulesMenu(value: ModulesMenuCompat) = withContext(Dispatchers.IO) {
userPreferences.updateData {
it.new {
modulesMenu = value.toProto()
}
it.copy(
modulesMenu = value
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import java.io.InputStream
import java.io.OutputStream
import javax.inject.Inject

class UserPreferencesSerializer @Inject constructor() : Serializer<UserPreferences> {
override val defaultValue: UserPreferences = UserPreferencesCompat.default().toProto()
class UserPreferencesSerializer @Inject constructor() : Serializer<UserPreferencesCompat> {
override val defaultValue = UserPreferencesCompat.default()

override suspend fun readFrom(input: InputStream): UserPreferences =
override suspend fun readFrom(input: InputStream) =
try {
UserPreferences.parseFrom(input)
} catch (exception: InvalidProtocolBufferException) {
throw CorruptionException("cannot read proto", exception)
UserPreferences.parseFrom(input).let(::UserPreferencesCompat)
} catch (e: InvalidProtocolBufferException) {
throw CorruptionException("cannot read proto", e)
}

override suspend fun writeTo(t: UserPreferences, output: OutputStream) {
t.writeTo(output)
override suspend fun writeTo(t: UserPreferencesCompat, output: OutputStream) {
t.toProto().writeTo(output)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.core.DataStoreFactory
import androidx.datastore.dataStoreFile
import com.sanmer.mrepo.datastore.UserPreferences
import com.sanmer.mrepo.datastore.UserPreferencesCompat
import com.sanmer.mrepo.datastore.UserPreferencesSerializer
import dagger.Module
import dagger.Provides
Expand All @@ -21,7 +21,7 @@ object DataStoreModule {
fun providesUserPreferencesDataStore(
@ApplicationContext context: Context,
userPreferencesSerializer: UserPreferencesSerializer
): DataStore<UserPreferences> =
): DataStore<UserPreferencesCompat> =
DataStoreFactory.create(
serializer = userPreferencesSerializer
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class UserPreferencesRepository @Inject constructor(

suspend fun setUseDoh(value: Boolean) = userPreferencesDataSource.setUseDoh(value)

suspend fun setDownloadPath(value: File) = userPreferencesDataSource.setDownloadPath(value.absolutePath)
suspend fun setDownloadPath(value: File) = userPreferencesDataSource.setDownloadPath(value)

suspend fun setRepositoryMenu(value: RepositoryMenuCompat) = userPreferencesDataSource.setRepositoryMenu(value)

Expand Down

0 comments on commit 0fa0608

Please sign in to comment.