mirror of
https://github.com/revanced/revanced-manager.git
synced 2025-05-03 15:24:25 +02:00
refactor: use EventEffect for legacy import
This commit is contained in:
parent
7644a74648
commit
fff1a41fee
@ -1,10 +1,13 @@
|
|||||||
package app.revanced.manager
|
package app.revanced.manager
|
||||||
|
|
||||||
|
import android.content.ActivityNotFoundException
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Parcelable
|
import android.os.Parcelable
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.animation.ExperimentalAnimationApi
|
import androidx.compose.animation.ExperimentalAnimationApi
|
||||||
import androidx.compose.foundation.isSystemInDarkTheme
|
import androidx.compose.foundation.isSystemInDarkTheme
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
@ -46,12 +49,22 @@ class MainActivity : ComponentActivity() {
|
|||||||
installSplashScreen()
|
installSplashScreen()
|
||||||
|
|
||||||
val vm: MainViewModel = getActivityViewModel()
|
val vm: MainViewModel = getActivityViewModel()
|
||||||
vm.importLegacySettings(this)
|
|
||||||
|
|
||||||
setContent {
|
setContent {
|
||||||
|
val launcher = rememberLauncherForActivityResult(
|
||||||
|
ActivityResultContracts.StartActivityForResult(),
|
||||||
|
onResult = vm::applyLegacySettings
|
||||||
|
)
|
||||||
val theme by vm.prefs.theme.getAsState()
|
val theme by vm.prefs.theme.getAsState()
|
||||||
val dynamicColor by vm.prefs.dynamicColor.getAsState()
|
val dynamicColor by vm.prefs.dynamicColor.getAsState()
|
||||||
|
|
||||||
|
EventEffect(vm.legacyImportActivityFlow) {
|
||||||
|
try {
|
||||||
|
launcher.launch(it)
|
||||||
|
} catch (_: ActivityNotFoundException) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ReVancedManagerTheme(
|
ReVancedManagerTheme(
|
||||||
darkTheme = theme == Theme.SYSTEM && isSystemInDarkTheme() || theme == Theme.DARK,
|
darkTheme = theme == Theme.SYSTEM && isSystemInDarkTheme() || theme == Theme.DARK,
|
||||||
dynamicColor = dynamicColor
|
dynamicColor = dynamicColor
|
||||||
|
@ -2,13 +2,10 @@ package app.revanced.manager.ui.viewmodel
|
|||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.content.ActivityNotFoundException
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.util.Base64
|
import android.util.Base64
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.activity.ComponentActivity
|
|
||||||
import androidx.activity.result.ActivityResult
|
import androidx.activity.result.ActivityResult
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import app.revanced.manager.R
|
import app.revanced.manager.R
|
||||||
@ -28,6 +25,7 @@ import kotlinx.coroutines.flow.first
|
|||||||
import kotlinx.coroutines.flow.receiveAsFlow
|
import kotlinx.coroutines.flow.receiveAsFlow
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
import kotlinx.serialization.SerializationException
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
|
|
||||||
class MainViewModel(
|
class MainViewModel(
|
||||||
@ -36,10 +34,13 @@ class MainViewModel(
|
|||||||
private val downloadedAppRepository: DownloadedAppRepository,
|
private val downloadedAppRepository: DownloadedAppRepository,
|
||||||
private val keystoreManager: KeystoreManager,
|
private val keystoreManager: KeystoreManager,
|
||||||
private val app: Application,
|
private val app: Application,
|
||||||
val prefs: PreferencesManager
|
val prefs: PreferencesManager,
|
||||||
|
private val json: Json
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
private val appSelectChannel = Channel<SelectedApp>()
|
private val appSelectChannel = Channel<SelectedApp>()
|
||||||
val appSelectFlow = appSelectChannel.receiveAsFlow()
|
val appSelectFlow = appSelectChannel.receiveAsFlow()
|
||||||
|
private val legacyImportActivityChannel = Channel<Intent>()
|
||||||
|
val legacyImportActivityFlow = legacyImportActivityChannel.receiveAsFlow()
|
||||||
|
|
||||||
private suspend fun suggestedVersion(packageName: String) =
|
private suspend fun suggestedVersion(packageName: String) =
|
||||||
patchBundleRepository.suggestedVersions.first()[packageName]
|
patchBundleRepository.suggestedVersions.first()[packageName]
|
||||||
@ -50,7 +51,8 @@ class MainViewModel(
|
|||||||
val suggestedVersion = suggestedVersion(app.packageName) ?: return null
|
val suggestedVersion = suggestedVersion(app.packageName) ?: return null
|
||||||
|
|
||||||
val downloadedApp =
|
val downloadedApp =
|
||||||
downloadedAppRepository.get(app.packageName, suggestedVersion, markUsed = true) ?: return null
|
downloadedAppRepository.get(app.packageName, suggestedVersion, markUsed = true)
|
||||||
|
?: return null
|
||||||
return SelectedApp.Local(
|
return SelectedApp.Local(
|
||||||
downloadedApp.packageName,
|
downloadedApp.packageName,
|
||||||
downloadedApp.version,
|
downloadedApp.version,
|
||||||
@ -67,42 +69,46 @@ class MainViewModel(
|
|||||||
selectApp(SelectedApp.Search(packageName, suggestedVersion(packageName)))
|
selectApp(SelectedApp.Search(packageName, suggestedVersion(packageName)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun importLegacySettings(componentActivity: ComponentActivity) {
|
init {
|
||||||
if (!prefs.firstLaunch.getBlocking()) return
|
viewModelScope.launch {
|
||||||
|
if (!prefs.firstLaunch.get()) return@launch
|
||||||
try {
|
legacyImportActivityChannel.send(Intent().apply {
|
||||||
val launcher = componentActivity.registerForActivityResult(
|
|
||||||
ActivityResultContracts.StartActivityForResult()
|
|
||||||
) { result: ActivityResult ->
|
|
||||||
if (result.resultCode == Activity.RESULT_OK) {
|
|
||||||
result.data?.getStringExtra("data")?.let {
|
|
||||||
applyLegacySettings(it)
|
|
||||||
} ?: app.toast(app.getString(R.string.legacy_import_failed))
|
|
||||||
} else {
|
|
||||||
app.toast(app.getString(R.string.legacy_import_failed))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val intent = Intent().apply {
|
|
||||||
setClassName(
|
setClassName(
|
||||||
"app.revanced.manager.flutter",
|
"app.revanced.manager.flutter",
|
||||||
"app.revanced.manager.flutter.ExportSettingsActivity"
|
"app.revanced.manager.flutter.ExportSettingsActivity"
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
|
|
||||||
launcher.launch(intent)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
if (e !is ActivityNotFoundException) {
|
|
||||||
app.toast(app.getString(R.string.legacy_import_failed))
|
|
||||||
Log.e(tag, "Failed to launch legacy import activity: $e")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun applyLegacySettings(data: String) = viewModelScope.launch {
|
fun applyLegacySettings(result: ActivityResult) {
|
||||||
val json = Json { ignoreUnknownKeys = true }
|
if (result.resultCode != Activity.RESULT_OK) {
|
||||||
val settings = json.decodeFromString<LegacySettings>(data)
|
app.toast(app.getString(R.string.legacy_import_failed))
|
||||||
|
Log.e(
|
||||||
|
tag,
|
||||||
|
"Got unknown result code while importing legacy settings: ${result.resultCode}"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val jsonStr = result.data?.getStringExtra("data")
|
||||||
|
if (jsonStr == null) {
|
||||||
|
app.toast(app.getString(R.string.legacy_import_failed))
|
||||||
|
Log.e(tag, "Legacy settings data is null")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val settings = try {
|
||||||
|
json.decodeFromString<LegacySettings>(jsonStr)
|
||||||
|
} catch (e: SerializationException) {
|
||||||
|
app.toast(app.getString(R.string.legacy_import_failed))
|
||||||
|
Log.e(tag, "Legacy settings data could not be deserialized", e)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
applyLegacySettings(settings)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun applyLegacySettings(settings: LegacySettings) = viewModelScope.launch {
|
||||||
settings.themeMode?.let { theme ->
|
settings.themeMode?.let { theme ->
|
||||||
val themeMap = mapOf(
|
val themeMap = mapOf(
|
||||||
0 to Theme.SYSTEM,
|
0 to Theme.SYSTEM,
|
||||||
@ -145,6 +151,7 @@ class MainViewModel(
|
|||||||
settings.patches?.let { selection ->
|
settings.patches?.let { selection ->
|
||||||
patchSelectionRepository.import(0, selection)
|
patchSelectionRepository.import(0, selection)
|
||||||
}
|
}
|
||||||
|
Log.d(tag, "Imported legacy settings")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
|
Loading…
x
Reference in New Issue
Block a user