Move badge settings to decentralized data store, add preference for plugin badges

This commit is contained in:
MM20
2023-12-07 22:44:41 +01:00
parent 0f6d636ca0
commit 2479fdbc03
15 changed files with 268 additions and 95 deletions
+1
View File
@@ -42,6 +42,7 @@ dependencies {
implementation(libs.androidx.core)
implementation(libs.androidx.appcompat)
implementation(libs.androidx.datastore)
implementation(libs.materialcomponents.core)
implementation(libs.koin.android)
@@ -0,0 +1,66 @@
package de.mm20.launcher2.settings
import android.content.Context
import android.util.Log
import androidx.datastore.core.DataMigration
import androidx.datastore.core.Serializer
import androidx.datastore.dataStore
import de.mm20.launcher2.backup.Backupable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.serialization.SerializationException
import java.io.File
abstract class BaseSettings<T>(
internal val context: Context,
private val fileName: String,
private val serializer: Serializer<T>,
migrations: List<DataMigration<T>>
): Backupable {
protected val scope = CoroutineScope(Job() + Dispatchers.Default)
protected val Context.dataStore by dataStore(
fileName = fileName,
serializer = serializer,
produceMigrations = {
migrations
},
)
protected fun updateData(block: suspend (T) -> T) {
scope.launch {
context.dataStore.updateData(block)
}
}
override suspend fun backup(toDir: File) {
val data = context.dataStore.data.first()
val file = File(toDir, fileName)
file.outputStream().use {
serializer.writeTo(data, it)
}
}
override suspend fun restore(fromDir: File) {
val file = File(fromDir, fileName)
if (!file.exists()) {
return
}
try {
file.inputStream().use {
val data = serializer.readFrom(it)
context.dataStore.updateData {
data
}
}
} catch (e: SerializationException) {
Log.e("MM20", "Cannot restore $fileName", e)
} catch (e: IllegalArgumentException) {
Log.e("MM20", "Cannot restore $fileName", e)
}
}
}
@@ -552,6 +552,8 @@
<string name="preference_cloud_badges_summary">Show a badge for files that are stored in a cloud</string>
<string name="preference_shortcut_badges">Shortcut badges</string>
<string name="preference_shortcut_badges_summary">Show a badge which indicates to which app a shortcut belongs</string>
<string name="preference_plugin_badges">Plugin badges</string>
<string name="preference_plugin_badges_summary">Indicate by which plugin a search result was created</string>
<string name="preference_microsoft">Microsoft</string>
<string name="preference_ms_signin">Sign in with Microsoft</string>
<string name="preference_ms_signin_summary">Sign in to search OneDrive</string>
@@ -221,7 +221,7 @@ message Settings {
bool cloud_files = 3;
bool shortcuts = 4;
}
BadgeSettings badges = 18;
BadgeSettings badges = 18 [deprecated = true];
message GridSettings {
uint32 column_count = 1;