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: remember patch options (ReVanced#1449)
- Loading branch information
Showing
16 changed files
with
511 additions
and
36 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
23 changes: 23 additions & 0 deletions
23
app/src/main/java/app/revanced/manager/data/room/options/Option.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,23 @@ | ||
package app.revanced.manager.data.room.options | ||
|
||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.ForeignKey | ||
|
||
@Entity( | ||
tableName = "options", | ||
primaryKeys = ["group", "patch_name", "key"], | ||
foreignKeys = [ForeignKey( | ||
OptionGroup::class, | ||
parentColumns = ["uid"], | ||
childColumns = ["group"], | ||
onDelete = ForeignKey.CASCADE | ||
)] | ||
) | ||
data class Option( | ||
@ColumnInfo(name = "group") val group: Int, | ||
@ColumnInfo(name = "patch_name") val patchName: String, | ||
@ColumnInfo(name = "key") val key: String, | ||
// Encoded as Json. | ||
@ColumnInfo(name = "value") val value: String, | ||
) |
51 changes: 51 additions & 0 deletions
51
app/src/main/java/app/revanced/manager/data/room/options/OptionDao.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,51 @@ | ||
package app.revanced.manager.data.room.options | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.MapInfo | ||
import androidx.room.Query | ||
import androidx.room.Transaction | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
@Dao | ||
abstract class OptionDao { | ||
@Transaction | ||
@MapInfo(keyColumn = "patch_bundle") | ||
@Query( | ||
"SELECT patch_bundle, `group`, patch_name, `key`, value FROM option_groups" + | ||
" LEFT JOIN options ON uid = options.`group`" + | ||
" WHERE package_name = :packageName" | ||
) | ||
abstract suspend fun getOptions(packageName: String): Map<Int, List<Option>> | ||
|
||
@Query("SELECT uid FROM option_groups WHERE patch_bundle = :bundleUid AND package_name = :packageName") | ||
abstract suspend fun getGroupId(bundleUid: Int, packageName: String): Int? | ||
|
||
@Query("SELECT package_name FROM option_groups") | ||
abstract fun getPackagesWithOptions(): Flow<List<String>> | ||
|
||
@Insert | ||
abstract suspend fun createOptionGroup(group: OptionGroup) | ||
|
||
@Query("DELETE FROM option_groups WHERE patch_bundle = :uid") | ||
abstract suspend fun clearForPatchBundle(uid: Int) | ||
|
||
@Query("DELETE FROM option_groups WHERE package_name = :packageName") | ||
abstract suspend fun clearForPackage(packageName: String) | ||
|
||
@Query("DELETE FROM option_groups") | ||
abstract suspend fun reset() | ||
|
||
@Insert | ||
protected abstract suspend fun insertOptions(patches: List<Option>) | ||
|
||
@Query("DELETE FROM options WHERE `group` = :groupId") | ||
protected abstract suspend fun clearGroup(groupId: Int) | ||
|
||
@Transaction | ||
open suspend fun updateOptions(options: Map<Int, List<Option>>) = | ||
options.forEach { (groupId, options) -> | ||
clearGroup(groupId) | ||
insertOptions(options) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
app/src/main/java/app/revanced/manager/data/room/options/OptionGroup.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.options | ||
|
||
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.bundles.PatchBundleEntity | ||
|
||
@Entity( | ||
tableName = "option_groups", | ||
foreignKeys = [ForeignKey( | ||
PatchBundleEntity::class, | ||
parentColumns = ["uid"], | ||
childColumns = ["patch_bundle"], | ||
onDelete = ForeignKey.CASCADE | ||
)], | ||
indices = [Index(value = ["patch_bundle", "package_name"], unique = true)] | ||
) | ||
data class OptionGroup( | ||
@PrimaryKey val uid: Int, | ||
@ColumnInfo(name = "patch_bundle") val patchBundle: Int, | ||
@ColumnInfo(name = "package_name") val packageName: String | ||
) |
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
89 changes: 89 additions & 0 deletions
89
app/src/main/java/app/revanced/manager/domain/repository/PatchOptionsRepository.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,89 @@ | ||
package app.revanced.manager.domain.repository | ||
|
||
import app.revanced.manager.data.room.AppDatabase | ||
import app.revanced.manager.data.room.options.Option | ||
import app.revanced.manager.data.room.options.OptionGroup | ||
import app.revanced.manager.util.Options | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonNull | ||
import kotlinx.serialization.json.JsonPrimitive | ||
import kotlinx.serialization.json.booleanOrNull | ||
import kotlinx.serialization.json.floatOrNull | ||
import kotlinx.serialization.json.intOrNull | ||
|
||
class PatchOptionsRepository(db: AppDatabase) { | ||
private val dao = db.optionDao() | ||
|
||
private suspend fun getOrCreateGroup(bundleUid: Int, packageName: String) = | ||
dao.getGroupId(bundleUid, packageName) ?: OptionGroup( | ||
uid = AppDatabase.generateUid(), | ||
patchBundle = bundleUid, | ||
packageName = packageName | ||
).also { dao.createOptionGroup(it) }.uid | ||
|
||
suspend fun getOptions(packageName: String): Options { | ||
val options = dao.getOptions(packageName) | ||
// Bundle -> Patches | ||
return buildMap<Int, MutableMap<String, MutableMap<String, Any?>>>(options.size) { | ||
options.forEach { (sourceUid, bundlePatchOptionsList) -> | ||
// Patches -> Patch options | ||
this[sourceUid] = bundlePatchOptionsList.fold(mutableMapOf()) { bundlePatchOptions, option -> | ||
val patchOptions = bundlePatchOptions.getOrPut(option.patchName, ::mutableMapOf) | ||
|
||
patchOptions[option.key] = deserialize(option.value) | ||
|
||
bundlePatchOptions | ||
} | ||
} | ||
} | ||
} | ||
|
||
suspend fun saveOptions(packageName: String, options: Options) = | ||
dao.updateOptions(options.entries.associate { (sourceUid, bundlePatchOptions) -> | ||
val groupId = getOrCreateGroup(sourceUid, packageName) | ||
|
||
groupId to bundlePatchOptions.flatMap { (patchName, patchOptions) -> | ||
patchOptions.mapNotNull { (key, value) -> | ||
val serialized = serialize(value) | ||
?: return@mapNotNull null // Don't save options that we can't serialize. | ||
|
||
Option(groupId, patchName, key, serialized) | ||
} | ||
} | ||
}) | ||
|
||
fun getPackagesWithSavedOptions() = | ||
dao.getPackagesWithOptions().map(Iterable<String>::toSet).distinctUntilChanged() | ||
|
||
suspend fun clearOptionsForPackage(packageName: String) = dao.clearForPackage(packageName) | ||
suspend fun clearOptionsForPatchBundle(uid: Int) = dao.clearForPatchBundle(uid) | ||
suspend fun reset() = dao.reset() | ||
|
||
private companion object { | ||
fun deserialize(value: String): Any? { | ||
val primitive = Json.decodeFromString<JsonPrimitive>(value) | ||
|
||
return when { | ||
primitive.isString -> primitive.content | ||
primitive is JsonNull -> null | ||
else -> primitive.booleanOrNull ?: primitive.intOrNull ?: primitive.floatOrNull | ||
} | ||
} | ||
|
||
fun serialize(value: Any?): String? { | ||
val primitive = when (value) { | ||
null -> JsonNull | ||
is String -> JsonPrimitive(value) | ||
is Int -> JsonPrimitive(value) | ||
is Float -> JsonPrimitive(value) | ||
is Boolean -> JsonPrimitive(value) | ||
else -> return null | ||
} | ||
|
||
return Json.encodeToString(primitive) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.