Skip to content

Commit

Permalink
fix: use upsert when modifying installed apps
Browse files Browse the repository at this point in the history
  • Loading branch information
Axelen123 committed Oct 19, 2023
1 parent c3af6ac commit 9df98ed
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class InstallerViewModel(
installedPackageName =
intent.getStringExtra(InstallService.EXTRA_PACKAGE_NAME)
viewModelScope.launch {
installedAppRepository.add(
installedAppRepository.addOrUpdate(
installedPackageName!!,
packageName,
input.selectedApp.version,
Expand Down Expand Up @@ -277,7 +277,7 @@ class InstallerViewModel(

installedApp?.let { installedAppRepository.delete(it) }

installedAppRepository.add(
installedAppRepository.addOrUpdate(
packageName,
packageName,
input.selectedApp.version,
Expand Down

0 comments on commit 9df98ed

Please sign in to comment.