Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ private fun LoadedContent(
),
onButtonClick = onRootButtonClick,
enabled = state.isNotificationPermissionGranted,
isLoading = state.isStarting,
isLoading =
state.isStarting &&
state.startingMethod == SystemBridgeStartMethod.ROOT,
)

Spacer(modifier = Modifier.height(8.dp))
Expand Down Expand Up @@ -389,7 +391,9 @@ private fun LoadedContent(
buttonText = shizukuButtonText,
onButtonClick = onShizukuButtonClick,
enabled = state.isNotificationPermissionGranted,
isLoading = state.isStarting,
isLoading =
state.isStarting &&
state.startingMethod == SystemBridgeStartMethod.SHIZUKU,
)
}

Expand Down Expand Up @@ -422,7 +426,9 @@ private fun LoadedContent(
enabled =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R &&
state.isNotificationPermissionGranted,
isLoading = state.isStarting,
isLoading =
state.isStarting &&
state.startingMethod == SystemBridgeStartMethod.ADB,
)
}
}
Expand Down Expand Up @@ -744,6 +750,7 @@ private fun Preview() {
isRootGranted = false,
shizukuSetupState = ShizukuSetupState.PERMISSION_GRANTED,
isNotificationPermissionGranted = true,
startingMethod = null,
isStarting = false,
),
),
Expand Down Expand Up @@ -823,6 +830,7 @@ private fun PreviewNotificationPermissionNotGranted() {
isRootGranted = true,
shizukuSetupState = ShizukuSetupState.PERMISSION_GRANTED,
isNotificationPermissionGranted = false,
startingMethod = null,
isStarting = false,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ data class ExpertModeSetupState(
val stepContent: StepContent,
val isSetupAssistantChecked: Boolean,
val isSetupAssistantButtonEnabled: Boolean,
val isStarting: Boolean,
val startingMethod: SystemBridgeStartMethod?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import javax.inject.Inject
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
Expand All @@ -42,16 +43,27 @@ class ExpertModeViewModel @Inject constructor(
private const val WARNING_COUNT_DOWN_SECONDS = 5
}

private val startingMethod =
MutableStateFlow<SystemBridgeStartMethod?>(null)

init {
viewModelScope.launch {
useCase.isSystemBridgeConnected.collect { connected ->
if (connected) {
startingMethod.value = null
}
}
}
}

@OptIn(ExperimentalCoroutinesApi::class)
val warningState: StateFlow<ExpertModeWarningState> =
useCase.isWarningUnderstood
.flatMapLatest { isUnderstood -> createWarningStateFlow(isUnderstood) }
.stateIn(
viewModelScope,
SharingStarted.Eagerly,
ExpertModeWarningState.CountingDown(
WARNING_COUNT_DOWN_SECONDS,
),
ExpertModeWarningState.CountingDown(WARNING_COUNT_DOWN_SECONDS),
)

val setupState: StateFlow<State<ExpertModeState>> =
Expand All @@ -60,9 +72,22 @@ class ExpertModeViewModel @Inject constructor(
useCase.isRootGranted,
useCase.shizukuSetupState,
useCase.isNotificationPermissionGranted,
startingMethod,
useCase.isSystemBridgeStarting,
::buildSetupState,
).stateIn(viewModelScope, SharingStarted.Eagerly, State.Loading)
) { connected, rootGranted, shizukuState, notifGranted, method, isStarting ->
buildSetupState(
connected,
rootGranted,
shizukuState,
notifGranted,
method,
isStarting,
)
}.stateIn(
viewModelScope,
SharingStarted.Eagerly,
State.Loading,
)

val autoStartBootEnabled: StateFlow<Boolean> =
useCase.isAutoStartBootEnabled
Expand All @@ -73,8 +98,6 @@ class ExpertModeViewModel @Inject constructor(

fun hideInfoCard() {
showInfoCard = false
// Save that they've dismissed the card so it is not shown by default the next
// time they visit the PRO mode page.
useCase.dismissInfo()
}

Expand All @@ -88,10 +111,13 @@ class ExpertModeViewModel @Inject constructor(
} else {
flow {
repeat(WARNING_COUNT_DOWN_SECONDS) {
emit(ExpertModeWarningState.CountingDown(WARNING_COUNT_DOWN_SECONDS - it))
emit(
ExpertModeWarningState.CountingDown(
WARNING_COUNT_DOWN_SECONDS - it,
),
)
delay(1000L)
}

emit(ExpertModeWarningState.Idle)
}
}
Expand All @@ -105,26 +131,18 @@ class ExpertModeViewModel @Inject constructor(
}

fun onRootButtonClick() {
startingMethod.value = SystemBridgeStartMethod.ROOT
useCase.startSystemBridgeWithRoot()
}

fun onShizukuButtonClick() {
viewModelScope.launch {
val shizukuState = useCase.shizukuSetupState.first()
when (shizukuState) {
ShizukuSetupState.NOT_FOUND -> {
// Do nothing
}

ShizukuSetupState.INSTALLED -> {
useCase.openShizukuApp()
}

ShizukuSetupState.STARTED -> {
useCase.requestShizukuPermission()
}

when (useCase.shizukuSetupState.first()) {
ShizukuSetupState.NOT_FOUND -> Unit
ShizukuSetupState.INSTALLED -> useCase.openShizukuApp()
ShizukuSetupState.STARTED -> useCase.requestShizukuPermission()
ShizukuSetupState.PERMISSION_GRANTED -> {
startingMethod.value = SystemBridgeStartMethod.SHIZUKU
useCase.startSystemBridgeWithShizuku()
}
}
Expand All @@ -139,7 +157,10 @@ class ExpertModeViewModel @Inject constructor(

fun onSetupWithKeyMapperClick() {
viewModelScope.launch {
navigate("setup_expert_mode_with_key_mapper", NavDestination.ExpertModeSetup)
navigate(
"setup_expert_mode_with_key_mapper",
NavDestination.ExpertModeSetup,
)
}
}

Expand All @@ -156,22 +177,24 @@ class ExpertModeViewModel @Inject constructor(
isRootGranted: Boolean,
shizukuSetupState: ShizukuSetupState,
isNotificationPermissionGranted: Boolean,
startingMethod: SystemBridgeStartMethod?,
isSystemBridgeStarting: Boolean,
): State<ExpertModeState> {
if (isSystemBridgeConnected) {
return State.Data(
return if (isSystemBridgeConnected) {
State.Data(
ExpertModeState.Started(
isDefaultUsbModeCompatible =
useCase.isCompatibleUsbModeSelected().valueOrNull() ?: false,
useCase.isCompatibleUsbModeSelected().valueOrNull() ?: false,
),
)
} else {
return State.Data(
State.Data(
ExpertModeState.Stopped(
isRootGranted = isRootGranted,
shizukuSetupState = shizukuSetupState,
isNotificationPermissionGranted = isNotificationPermissionGranted,
isStarting = isSystemBridgeStarting,
startingMethod = startingMethod,
isStarting = startingMethod != null || isSystemBridgeStarting,
),
)
}
Expand All @@ -189,8 +212,11 @@ sealed class ExpertModeState {
val isRootGranted: Boolean,
val shizukuSetupState: ShizukuSetupState,
val isNotificationPermissionGranted: Boolean,
val startingMethod: SystemBridgeStartMethod?,
val isStarting: Boolean,
) : ExpertModeState()

data class Started(val isDefaultUsbModeCompatible: Boolean) : ExpertModeState()
data class Started(
val isDefaultUsbModeCompatible: Boolean,
) : ExpertModeState()
}