Skip to content
Merged
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
53 changes: 43 additions & 10 deletions app/src/main/java/com/rodrig20/isodroid/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,23 @@ fun App() {
// While a turn on/off script runs, lock the toggle with a
// progress bar so taps feel answered immediately.
var isToggling by remember { mutableStateOf(false) }

// Enables the gadget and surfaces failures in the UI. Shared by the
// gadget switch and the auto-enable done when mounting the first item.
suspend fun enableGadget(): Boolean {
val result = rootManager.turnOnApp()
if (result.startsWith("Error")) {
snackbarHostState.showSnackbar(result)
return false
}
if (result.contains("waiting-host")) {
snackbarHostState.showSnackbar(
"Gadget on, but the PC has not enumerated it yet (plug the cable or rescan)"
)
}
return true
}

when (currentScreen) {
is Screen.Home -> HomeScreen(
isAppEnabled = isAppEnabled,
Expand All @@ -253,14 +270,7 @@ fun App() {
coroutineScope.launch {
try {
if (enabled) {
val result = rootManager.turnOnApp()
if (result.startsWith("Error")) {
snackbarHostState.showSnackbar(result)
} else if (result.contains("waiting-host")) {
snackbarHostState.showSnackbar(
"Gadget on, but the PC has not enumerated it yet (plug the cable or rescan)"
)
}
enableGadget()
} else {
val result = rootManager.turnOffApp()
if (result.startsWith("Error")) {
Expand Down Expand Up @@ -289,6 +299,19 @@ fun App() {
}
}
},
onEnsureGadgetEnabled = {
if (isToggling) {
false
} else {
isToggling = true
try {
IsoLog.i("auto enabling gadget before first mount")
enableGadget()
} finally {
isToggling = false
}
}
},
rootManager = rootManager,
onNavigateToSettings = { currentScreen = Screen.Settings }
)
Expand Down Expand Up @@ -337,7 +360,9 @@ fun HomeScreen(
rootManager: RootManager, // Manager for root operations
onNavigateToSettings: () -> Unit, // Callback for navigating to settings
snackbarHostState: SnackbarHostState = remember { SnackbarHostState() },
isToggling: Boolean = false // A turn on/off script is running
isToggling: Boolean = false, // A turn on/off script is running
// Turns the gadget on when a mount needs it (false = failed).
onEnsureGadgetEnabled: suspend () -> Boolean = { false }
) {
val context = LocalContext.current
val diskItemRepository = remember { DiskItemRepository(context) }
Expand Down Expand Up @@ -408,6 +433,12 @@ fun HomeScreen(
coroutineScope.launch {
var updatedItem: DiskItem
if (newItemState) {
// Mounting needs a bound gadget, so bring it up
// first when it is still off.
if (!isAppEnabled && !onEnsureGadgetEnabled()) {
// enableGadget already reported the failure.
return@launch
}
// Mount the item if the new state is active
val result = rootManager.mountItem(
item.path ?: "",
Expand Down Expand Up @@ -496,7 +527,9 @@ fun HomeScreen(
Switch(
checked = item.isActive,
onCheckedChange = { onToggle(it) },
enabled = isAppEnabled
// Enabled even with the gadget off: mounting
// turns the gadget on automatically.
enabled = !isToggling
)
}
}
Expand Down
Loading