[app] Use RepoManager for deleting repositories

This commit is contained in:
Torsten Grote
2023-05-26 15:12:05 -03:00
committed by Hans-Christoph Steiner
parent e819741c1a
commit e382900f39
2 changed files with 22 additions and 2 deletions

View File

@@ -1,5 +1,8 @@
package org.fdroid.index
import androidx.annotation.WorkerThread
import androidx.lifecycle.LiveData
import androidx.lifecycle.asLiveData
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
@@ -19,10 +22,14 @@ public class RepoManager @JvmOverloads constructor(
private val coroutineContext: CoroutineContext = Dispatchers.IO,
) {
private val repositoryDao = db.getRepositoryDao()
private val _repositoriesState: MutableStateFlow<List<Repository>> =
MutableStateFlow(emptyList())
public val repositoriesState: StateFlow<List<Repository>> = _repositoriesState.asStateFlow()
public val liveRepositories: LiveData<List<Repository>> = _repositoriesState.asLiveData()
/**
* Used internally as a mechanism to wait until repositories are loaded from the DB.
* This happens quite fast and the load is triggered at construction time.
@@ -34,7 +41,7 @@ public class RepoManager @JvmOverloads constructor(
init {
// we need to load the repositories first off the UiThread, so it doesn't deadlock
GlobalScope.launch(coroutineContext) {
_repositoriesState.value = db.getRepositoryDao().getRepositories()
_repositoriesState.value = repositoryDao.getRepositories()
repoCountDownLatch.countDown()
withContext(Dispatchers.Main) {
// keep observing the repos from the DB
@@ -64,4 +71,17 @@ public class RepoManager @JvmOverloads constructor(
return repositoriesState.value
}
/**
* Removes a Repository with the given repoId with all associated data from the database.
*/
@WorkerThread
public fun deleteRepository(repoId: Long) {
repositoryDao.deleteRepository(repoId)
// while this gets updated automatically, getting the update may be slow,
// so to speed up the UI, we emit the state change right away
_repositoriesState.value = _repositoriesState.value.filter { repository ->
repository.repoId == repoId
}
}
}