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

Commit

Permalink
Use kotlinx.serialization.protobuf instead of google.protobuf
Browse files Browse the repository at this point in the history
- Remove `UserPreferencesCompat`
  • Loading branch information
SanmerDev committed Jun 27, 2024
1 parent d1babc5 commit 4db6301
Show file tree
Hide file tree
Showing 37 changed files with 188 additions and 335 deletions.
19 changes: 1 addition & 18 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ plugins {
alias(libs.plugins.self.room)
alias(libs.plugins.kotlin.parcelize)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.protobuf)
}

val baseVersionName = "2.5.0-beta02"
Expand Down Expand Up @@ -99,22 +98,6 @@ android {
}
}

protobuf {
protoc {
artifact = libs.protobuf.protoc.get().toString()
}

generateProtoTasks {
all().forEach { task ->
task.builtins {
register("java") {
option("lite")
}
}
}
}
}

dependencies {
implementation(projects.core)
implementation(libs.sanmer.su)
Expand All @@ -135,7 +118,7 @@ dependencies {
implementation(libs.kotlinx.coroutines.android)
implementation(libs.kotlinx.datetime)
implementation(libs.kotlinx.serialization.json)
implementation(libs.protobuf.kotlin.lite)
implementation(libs.kotlinx.serialization.protobuf)
implementation(libs.square.retrofit)
implementation(libs.square.retrofit.kotlinxSerialization)
implementation(libs.square.okhttp)
Expand Down
5 changes: 1 addition & 4 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
-repackageclasses dev.sanmer.mrepo

# Keep DataStore fields
-keepclassmembers class * extends com.google.protobuf.GeneratedMessageLite* { <fields>; }
-repackageclasses dev.sanmer.mrepo
2 changes: 1 addition & 1 deletion app/src/main/kotlin/dev/sanmer/mrepo/Compat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.sanmer.mrepo
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import dev.sanmer.mrepo.datastore.WorkingMode
import dev.sanmer.mrepo.datastore.model.WorkingMode
import dev.sanmer.mrepo.stub.IModuleManager
import dev.sanmer.su.IServiceManager
import dev.sanmer.su.ServiceManagerCompat
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package dev.sanmer.mrepo.datastore

import androidx.datastore.core.DataStore
import dev.sanmer.mrepo.datastore.model.DarkMode
import dev.sanmer.mrepo.datastore.model.ModulesMenu
import dev.sanmer.mrepo.datastore.model.RepositoryMenu
import dev.sanmer.mrepo.datastore.model.UserPreferences
import dev.sanmer.mrepo.datastore.model.WorkingMode
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File
import javax.inject.Inject

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

Expand Down Expand Up @@ -43,23 +47,23 @@ class UserPreferencesDataSource @Inject constructor(
}
}

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

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

suspend fun setModulesMenu(value: ModulesMenuCompat) = withContext(Dispatchers.IO) {
suspend fun setModulesMenu(value: ModulesMenu) = withContext(Dispatchers.IO) {
userPreferences.updateData {
it.copy(
modulesMenu = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ package dev.sanmer.mrepo.datastore

import androidx.datastore.core.CorruptionException
import androidx.datastore.core.Serializer
import com.google.protobuf.InvalidProtocolBufferException
import dev.sanmer.mrepo.datastore.model.UserPreferences
import kotlinx.serialization.SerializationException
import java.io.InputStream
import java.io.OutputStream
import javax.inject.Inject

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

override suspend fun readFrom(input: InputStream) =
try {
UserPreferencesCompat.from(input)
} catch (e: InvalidProtocolBufferException) {
throw CorruptionException("cannot read proto", e)
UserPreferences.decodeFrom(input)
} catch (e: SerializationException) {
throw CorruptionException("Failed to read proto", e)
}

override suspend fun writeTo(t: UserPreferencesCompat, output: OutputStream) {
t.writeTo(output)
override suspend fun writeTo(t: UserPreferences, output: OutputStream) {
t.encodeTo(output)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.core.DataStoreFactory
import androidx.datastore.dataStoreFile
import dev.sanmer.mrepo.datastore.UserPreferencesCompat
import dev.sanmer.mrepo.datastore.UserPreferencesSerializer
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import dev.sanmer.mrepo.datastore.UserPreferencesSerializer
import dev.sanmer.mrepo.datastore.model.UserPreferences
import javax.inject.Singleton

@Module
Expand All @@ -21,7 +21,7 @@ object DataStoreModule {
fun providesUserPreferencesDataStore(
@ApplicationContext context: Context,
userPreferencesSerializer: UserPreferencesSerializer
): DataStore<UserPreferencesCompat> =
): DataStore<UserPreferences> =
DataStoreFactory.create(
serializer = userPreferencesSerializer
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.sanmer.mrepo.datastore.model

enum class DarkMode {
FollowSystem,
AlwaysOff,
AlwaysOn
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.sanmer.mrepo.datastore.model

import kotlinx.serialization.Serializable

@Serializable
data class ModulesMenu(
val option: Option = Option.Name,
val descending: Boolean = false,
val pinEnabled: Boolean = false,
val showUpdatedTime: Boolean = false
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.sanmer.mrepo.datastore.model

enum class Option {
Name,
UpdatedTime
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.sanmer.mrepo.datastore.model

import kotlinx.serialization.Serializable

@Serializable
data class RepositoryMenu(
val option: Option = Option.Name,
val descending: Boolean = false,
val pinInstalled: Boolean = true,
val pinUpdatable: Boolean = true,
val showIcon: Boolean = true,
val showLicense: Boolean = true,
val showUpdatedTime: Boolean = false
)
Loading

0 comments on commit 4db6301

Please sign in to comment.