diff --git a/app/src/main/cpp/droidboot_gui b/app/src/main/cpp/droidboot_gui index ba6da4f9..b0c0b94c 160000 --- a/app/src/main/cpp/droidboot_gui +++ b/app/src/main/cpp/droidboot_gui @@ -1 +1 @@ -Subproject commit ba6da4f93b31567db31d5f448fbe18e3a4559fa4 +Subproject commit b0c0b94c86a557e18d6b3cd518fdfe429b561bda diff --git a/app/src/main/java/org/andbootmgr/app/MainActivity.kt b/app/src/main/java/org/andbootmgr/app/MainActivity.kt index af1b176d..a6636e14 100644 --- a/app/src/main/java/org/andbootmgr/app/MainActivity.kt +++ b/app/src/main/java/org/andbootmgr/app/MainActivity.kt @@ -213,7 +213,7 @@ class MainActivity : ComponentActivity() { @OptIn(ExperimentalMaterial3Api::class) @Composable -private fun AppContent(vm: MainActivityState, view: @Composable (PaddingValues) -> Unit) { +fun AppContent(vm: MainActivityState, view: @Composable (PaddingValues) -> Unit) { val drawerState = vm.drawerState!! val scope = vm.scope!! var fabhint by remember { mutableStateOf(false) } diff --git a/app/src/main/java/org/andbootmgr/app/Themes.kt b/app/src/main/java/org/andbootmgr/app/Themes.kt index 60281881..04f75301 100644 --- a/app/src/main/java/org/andbootmgr/app/Themes.kt +++ b/app/src/main/java/org/andbootmgr/app/Themes.kt @@ -1,10 +1,31 @@ package org.andbootmgr.app import android.content.Intent +import android.widget.Toast +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.padding import androidx.compose.material3.Button +import androidx.compose.material3.DrawerValue +import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text +import androidx.compose.material3.TextField +import androidx.compose.material3.rememberDrawerState import androidx.compose.runtime.Composable +import androidx.compose.runtime.MutableState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateMapOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue +import androidx.compose.runtime.snapshots.SnapshotStateMap +import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource +import androidx.compose.ui.tooling.preview.Preview +import androidx.navigation.compose.rememberNavController +import org.andbootmgr.app.util.ConfigFile +import java.io.File +import java.util.Random /* uint32_t win_bg_color; @@ -28,6 +49,36 @@ import androidx.compose.ui.res.stringResource */ @Composable fun Themes(vm: MainActivityState) { + val c = remember { + try { + if (vm.logic == null) + throw ActionAbortedCleanlyError(Exception("Compose preview special-casing")) + ConfigFile.importFromFile(File(vm.logic!!.abmDb, "db.conf")) + } catch (e: ActionAbortedCleanlyError) { + if (vm.activity != null) // Compose preview special-casing + Toast.makeText(vm.activity, vm.activity!!.getString(R.string.malformed_dbcfg), Toast.LENGTH_LONG).show() + ConfigFile().also { + it["default"] = "Entry 01" + it["timeout"] = "5" + } + } + } + val e = remember { mutableStateMapOf() } + ConfigTextField(c, e, "COLOR", "win_bg_color", "hmm") + Button(onClick = { + if (e.containsValue(false)) + Toast.makeText(vm.activity!!, vm.activity!!.getString(R.string.invalid_in), Toast.LENGTH_LONG).show() + else { + try { + c.exportToFile(File(vm.logic!!.abmDb, "db.conf")) + } catch (e: ActionAbortedError) { + Toast.makeText(vm.activity!!, vm.activity!!.getString(R.string.failed2save), Toast.LENGTH_LONG) + .show() + } + } + }, enabled = !e.containsValue(false)) { + Text(stringResource(R.string.save_changes)) + } Button(onClick = { vm.activity!!.startActivity(Intent(vm.activity!!, Simulator::class.java).apply { putExtra("sdCardBlock", vm.deviceInfo!!.bdev) @@ -35,4 +86,42 @@ fun Themes(vm: MainActivityState) { }) { Text(text = stringResource(id = R.string.simulator)) } +} + +@Composable +private fun ConfigTextField(c: ConfigFile, e: SnapshotStateMap, text: String, configKey: String, default: String = "") { + val id = remember { Random().nextLong() } + var value by remember { mutableStateOf(c[configKey] ?: default) } + TextField( + value = value, + onValueChange = { + value = it + c[configKey] = it.trim() + e[id] = true + }, + label = { Text(text) }, + isError = !(e[id] ?: true) + ) + if (e[id] == false) { + Text(stringResource(id = R.string.invalid_in), color = MaterialTheme.colorScheme.error) + } else { + Text("") // Budget spacer + } +} + +@Preview +@Composable +fun ThemePreview() { + val vm = MainActivityState() + val navController = rememberNavController() + val drawerState = rememberDrawerState(DrawerValue.Closed) + val scope = rememberCoroutineScope() + vm.navController = navController + vm.drawerState = drawerState + vm.scope = scope + AppContent(vm) { + Box(modifier = Modifier.padding(it)) { + Themes(vm) + } + } } \ No newline at end of file