-
-
Notifications
You must be signed in to change notification settings - Fork 776
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: patch bundle sources system (#24)
- Loading branch information
Showing
40 changed files
with
1,064 additions
and
170 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
68 changes: 68 additions & 0 deletions
68
app/schemas/app.revanced.manager.compose.data.room.AppDatabase/1.json
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,68 @@ | ||
{ | ||
"formatVersion": 1, | ||
"database": { | ||
"version": 1, | ||
"identityHash": "b40f3b048880f3f3c9361f6d1c4aaea5", | ||
"entities": [ | ||
{ | ||
"tableName": "sources", | ||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`uid` INTEGER NOT NULL, `name` TEXT NOT NULL, `location` TEXT NOT NULL, `version` TEXT NOT NULL, `integrations_version` TEXT NOT NULL, PRIMARY KEY(`uid`))", | ||
"fields": [ | ||
{ | ||
"fieldPath": "uid", | ||
"columnName": "uid", | ||
"affinity": "INTEGER", | ||
"notNull": true | ||
}, | ||
{ | ||
"fieldPath": "name", | ||
"columnName": "name", | ||
"affinity": "TEXT", | ||
"notNull": true | ||
}, | ||
{ | ||
"fieldPath": "location", | ||
"columnName": "location", | ||
"affinity": "TEXT", | ||
"notNull": true | ||
}, | ||
{ | ||
"fieldPath": "versionInfo.patches", | ||
"columnName": "version", | ||
"affinity": "TEXT", | ||
"notNull": true | ||
}, | ||
{ | ||
"fieldPath": "versionInfo.integrations", | ||
"columnName": "integrations_version", | ||
"affinity": "TEXT", | ||
"notNull": true | ||
} | ||
], | ||
"primaryKey": { | ||
"autoGenerate": false, | ||
"columnNames": [ | ||
"uid" | ||
] | ||
}, | ||
"indices": [ | ||
{ | ||
"name": "index_sources_name", | ||
"unique": true, | ||
"columnNames": [ | ||
"name" | ||
], | ||
"orders": [], | ||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_sources_name` ON `${TABLE_NAME}` (`name`)" | ||
} | ||
], | ||
"foreignKeys": [] | ||
} | ||
], | ||
"views": [], | ||
"setupQueries": [ | ||
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", | ||
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'b40f3b048880f3f3c9361f6d1c4aaea5')" | ||
] | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
app/src/main/java/app/revanced/manager/compose/data/room/AppDatabase.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,13 @@ | ||
package app.revanced.manager.compose.data.room | ||
|
||
import androidx.room.Database | ||
import androidx.room.RoomDatabase | ||
import androidx.room.TypeConverters | ||
import app.revanced.manager.compose.data.room.sources.SourceEntity | ||
import app.revanced.manager.compose.data.room.sources.SourceDao | ||
|
||
@Database(entities = [SourceEntity::class], version = 1) | ||
@TypeConverters(Converters::class) | ||
abstract class AppDatabase : RoomDatabase() { | ||
abstract fun sourceDao(): SourceDao | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/app/revanced/manager/compose/data/room/Converters.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,16 @@ | ||
package app.revanced.manager.compose.data.room | ||
|
||
import androidx.room.TypeConverter | ||
import app.revanced.manager.compose.data.room.sources.SourceLocation | ||
import io.ktor.http.* | ||
|
||
class Converters { | ||
@TypeConverter | ||
fun locationFromString(value: String) = when(value) { | ||
SourceLocation.Local.SENTINEL -> SourceLocation.Local | ||
else -> SourceLocation.Remote(Url(value)) | ||
} | ||
|
||
@TypeConverter | ||
fun locationToString(location: SourceLocation) = location.toString() | ||
} |
24 changes: 24 additions & 0 deletions
24
app/src/main/java/app/revanced/manager/compose/data/room/sources/SourceDao.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.compose.data.room.sources | ||
|
||
import androidx.room.* | ||
|
||
@Dao | ||
interface SourceDao { | ||
@Query("SELECT * FROM $sourcesTableName") | ||
suspend fun all(): List<SourceEntity> | ||
|
||
@Query("SELECT version, integrations_version FROM $sourcesTableName WHERE uid = :uid") | ||
suspend fun getVersionById(uid: Int): VersionInfo | ||
|
||
@Query("UPDATE $sourcesTableName SET version=:patches, integrations_version=:integrations WHERE uid=:uid") | ||
suspend fun updateVersion(uid: Int, patches: String, integrations: String) | ||
|
||
@Query("DELETE FROM $sourcesTableName") | ||
suspend fun purge() | ||
|
||
@Query("DELETE FROM $sourcesTableName WHERE uid=:uid") | ||
suspend fun remove(uid: Int) | ||
|
||
@Insert | ||
suspend fun add(source: SourceEntity) | ||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/java/app/revanced/manager/compose/data/room/sources/SourceEntity.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,31 @@ | ||
package app.revanced.manager.compose.data.room.sources | ||
|
||
import androidx.room.* | ||
import io.ktor.http.* | ||
|
||
const val sourcesTableName = "sources" | ||
|
||
sealed class SourceLocation { | ||
object Local : SourceLocation() { | ||
const val SENTINEL = "local" | ||
|
||
override fun toString() = SENTINEL | ||
} | ||
|
||
data class Remote(val url: Url) : SourceLocation() { | ||
override fun toString() = url.toString() | ||
} | ||
} | ||
|
||
data class VersionInfo( | ||
@ColumnInfo(name = "version") val patches: String, | ||
@ColumnInfo(name = "integrations_version") val integrations: String, | ||
) | ||
|
||
@Entity(tableName = sourcesTableName, indices = [Index(value = ["name"], unique = true)]) | ||
data class SourceEntity( | ||
@PrimaryKey val uid: Int, | ||
@ColumnInfo(name = "name") val name: String, | ||
@Embedded val versionInfo: VersionInfo, | ||
@ColumnInfo(name = "location") val location: SourceLocation, | ||
) |
15 changes: 15 additions & 0 deletions
15
app/src/main/java/app/revanced/manager/compose/di/DatabaseModule.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,15 @@ | ||
package app.revanced.manager.compose.di | ||
|
||
import android.content.Context | ||
import androidx.room.Room | ||
import app.revanced.manager.compose.data.room.AppDatabase | ||
import org.koin.android.ext.koin.androidContext | ||
import org.koin.dsl.module | ||
|
||
val databaseModule = module { | ||
fun provideAppDatabase(context: Context) = Room.databaseBuilder(context, AppDatabase::class.java, "manager").build() | ||
|
||
single { | ||
provideAppDatabase(androidContext()) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/app/revanced/manager/compose/di/ManagerModule.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,10 @@ | ||
package app.revanced.manager.compose.di | ||
|
||
import app.revanced.manager.compose.domain.repository.SourceRepository | ||
import app.revanced.manager.compose.patcher.SignerService | ||
import org.koin.core.module.dsl.singleOf | ||
import org.koin.dsl.module | ||
|
||
val managerModule = module { | ||
singleOf(::SignerService) | ||
} |
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
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
50 changes: 50 additions & 0 deletions
50
app/src/main/java/app/revanced/manager/compose/domain/repository/BundleRepository.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,50 @@ | ||
package app.revanced.manager.compose.domain.repository | ||
|
||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.lifecycleScope | ||
import app.revanced.manager.compose.patcher.patch.PatchBundle | ||
import app.revanced.manager.compose.util.launchAndRepeatWithViewLifecycle | ||
import kotlinx.coroutines.flow.* | ||
import kotlinx.coroutines.launch | ||
|
||
class BundleRepository(private val sourceRepository: SourceRepository) { | ||
/** | ||
* A [Flow] that emits whenever the sources change. | ||
* | ||
* The outer flow emits whenever the sources configuration changes. | ||
* The inner flow emits whenever one of the bundles update. | ||
*/ | ||
private val sourceUpdates = sourceRepository.sources.map { sources -> | ||
sources.map { (name, source) -> | ||
source.bundle.map { bundle -> | ||
name to bundle | ||
} | ||
}.merge().buffer() | ||
} | ||
|
||
private val _bundles = MutableStateFlow<Map<String, PatchBundle>>(emptyMap()) | ||
|
||
/** | ||
* A [Flow] that gives you all loaded [PatchBundle]s. | ||
* This is only synced when the app is in the foreground. | ||
*/ | ||
val bundles = _bundles.asStateFlow() | ||
|
||
fun onAppStart(lifecycleOwner: LifecycleOwner) { | ||
lifecycleOwner.lifecycleScope.launch { | ||
sourceRepository.loadSources() | ||
} | ||
|
||
lifecycleOwner.launchAndRepeatWithViewLifecycle { | ||
sourceUpdates.collect { events -> | ||
val map = HashMap<String, PatchBundle>() | ||
_bundles.emit(map) | ||
|
||
events.collect { (name, new) -> | ||
map[name] = new | ||
_bundles.emit(map) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.