Skip to content

Commit

Permalink
make some code a little more compose-y
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Aug 4, 2024
1 parent 7e6ebf8 commit c9c27b1
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 135 deletions.
45 changes: 22 additions & 23 deletions app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
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.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
Expand Down Expand Up @@ -52,8 +55,10 @@ private class CreateBackupDataHolder(val vm: WizardActivityState){

@Composable
private fun ChooseAction(c: CreateBackupDataHolder) {
c.meta = remember { SDUtils.generateMeta(c.vm.deviceInfo!!) }
c.pi = remember { c.vm.activity.intent.getIntExtra("partitionid", -1) }
LaunchedEffect(Unit) {
c.meta = SDUtils.generateMeta(c.vm.deviceInfo)
c.pi = c.vm.activity.intent.getIntExtra("partitionid", -1)
}

Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxSize()
Expand All @@ -73,48 +78,42 @@ private fun ChooseAction(c: CreateBackupDataHolder) {

@Composable
private fun SelectDroidBoot(c: CreateBackupDataHolder) {
val nextButtonAvailable = remember { mutableStateOf(false) }
var nextButtonAvailable by remember { mutableStateOf(false) }

Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxSize()
) {
if (nextButtonAvailable.value) {
if (nextButtonAvailable) {
Text(stringResource(R.string.successfully_selected))
c.vm.nextText.value = stringResource(R.string.next)
c.vm.onNext.value = { it.navigate("go") }
} else {
Text(
if (c.action == 1)
stringResource(R.string.make_backup)
else if (c.action == 2)
stringResource(R.string.restore_backup)
else if (c.action == 3)
stringResource(R.string.restore_backup_sparse)
else
""
when (c.action) {
1 -> stringResource(R.string.make_backup)
2 -> stringResource(R.string.restore_backup)
3 -> stringResource(R.string.restore_backup_sparse)
else -> ""
}
)
Button(onClick = {
if (c.action != 1) {
c.vm.activity.chooseFile("*/*") {
c.path = it
nextButtonAvailable.value = true
nextButtonAvailable = true
c.vm.nextText.value = c.vm.activity.getString(R.string.next)
c.vm.onNext.value = { i -> i.navigate("go") }
}
} else {
c.vm.activity.createFile("${c.meta!!.dumpKernelPartition(c.pi).name}.img") {
c.path = it
nextButtonAvailable.value = true
nextButtonAvailable = true
c.vm.nextText.value = c.vm.activity.getString(R.string.next)
c.vm.onNext.value = { i -> i.navigate("go") }
}
}
}) {
Text(
if (c.action != 1) {
stringResource(R.string.choose_file)
} else {
stringResource(R.string.create_file)
}
)
Text(stringResource(if (c.action != 1) R.string.choose_file else R.string.create_file))
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private fun Start(vm: WizardActivityState) {
) {
Text(stringResource(R.string.welcome_text))
Text(
if (vm.deviceInfo.isBooted(vm.logic)) {
if (remember { vm.deviceInfo.isBooted(vm.logic) }) {
stringResource(R.string.install_abm)
} else {
stringResource(R.string.install_abm_dboot)
Expand Down Expand Up @@ -116,7 +116,6 @@ private fun Input(vm: WizardActivityState) {
@Composable
fun SelectDroidBoot(vm: WizardActivityState) {
val nextButtonAvailable = remember { mutableStateOf(false) }
val flashType = "DroidBootFlashType"

Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxSize()
Expand All @@ -135,7 +134,7 @@ fun SelectDroidBoot(vm: WizardActivityState) {
Text(stringResource(R.string.choose_droidboot_online))
Button(onClick = {
vm.activity.chooseFile("*/*") {
vm.flashes[flashType] = Pair(it, null)
vm.flashes["DroidBootFlashType"] = Pair(it, null)
nextButtonAvailable.value = true
}
}) {
Expand All @@ -153,7 +152,7 @@ fun SelectDroidBoot(vm: WizardActivityState) {
val bl = json.getJSONObject("bootloader")
val url = bl.getString("url")
val sha = if (bl.has("sha256")) bl.getString("sha256") else null
vm.flashes[flashType] = Pair(Uri.parse(url), sha)
vm.flashes["DroidBootFlashType"] = Pair(Uri.parse(url), sha)
nextButtonAvailable.value = true
} catch (e: Exception) {
Handler(Looper.getMainLooper()).post {
Expand Down
13 changes: 8 additions & 5 deletions app/src/main/java/org/andbootmgr/app/FixDroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.topjohnwu.superuser.io.SuFile
import com.topjohnwu.superuser.io.SuFileInputStream
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.andbootmgr.app.util.AbmTheme
import java.io.File
import java.io.IOException
Expand All @@ -21,7 +25,7 @@ class FixDroidBootWizardPageFactory(private val vm: WizardActivityState) {
NavButton(vm.activity.getString(R.string.cancel)) { it.finish() },
NavButton(vm.activity.getString(R.string.next)) { it.navigate(if (vm.deviceInfo.postInstallScript) "shSel" else "select") })
{
Start(vm)
Start()
}, WizardPage("shSel",
NavButton(vm.activity.getString(R.string.prev)) { it.navigate("start") },
NavButton("") {}
Expand All @@ -42,7 +46,7 @@ class FixDroidBootWizardPageFactory(private val vm: WizardActivityState) {
}

@Composable
private fun Start(vm: WizardActivityState) {
private fun Start() {
Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxSize()
) {
Expand All @@ -53,7 +57,6 @@ private fun Start(vm: WizardActivityState) {

@Composable
private fun Flash(vm: WizardActivityState) {
val flashType = "DroidBootFlashType"
Terminal(vm, logFile = "blfix_${System.currentTimeMillis()}.txt") { terminal ->
val tmpFile = if (vm.deviceInfo.postInstallScript) {
val tmpFile = createTempFileSu("abm", ".sh", vm.logic.rootTmpDir)
Expand All @@ -67,7 +70,7 @@ private fun Flash(vm: WizardActivityState) {
terminal.add(vm.activity.getString(R.string.term_cant_write_bl))
vm.copyPriv(SuFileInputStream.open(vm.deviceInfo.blBlock), File(vm.logic.fileDir, "backup_lk.img"))
try {
vm.copyPriv(vm.flashStream(flashType), File(vm.deviceInfo.blBlock))
vm.copyPriv(vm.flashStream("DroidBootFlashType"), File(vm.deviceInfo.blBlock))
} catch (e: IOException) {
terminal.add(vm.activity.getString(R.string.term_bl_failed))
terminal.add(if (e.message != null) e.message!! else "(null)")
Expand All @@ -83,7 +86,7 @@ private fun Flash(vm: WizardActivityState) {
tmpFile.delete()
}
terminal.add(vm.activity.getString(R.string.term_success))
vm.activity.runOnUiThread {
withContext(Dispatchers.Main) {
vm.btnsOverride = true
vm.nextText.value = vm.activity.getString(R.string.finish)
vm.onNext.value = {
Expand Down
77 changes: 9 additions & 68 deletions app/src/main/java/org/andbootmgr/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class MainActivityState {
var isOk = false
var logic: DeviceLogic? = null

fun loadDefaultCfg() {
private fun loadDefaultCfg() {
CoroutineScope(Dispatchers.IO).launch {
val cfg = ConfigFile.importFromFile(logic!!.abmDbConf).toMap()
withContext(Dispatchers.Main) {
Expand All @@ -110,10 +110,10 @@ class MainActivityState {
}
}

fun editDefaultCfg(changes: Map<String, String?>) {
suspend fun editDefaultCfg(changes: Map<String, String?>) {
val changesSafe = changes.toMutableMap() // multi threading may mean parameter is edited
if (!logic!!.mounted) throw IllegalStateException("bootset not mounted")
CoroutineScope(Dispatchers.Main).launch {
withContext(Dispatchers.Main) {
changesSafe.forEach { (t, u) ->
if (u != null) defaultCfg[t] = u else defaultCfg.remove(t)
}
Expand Down Expand Up @@ -141,6 +141,11 @@ class MainActivityState {
defaultCfg.clear()
logic!!.unmountBootset()
}

fun remountBootset() {
logic!!.unmountBootset()
logic!!.mountBootset(deviceInfo!!)
}
}


Expand Down Expand Up @@ -266,7 +271,7 @@ fun AppContent(vm: MainActivityState, view: @Composable (PaddingValues) -> Unit)
val scope = vm.scope!!
var fabhint by remember { mutableStateOf(false) }
val fab = @Composable {
if (vm.noobMode && vm.currentNav == "start") {
if (vm.noobMode && vm.isOk && vm.currentNav == "start") {
FloatingActionButton(onClick = { fabhint = true }) {
Icon(Icons.Default.Add, stringResource(R.string.add_icon_content_desc))
}
Expand Down Expand Up @@ -1143,70 +1148,6 @@ private fun PartTool(vm: MainActivityState) {
}
}

@Composable
private fun Settings(vm: MainActivityState) {
val ctx = LocalContext.current
val changes = remember { mutableStateMapOf<String, String>() }
val defaultText = changes["default"] ?: vm.defaultCfg["default"] ?: "Entry 01"
val defaultErr by remember { derivedStateOf { defaultText.isBlank() || !defaultText.matches(Regex("[\\dA-Za-z ]+")) } }
val timeoutText = changes["timeout"] ?: vm.defaultCfg["timeout"] ?: "20"
val timeoutErr by remember { derivedStateOf { timeoutText.isBlank() || !timeoutText.matches(Regex("\\d+")) } }
Column {
TextField(
value = defaultText,
onValueChange = {
changes["default"] = it
},
label = { Text(stringResource(R.string.default_entry)) },
isError = defaultErr
)
if (defaultErr) {
Text(stringResource(id = R.string.invalid_in), color = MaterialTheme.colorScheme.error)
} else {
Text("") // Budget spacer
}
TextField(
value = timeoutText,
onValueChange = {
changes["timeout"] = it
},
label = { Text(stringResource(R.string.timeout_secs)) },
isError = timeoutErr
)
if (timeoutErr) {
Text(stringResource(id = R.string.invalid_in), color = MaterialTheme.colorScheme.error)
} else {
Text("") // Budget spacer
}
Button(onClick = {
if (defaultErr || timeoutErr)
Toast.makeText(vm.activity!!, vm.activity!!.getString(R.string.invalid_in), Toast.LENGTH_LONG).show()
else
vm.editDefaultCfg(changes)
}, enabled = !(defaultErr || timeoutErr)) {
Text(stringResource(R.string.save_changes))
}
Button(onClick = {
vm.startFlow("update_droidboot")
}) {
Text(stringResource(R.string.update_droidboot))
}
Button(onClick = {
vm.unmountBootset()
vm.activity!!.finish()
}) {
Text(stringResource(R.string.umount))
}
Row(horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {
Text(stringResource(R.string.noob_mode))
Switch(checked = vm.noobMode, onCheckedChange = {
vm.noobMode = it
ctx.getSharedPreferences("abm", 0).edit().putBoolean("noob_mode", it).apply()
})
}
}
}

@Preview(showBackground = true)
@Composable
private fun Preview() {
Expand Down
Loading

0 comments on commit c9c27b1

Please sign in to comment.