Add settings page to manage tags

This commit is contained in:
MM20
2022-12-21 19:24:32 +01:00
parent 85f7a740e0
commit ddc157741a
16 changed files with 586 additions and 38 deletions
+2
View File
@@ -47,5 +47,7 @@ dependencies {
implementation(project(":core:base"))
implementation(project(":core:ktx"))
implementation(project(":core:crashreporter"))
implementation(project(":data:customattrs"))
implementation(project(":data:favorites"))
}
@@ -4,5 +4,5 @@ import de.mm20.launcher2.services.tags.impl.TagsServiceImpl
import org.koin.dsl.module
val servicesTagsModule = module {
single<TagsService> { TagsServiceImpl() }
single<TagsService> { TagsServiceImpl(get(), get()) }
}
@@ -1,8 +1,15 @@
package de.mm20.launcher2.services.tags
import de.mm20.launcher2.search.SavableSearchable
import kotlinx.coroutines.flow.Flow
interface TagsService {
fun getTags(startsWith: String? = null): List<String>
fun renameTag(oldName: String, newName: String)
fun getAllTags(startsWith: String? = null): Flow<List<String>>
fun deleteTag(tag: String)
fun cloneTag(tag: String, newTag: String)
fun getTaggedItems(tag: String): Flow<List<SavableSearchable>>
fun createTag(tag: String, items: List<SavableSearchable>)
fun updateTag(tag: String, newName: String? = null, items: List<SavableSearchable>? = null)
}
@@ -1,22 +1,67 @@
package de.mm20.launcher2.services.tags.impl
import de.mm20.launcher2.data.customattrs.CustomAttributesRepository
import de.mm20.launcher2.favorites.FavoritesRepository
import de.mm20.launcher2.search.SavableSearchable
import de.mm20.launcher2.search.data.Tag
import de.mm20.launcher2.services.tags.TagsService
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
internal class TagsServiceImpl(
): TagsService {
override fun getTags(startsWith: String?): List<String> {
TODO("Not yet implemented")
}
override fun renameTag(oldName: String, newName: String) {
TODO("Not yet implemented")
private val customAttributesRepository: CustomAttributesRepository,
private val favoritesRepository: FavoritesRepository,
) : TagsService {
private val scope = CoroutineScope(Job() + Dispatchers.Default)
override fun getAllTags(startsWith: String?): Flow<List<String>> {
return customAttributesRepository.getAllTags(startsWith)
}
override fun deleteTag(tag: String) {
TODO("Not yet implemented")
favoritesRepository.remove(Tag(tag))
customAttributesRepository.deleteTag(tag)
}
override fun cloneTag(tag: String, newTag: String) {
TODO("Not yet implemented")
scope.launch {
val items = getTaggedItems(tag).first()
createTag(newTag, items)
}
}
override fun getTaggedItems(tag: String): Flow<List<SavableSearchable>> {
return customAttributesRepository.getItemsForTag(tag)
}
override fun updateTag(tag: String, newName: String?, items: List<SavableSearchable>?) {
scope.launch {
if (items != null) {
customAttributesRepository.setItemsForTag(tag, items).join()
}
if (newName != null && newName != tag) {
customAttributesRepository.renameTag(tag, newName).join()
val pinnedTags = favoritesRepository.getFavorites(
includeTypes = listOf(Tag.Domain),
manuallySorted = true,
automaticallySorted = true
).first()
val oldTag = Tag(tag)
if (pinnedTags.any { it.key == oldTag.key }) {
favoritesRepository.unpinItem(oldTag)
favoritesRepository.pinItem(Tag(newName))
}
}
}
}
override fun createTag(tag: String, items: List<SavableSearchable>) {
scope.launch {
customAttributesRepository.setItemsForTag(tag, items)
}
}
}