@@ -15,6 +15,8 @@ import androidx.lifecycle.viewModelScope
1515import androidx.lifecycle.viewmodel.compose.SavedStateHandleSaveableApi
1616import androidx.lifecycle.viewmodel.compose.saveable
1717import app.revanced.manager.R
18+ import app.revanced.manager.domain.bundles.PatchBundleSource
19+ import app.revanced.manager.domain.bundles.PatchBundleSource.State
1820import app.revanced.manager.domain.manager.PreferencesManager
1921import app.revanced.manager.domain.repository.PatchBundleRepository
2022import app.revanced.manager.patcher.patch.PatchBundleInfo
@@ -37,6 +39,7 @@ import kotlinx.collections.immutable.toPersistentMap
3739import kotlinx.collections.immutable.toPersistentSet
3840import kotlinx.coroutines.CoroutineStart
3941import kotlinx.coroutines.async
42+ import kotlinx.coroutines.flow.combine
4043import kotlinx.coroutines.flow.first
4144import kotlinx.coroutines.flow.flow
4245import kotlinx.coroutines.flow.map
@@ -65,11 +68,34 @@ class PatchesSelectorViewModel(input: SelectedApplicationInfo.PatchesSelector.Vi
6568 val allowIncompatiblePatches =
6669 get<PreferencesManager >().disablePatchVersionCompatCheck.getBlocking() || appVersion == null
6770 val bundlesFlow = if (browseAllBundles) {
68- bundleRepository.bundleInfoFlow.map { bundles ->
69- bundles.values.map(PatchBundleInfo .Global ::asReadonlyScoped)
71+ combine(bundleRepository.sources, bundleRepository.bundleInfoFlow) { sources, bundles ->
72+ mergeSourcesWithBundleInfo(
73+ sources,
74+ bundles.mapValues { (_, bundle) -> bundle.asReadonlyScoped() }
75+ )
7076 }
7177 } else {
72- bundleRepository.scopedBundleInfoFlow(packageName, input.app.version)
78+ combine(
79+ bundleRepository.sources,
80+ bundleRepository.scopedBundleInfoFlow(packageName, input.app.version)
81+ ) { sources, bundles ->
82+ mergeSourcesWithBundleInfo(
83+ sources,
84+ bundles.associateBy(PatchBundleInfo .Scoped ::uid)
85+ )
86+ }
87+ }
88+
89+ val bundleLoadIssuesFlow = bundleRepository.sources.map { sources ->
90+ sources.mapNotNull { source ->
91+ val messageId = when {
92+ source.error != null -> R .string.patches_error_description
93+ source.state is State .Missing -> R .string.patches_not_downloaded
94+ else -> null
95+ } ? : return @mapNotNull null
96+
97+ source.uid to messageId
98+ }.toMap()
7399 }
74100
75101 init {
@@ -306,6 +332,13 @@ class PatchesSelectorViewModel(input: SelectedApplicationInfo.PatchesSelector.Vi
306332 private val selectionSaver: Saver <PersistentPatchSelection ?, Nullable <PatchSelection >> =
307333 nullableSaver(persistentMapSaver(valueSaver = persistentSetSaver()))
308334 }
335+
336+ private fun mergeSourcesWithBundleInfo (
337+ sources : List <PatchBundleSource >,
338+ scopedBundleInfoByUid : Map <Int , PatchBundleInfo .Scoped >
339+ ) = sources.map { source ->
340+ scopedBundleInfoByUid[source.uid] ? : source.emptyScopedBundleInfo()
341+ }
309342}
310343
311344// Versions of other types, but utilizing persistent/observable collection types.
@@ -324,3 +357,13 @@ private fun PatchBundleInfo.Global.asReadonlyScoped() = PatchBundleInfo.Scoped(
324357 incompatible = emptyList(),
325358 universal = emptyList()
326359)
360+
361+ private fun PatchBundleSource.emptyScopedBundleInfo () = PatchBundleInfo .Scoped (
362+ name = name,
363+ version = version,
364+ uid = uid,
365+ patches = emptyList(),
366+ compatible = emptyList(),
367+ incompatible = emptyList(),
368+ universal = emptyList()
369+ )
0 commit comments