From f1dbb84f61084ad986bac18b7d72c5acf68f6b10 Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Sun, 25 Sep 2022 19:24:31 +0200 Subject: [PATCH] Include custom attr table in database cleanup --- .../customattrs/CustomAttributesRepository.kt | 11 +++++++++++ .../de/mm20/launcher2/database/BackupRestoreDao.kt | 3 +++ .../ui/settings/debug/DebugSettingsScreenVM.kt | 8 ++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttributesRepository.kt b/customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttributesRepository.kt index 519546e4..fefa61fe 100644 --- a/customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttributesRepository.kt +++ b/customattrs/src/main/java/de/mm20/launcher2/customattrs/CustomAttributesRepository.kt @@ -33,6 +33,7 @@ interface CustomAttributesRepository { suspend fun getAllTags(startsWith: String? = null): List fun getItemsForTag(tag: String): Flow> fun addTag(item: Searchable, tag: String) + suspend fun cleanupDatabase(): Int } internal class CustomAttributesRepositoryImpl( @@ -198,4 +199,14 @@ internal class CustomAttributesRepositoryImpl( } } } + + override suspend fun cleanupDatabase(): Int { + val dao = appDatabase.backupDao() + var removed = 0 + val job = scope.launch { + removed = dao.cleanUp() + } + job.join() + return removed + } } \ No newline at end of file diff --git a/database/src/main/java/de/mm20/launcher2/database/BackupRestoreDao.kt b/database/src/main/java/de/mm20/launcher2/database/BackupRestoreDao.kt index 780255b3..6d6f9bae 100644 --- a/database/src/main/java/de/mm20/launcher2/database/BackupRestoreDao.kt +++ b/database/src/main/java/de/mm20/launcher2/database/BackupRestoreDao.kt @@ -47,4 +47,7 @@ interface BackupRestoreDao { @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun importCustomAttributes(items: List) + + @Query("DELETE FROM CustomAttributes WHERE (type = 'tag' OR type = 'label') AND NOT EXISTS(SELECT 1 FROM Searchable WHERE CustomAttributes.key = Searchable.key)") + suspend fun cleanUp(): Int } \ No newline at end of file diff --git a/ui/src/main/java/de/mm20/launcher2/ui/settings/debug/DebugSettingsScreenVM.kt b/ui/src/main/java/de/mm20/launcher2/ui/settings/debug/DebugSettingsScreenVM.kt index 9c6b1a70..1f628fcb 100644 --- a/ui/src/main/java/de/mm20/launcher2/ui/settings/debug/DebugSettingsScreenVM.kt +++ b/ui/src/main/java/de/mm20/launcher2/ui/settings/debug/DebugSettingsScreenVM.kt @@ -2,6 +2,7 @@ package de.mm20.launcher2.ui.settings.debug import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import de.mm20.launcher2.customattrs.CustomAttributesRepository import de.mm20.launcher2.favorites.FavoritesRepository import kotlinx.coroutines.launch import org.koin.core.component.KoinComponent @@ -9,8 +10,11 @@ import org.koin.core.component.inject class DebugSettingsScreenVM: ViewModel(), KoinComponent { - val favoritesRepository: FavoritesRepository by inject() + private val favoritesRepository: FavoritesRepository by inject() + private val customAttributesRepository: CustomAttributesRepository by inject() suspend fun cleanUpDatabase(): Int { - return favoritesRepository.cleanupDatabase() + var removed = favoritesRepository.cleanupDatabase() + removed += customAttributesRepository.cleanupDatabase() + return removed } } \ No newline at end of file