forked from ReVanced/revanced-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: save patch selection using room db (ReVanced#38)
- Loading branch information
Showing
24 changed files
with
766 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
app/src/main/java/app/revanced/manager/data/room/selection/PatchSelection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package app.revanced.manager.data.room.selection | ||
|
||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.ForeignKey | ||
import androidx.room.Index | ||
import androidx.room.PrimaryKey | ||
import app.revanced.manager.data.room.sources.SourceEntity | ||
|
||
@Entity( | ||
tableName = "patch_selections", | ||
foreignKeys = [ForeignKey( | ||
SourceEntity::class, | ||
parentColumns = ["uid"], | ||
childColumns = ["source"], | ||
onDelete = ForeignKey.CASCADE | ||
)], | ||
indices = [Index(value = ["source", "package_name"], unique = true)] | ||
) | ||
data class PatchSelection( | ||
@PrimaryKey val uid: Int, | ||
@ColumnInfo(name = "source") val source: Int, | ||
@ColumnInfo(name = "package_name") val packageName: String | ||
) |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/app/revanced/manager/data/room/selection/SelectedPatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package app.revanced.manager.data.room.selection | ||
|
||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.ForeignKey | ||
|
||
@Entity( | ||
tableName = "selected_patches", | ||
primaryKeys = ["selection", "patch_name"], | ||
foreignKeys = [ForeignKey( | ||
PatchSelection::class, | ||
parentColumns = ["uid"], | ||
childColumns = ["selection"], | ||
onDelete = ForeignKey.CASCADE | ||
)] | ||
) | ||
data class SelectedPatch( | ||
@ColumnInfo(name = "selection") val selection: Int, | ||
@ColumnInfo(name = "patch_name") val patchName: String | ||
) |
53 changes: 53 additions & 0 deletions
53
app/src/main/java/app/revanced/manager/data/room/selection/SelectionDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package app.revanced.manager.data.room.selection | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.MapInfo | ||
import androidx.room.Query | ||
import androidx.room.Transaction | ||
|
||
@Dao | ||
abstract class SelectionDao { | ||
@Transaction | ||
@MapInfo(keyColumn = "source", valueColumn = "patch_name") | ||
@Query( | ||
"SELECT source, patch_name FROM patch_selections" + | ||
" LEFT JOIN selected_patches ON uid = selected_patches.selection" + | ||
" WHERE package_name = :packageName" | ||
) | ||
abstract suspend fun getSelectedPatches(packageName: String): Map<Int, List<String>> | ||
|
||
@Transaction | ||
@MapInfo(keyColumn = "package_name", valueColumn = "patch_name") | ||
@Query( | ||
"SELECT package_name, patch_name FROM patch_selections" + | ||
" LEFT JOIN selected_patches ON uid = selected_patches.selection" + | ||
" WHERE source = :sourceUid" | ||
) | ||
abstract suspend fun exportSelection(sourceUid: Int): Map<String, List<String>> | ||
|
||
@Query("SELECT uid FROM patch_selections WHERE source = :sourceUid AND package_name = :packageName") | ||
abstract suspend fun getSelectionId(sourceUid: Int, packageName: String): Int? | ||
|
||
@Insert | ||
abstract suspend fun createSelection(selection: PatchSelection) | ||
|
||
@Query("DELETE FROM patch_selections WHERE source = :uid") | ||
abstract suspend fun clearForSource(uid: Int) | ||
|
||
@Query("DELETE FROM patch_selections") | ||
abstract suspend fun reset() | ||
|
||
@Insert | ||
protected abstract suspend fun selectPatches(patches: List<SelectedPatch>) | ||
|
||
@Query("DELETE FROM selected_patches WHERE selection = :selectionId") | ||
protected abstract suspend fun clearSelection(selectionId: Int) | ||
|
||
@Transaction | ||
open suspend fun updateSelections(selections: Map<Int, Set<String>>) = | ||
selections.map { (selectionUid, patches) -> | ||
clearSelection(selectionUid) | ||
selectPatches(patches.map { SelectedPatch(selectionUid, it) }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
app/src/main/java/app/revanced/manager/domain/repository/PatchSelectionRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package app.revanced.manager.domain.repository | ||
|
||
import app.revanced.manager.data.room.AppDatabase | ||
import app.revanced.manager.data.room.AppDatabase.Companion.generateUid | ||
import app.revanced.manager.data.room.selection.PatchSelection | ||
import app.revanced.manager.domain.sources.Source | ||
|
||
class PatchSelectionRepository(db: AppDatabase) { | ||
private val dao = db.selectionDao() | ||
|
||
private suspend fun getOrCreateSelection(sourceUid: Int, packageName: String) = | ||
dao.getSelectionId(sourceUid, packageName) ?: PatchSelection( | ||
uid = generateUid(), | ||
source = sourceUid, | ||
packageName = packageName | ||
).also { dao.createSelection(it) }.uid | ||
|
||
suspend fun getSelection(packageName: String): Map<Int, Set<String>> = | ||
dao.getSelectedPatches(packageName).mapValues { it.value.toSet() } | ||
|
||
suspend fun updateSelection(packageName: String, selection: Map<Int, Set<String>>) = | ||
dao.updateSelections(selection.mapKeys { (sourceUid, _) -> | ||
getOrCreateSelection( | ||
sourceUid, | ||
packageName | ||
) | ||
}) | ||
|
||
suspend fun reset() = dao.reset() | ||
|
||
suspend fun export(source: Source): SerializedSelection = dao.exportSelection(source.uid) | ||
|
||
suspend fun import(source: Source, selection: SerializedSelection) { | ||
dao.clearForSource(source.uid) | ||
dao.updateSelections(selection.entries.associate { (packageName, patches) -> | ||
getOrCreateSelection(source.uid, packageName) to patches.toSet() | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* A [Map] of package name -> selected patches. | ||
*/ | ||
typealias SerializedSelection = Map<String, List<String>> |
4 changes: 1 addition & 3 deletions
4
app/src/main/java/app/revanced/manager/domain/repository/SourcePersistenceRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.