Custom labels

This commit is contained in:
MM20
2022-09-11 18:42:02 +02:00
parent 29a06dfc4f
commit 7b86677fc2
18 changed files with 161 additions and 63 deletions
@@ -13,7 +13,8 @@ sealed interface CustomAttribute {
if (entity == null) return null
return when (entity.type) {
CustomAttributeType.Label.value -> CustomLabel(
label = entity.value
label = entity.value,
key = entity.key
)
CustomAttributeType.Tag.value -> CustomTag(
tagName = entity.value
@@ -31,6 +32,7 @@ sealed interface CustomAttribute {
class CustomLabel(
val key: String,
val label: String,
) : CustomAttribute {
override fun toDatabaseEntity(key: String): CustomAttributeEntity {
@@ -1,9 +1,9 @@
package de.mm20.launcher2.customattrs
import android.util.Log
import de.mm20.launcher2.crashreporter.CrashReporter
import de.mm20.launcher2.database.AppDatabase
import de.mm20.launcher2.database.entities.CustomAttributeEntity
import de.mm20.launcher2.database.entities.WebsearchEntity
import de.mm20.launcher2.ktx.jsonObjectOf
import de.mm20.launcher2.search.data.Searchable
import kotlinx.coroutines.*
@@ -17,6 +17,10 @@ interface CustomAttributesRepository {
fun getCustomIcon(searchable: Searchable): Flow<CustomIcon?>
fun setCustomIcon(searchable: Searchable, icon: CustomIcon?)
fun getCustomLabels(items: List<Searchable>): Flow<List<CustomLabel>>
fun setCustomLabel(searchable: Searchable, label: String)
fun clearCustomLabel(searchable: Searchable)
suspend fun export(toDir: File)
suspend fun import(fromDir: File)
}
@@ -44,6 +48,36 @@ internal class CustomAttributesRepositoryImpl(
}
}
override fun getCustomLabels(items: List<Searchable>): Flow<List<CustomLabel>> {
val dao = appDatabase.customAttrsDao()
return dao.getCustomAttributes(items.map { it.key }, CustomAttributeType.Label.value)
.map { list ->
list.mapNotNull { CustomAttribute.fromDatabaseEntity(it) as? CustomLabel }
}
}
override fun setCustomLabel(searchable: Searchable, label: String) {
val dao = appDatabase.customAttrsDao()
scope.launch {
appDatabase.runInTransaction {
dao.clearCustomAttribute(searchable.key, CustomAttributeType.Label.value)
dao.setCustomAttribute(
CustomLabel(
key = searchable.key,
label = label,
).toDatabaseEntity(searchable.key)
)
}
}
}
override fun clearCustomLabel(searchable: Searchable) {
val dao = appDatabase.customAttrsDao()
scope.launch {
dao.clearCustomAttribute(searchable.key, CustomAttributeType.Label.value)
}
}
override suspend fun export(toDir: File) = withContext(Dispatchers.IO) {
val dao = appDatabase.backupDao()
var page = 0
@@ -72,7 +106,9 @@ internal class CustomAttributesRepositoryImpl(
val dao = appDatabase.backupDao()
dao.wipeCustomAttributes()
val files = fromDir.listFiles { _, name -> name.startsWith("customizations.") } ?: return@withContext
val files =
fromDir.listFiles { _, name -> name.startsWith("customizations.") }
?: return@withContext
for (file in files) {
val customAttrs = mutableListOf<CustomAttributeEntity>()