From 5aefb3bc59bf95ea284b976538b8bd92a5e4bd99 Mon Sep 17 00:00:00 2001 From: Benjamin <73490201+BenjaminHalko@users.noreply.github.com> Date: Mon, 16 Oct 2023 12:48:51 -0700 Subject: [PATCH] fix: hide patch button (#1284) --- .../ui/screen/PatchesSelectorScreen.kt | 41 +++++++++++++------ .../ui/viewmodel/PatchesSelectorViewModel.kt | 14 +++++++ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/app/revanced/manager/ui/screen/PatchesSelectorScreen.kt b/app/src/main/java/app/revanced/manager/ui/screen/PatchesSelectorScreen.kt index 47119f4b36..4ac41b063e 100644 --- a/app/src/main/java/app/revanced/manager/ui/screen/PatchesSelectorScreen.kt +++ b/app/src/main/java/app/revanced/manager/ui/screen/PatchesSelectorScreen.kt @@ -44,6 +44,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue @@ -56,6 +57,7 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.window.Dialog import androidx.compose.ui.window.DialogProperties import androidx.lifecycle.compose.collectAsStateWithLifecycle +import androidx.lifecycle.viewModelScope import app.revanced.manager.R import app.revanced.manager.domain.manager.PreferencesManager import app.revanced.manager.patcher.patch.PatchInfo @@ -91,7 +93,10 @@ fun PatchesSelectorScreen( mutableStateOf(null) } var showBottomSheet by rememberSaveable { mutableStateOf(false) } - + var showPatchButton by remember { mutableStateOf(true) } + LaunchedEffect(Unit) { + showPatchButton = vm.isSelectionNotEmpty() + } if (showBottomSheet) { ModalBottomSheet( onDismissRequest = { @@ -222,10 +227,17 @@ fun PatchesSelectorScreen( if (vm.selectionWarningEnabled) { vm.pendingSelectionAction = { vm.togglePatch(uid, patch) + vm.viewModelScope.launch { + showPatchButton = vm.isSelectionNotEmpty() + } } } else { vm.togglePatch(uid, patch) + vm.viewModelScope.launch { + showPatchButton = vm.isSelectionNotEmpty() + } } + }, supported = supported ) @@ -296,6 +308,7 @@ fun PatchesSelectorScreen( } } + Scaffold( topBar = { AppTopBar( @@ -319,18 +332,22 @@ fun PatchesSelectorScreen( ) }, floatingActionButton = { - ExtendedFloatingActionButton( - text = { Text(stringResource(R.string.patch)) }, - icon = { Icon(Icons.Default.Build, null) }, - onClick = { - // TODO: only allow this if all required options have been set. - composableScope.launch { - val selection = vm.getSelection() - vm.saveSelection(selection).join() - onPatchClick(selection, vm.getOptions()) + if(showPatchButton) { + ExtendedFloatingActionButton( + text = { + Text(stringResource(R.string.patch)) + }, + icon = { Icon(Icons.Default.Build, null) }, + onClick = { + // TODO: only allow this if all required options have been set. + composableScope.launch { + val selection = vm.getSelection() + vm.saveSelection(selection).join() + onPatchClick(selection, vm.getOptions()) + } } - } - ) + ) + } } ) { paddingValues -> Column( diff --git a/app/src/main/java/app/revanced/manager/ui/viewmodel/PatchesSelectorViewModel.kt b/app/src/main/java/app/revanced/manager/ui/viewmodel/PatchesSelectorViewModel.kt index 036bc6133a..8e4f7246c1 100644 --- a/app/src/main/java/app/revanced/manager/ui/viewmodel/PatchesSelectorViewModel.kt +++ b/app/src/main/java/app/revanced/manager/ui/viewmodel/PatchesSelectorViewModel.kt @@ -175,6 +175,20 @@ class PatchesSelectorViewModel( } } + private suspend fun patchesAvailable(bundle: BundleInfo): List { + val patches = (bundle.supported + bundle.universal).toMutableList() + val removeUnsupported = !allowExperimental.get() + if (!removeUnsupported) patches += bundle.unsupported + return patches + } + + suspend fun isSelectionNotEmpty() = + bundlesFlow.first().any { bundle -> + patchesAvailable(bundle).any { patch -> + isSelected(bundle.uid, patch) + } + } + private fun getOrCreateSelection(bundle: Int) = explicitPatchesSelection.getOrPut(bundle, ::mutableStateMapOf)