fix: use upsert when modifying installed apps

This commit is contained in:
Ax333l 2023-10-19 22:01:15 +02:00
parent c3af6acb2c
commit 9df98edca5
No known key found for this signature in database
GPG Key ID: D2B4D85271127D23
3 changed files with 13 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import androidx.room.Insert
import androidx.room.MapInfo
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Upsert
import kotlinx.coroutines.flow.Flow
@Dao
@ -24,17 +25,21 @@ interface InstalledAppDao {
suspend fun getPatchesSelection(packageName: String): Map<Int, List<String>>
@Transaction
suspend fun insertApp(installedApp: InstalledApp, appliedPatches: List<AppliedPatch>) {
insertApp(installedApp)
suspend fun upsertApp(installedApp: InstalledApp, appliedPatches: List<AppliedPatch>) {
upsertApp(installedApp)
deleteAppliedPatches(installedApp.currentPackageName)
insertAppliedPatches(appliedPatches)
}
@Insert
suspend fun insertApp(installedApp: InstalledApp)
@Upsert
suspend fun upsertApp(installedApp: InstalledApp)
@Insert
suspend fun insertAppliedPatches(appliedPatches: List<AppliedPatch>)
@Query("DELETE FROM applied_patch WHERE package_name = :packageName")
suspend fun deleteAppliedPatches(packageName: String)
@Delete
suspend fun delete(installedApp: InstalledApp)
}

View File

@ -19,14 +19,14 @@ class InstalledAppRepository(
suspend fun getAppliedPatches(packageName: String): PatchesSelection =
dao.getPatchesSelection(packageName).mapValues { (_, patches) -> patches.toSet() }
suspend fun add(
suspend fun addOrUpdate(
currentPackageName: String,
originalPackageName: String,
version: String,
installType: InstallType,
patchesSelection: PatchesSelection
) {
dao.insertApp(
dao.upsertApp(
InstalledApp(
currentPackageName = currentPackageName,
originalPackageName = originalPackageName,

View File

@ -142,7 +142,7 @@ class InstallerViewModel(
installedPackageName =
intent.getStringExtra(InstallService.EXTRA_PACKAGE_NAME)
viewModelScope.launch {
installedAppRepository.add(
installedAppRepository.addOrUpdate(
installedPackageName!!,
packageName,
input.selectedApp.version,
@ -277,7 +277,7 @@ class InstallerViewModel(
installedApp?.let { installedAppRepository.delete(it) }
installedAppRepository.add(
installedAppRepository.addOrUpdate(
packageName,
packageName,
input.selectedApp.version,