Include custom attr table in database cleanup

This commit is contained in:
MM20 2022-09-25 19:24:31 +02:00
parent 41766cc0ac
commit f1dbb84f61
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
3 changed files with 20 additions and 2 deletions

View File

@ -33,6 +33,7 @@ interface CustomAttributesRepository {
suspend fun getAllTags(startsWith: String? = null): List<String>
fun getItemsForTag(tag: String): Flow<List<Searchable>>
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
}
}

View File

@ -47,4 +47,7 @@ interface BackupRestoreDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun importCustomAttributes(items: List<CustomAttributeEntity>)
@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
}

View File

@ -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
}
}