Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Aug 21, 2024
1 parent d812530 commit d2db999
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 170 deletions.
30 changes: 12 additions & 18 deletions app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import org.andbootmgr.app.util.Terminal
import java.io.File
import java.io.IOException

class BackupRestoreWizardPageFactory(private val vm: WizardActivityState) {
fun get(): List<IWizardPage> {
val c = CreateBackupDataHolder(vm)
class BackupRestoreFlow(private val partitionId: Int): WizardFlow() {
override fun get(vm: WizardActivityState): List<IWizardPage> {
val c = CreateBackupDataHolder(vm, partitionId)
return listOf(WizardPage("start",
NavButton(vm.activity.getString(R.string.cancel)) { it.finish() },
NavButton("") {})
Expand All @@ -45,8 +45,7 @@ class BackupRestoreWizardPageFactory(private val vm: WizardActivityState) {
}
}

private class CreateBackupDataHolder(val vm: WizardActivityState) {
var pi: Int = -1
private class CreateBackupDataHolder(val vm: WizardActivityState, val pi: Int) {
var action: Int = 0
var path: Uri? = null
var meta: SDUtils.SDPartitionMeta? = null
Expand All @@ -56,7 +55,6 @@ private class CreateBackupDataHolder(val vm: WizardActivityState) {
private fun ChooseAction(c: CreateBackupDataHolder) {
LaunchedEffect(Unit) {
c.meta = SDUtils.generateMeta(c.vm.deviceInfo)
c.pi = c.vm.mvm.wizardCompatPid!!
}

Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center,
Expand Down Expand Up @@ -95,21 +93,17 @@ private fun SelectDroidBoot(c: CreateBackupDataHolder) {
else -> ""
}
)
val next = { it: Uri ->
c.path = it
nextButtonAvailable = true
c.vm.nextText = c.vm.activity.getString(R.string.next)
c.vm.onNext = { i -> i.navigate("go") }
}
Button(onClick = {
if (c.action != 1) {
c.vm.activity.chooseFile("*/*") {
c.path = it
nextButtonAvailable = true
c.vm.nextText = c.vm.activity.getString(R.string.next)
c.vm.onNext = { i -> i.navigate("go") }
}
c.vm.activity.chooseFile("*/*", next)
} else {
c.vm.activity.createFile("${c.meta!!.dumpKernelPartition(c.pi).name}.img") {
c.path = it
nextButtonAvailable = true
c.vm.nextText = c.vm.activity.getString(R.string.next)
c.vm.onNext = { i -> i.navigate("go") }
}
c.vm.activity.createFile("${c.meta!!.dumpKernelPartition(c.pi).name}.img", next)
}
}) {
Text(stringResource(if (c.action != 1) R.string.choose_file else R.string.create_file))
Expand Down
56 changes: 24 additions & 32 deletions app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ import java.math.BigDecimal
import java.net.URL
import java.util.concurrent.TimeUnit

class CreatePartWizardPageFactory(private val vm: WizardActivityState) {
fun get(): List<IWizardPage> {
val c = CreatePartDataHolder(vm)
class CreatePartFlow(private val desiredStartSector: Long): WizardFlow() {
override fun get(vm: WizardActivityState): List<IWizardPage> {
val c = CreatePartDataHolder(vm, desiredStartSector)
return listOf(WizardPage("start",
NavButton(vm.activity.getString(R.string.cancel)) { it.finish() },
NavButton("") {}
Expand Down Expand Up @@ -120,21 +120,20 @@ internal interface ProgressListener {
fun update(bytesRead: Long, contentLength: Long, done: Boolean)
}

private class CreatePartDataHolder(val vm: WizardActivityState): ProgressListener {
private class CreatePartDataHolder(val vm: WizardActivityState, private val desiredStartSector: Long): ProgressListener {

var meta: SDUtils.SDPartitionMeta? = null
lateinit var p: SDUtils.Partition.FreeSpace
lateinit var l: String
lateinit var u: String
var f = 0L
var t: String? = null
var noobMode: Boolean = false
var scriptInet: String? = null
var scriptShaInet: String? = null

var painter: @Composable (() -> Painter)? = null
var rtype by mutableStateOf("")
var cmdline by mutableStateOf("")
var rtype = ""
var cmdline = ""
val dmaMeta = ArrayMap<String, String>()
val count = mutableIntStateOf(0)
val intVals = mutableStateListOf<Long>()
Expand All @@ -156,8 +155,8 @@ private class CreatePartDataHolder(val vm: WizardActivityState): ProgressListene
}.build()
var pl: ProgressListener? = null
var cl: (() -> Unit)? = null
lateinit var t2: MutableState<String>
val t3 = mutableStateOf("")
var t2 by mutableStateOf("")
var t3 by mutableStateOf("")
var availableSize: Long = 0

override fun update(bytesRead: Long, contentLength: Long, done: Boolean) {
Expand All @@ -179,12 +178,9 @@ private class CreatePartDataHolder(val vm: WizardActivityState): ProgressListene
idNeeded.removeAll(idUnneeded)
}

@SuppressLint("ComposableNaming")
@Composable
fun lateInit() {
noobMode = LocalContext.current.getSharedPreferences("abm", 0).getBoolean("noob_mode", BuildConfig.DEFAULT_NOOB_MODE)
meta = SDUtils.generateMeta(vm.deviceInfo)
p = (meta?.s?.find { vm.mvm.wizardCompatSid == it.startSector } as SDUtils.Partition.FreeSpace?)!!
p = (meta?.s?.find { desiredStartSector == it.startSector } as SDUtils.Partition.FreeSpace?)!!
}

fun painterFromRtype(type: String): @Composable () -> Painter {
Expand Down Expand Up @@ -263,7 +259,7 @@ private fun Start(c: CreatePartDataHolder) {
}
}

if (remember { ctx.getSharedPreferences("abm", 0).getBoolean("noob_mode", BuildConfig.DEFAULT_NOOB_MODE) }) {
if (c.vm.mvm.noobMode) {
Card(
modifier = Modifier
.fillMaxWidth()
Expand Down Expand Up @@ -471,12 +467,14 @@ private fun Os(c: CreatePartDataHolder) {
a.sortWith(Comparator.comparingInt { c -> c.substring(3, c.length - 5).toInt() })
val b = if (a.size > 0) a.last().substring(3, a.last().length - 5).toInt() + 1 else 0
c.t2 = mutableStateOf("rom$b")
c.t3.value = c.dmaMeta["name"]!!
c.t3 = c.dmaMeta["name"]!!
}

val s = rememberScrollState()
var expanded by remember { mutableIntStateOf(0) }
var e by remember { mutableStateOf(false) }
val et2 by remember { derivedStateOf { !(c.t2.matches(Regex("\\A\\p{ASCII}*\\z"))) } }
val et3 by remember { derivedStateOf { !(c.t3.matches(Regex("\\A\\p{ASCII}*\\z"))) } }
val e = et2 || et3
Column(
Modifier
.fillMaxSize()
Expand All @@ -502,7 +500,7 @@ private fun Os(c: CreatePartDataHolder) {
}
}
}
if (!c.noobMode)
if (!c.vm.mvm.noobMode)
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.Center, modifier = Modifier
.fillMaxWidth()
.padding(5.dp)
Expand All @@ -516,32 +514,26 @@ private fun Os(c: CreatePartDataHolder) {
Icon(painterResource(id = R.drawable.ic_baseline_keyboard_arrow_down_24), stringResource(R.string.expand_content_desc))
}
}
if (expanded == 1 || c.noobMode) {
if (expanded == 1 || c.vm.mvm.noobMode) {
Column(
Modifier
.fillMaxWidth()
.padding(5.dp)
) {
var et2 by remember { mutableStateOf(false) }
var et3 by remember { mutableStateOf(false) }
if (!c.noobMode)
TextField(value = c.t2.value, onValueChange = {
c.t2.value = it
et2 = !(c.t2.value.matches(Regex("\\A\\p{ASCII}*\\z")))
e = et2 || et3
if (!c.vm.mvm.noobMode)
TextField(value = c.t2, onValueChange = {
c.t2 = it
}, isError = et2, label = {
Text(stringResource(R.string.internal_id))
})
TextField(value = c.t3.value, onValueChange = {
c.t3.value = it
et3 = !(c.t3.value.matches(Regex("\\A\\p{ASCII}*\\z")))
e = et2 || et3
TextField(value = c.t3, onValueChange = {
c.t3 = it
}, isError = et3, label = {
Text(stringResource(R.string.name_in_boot))
})
}
}
if (!c.noobMode)
if (!c.vm.mvm.noobMode)
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.Center, modifier = Modifier
.fillMaxWidth()
.padding(5.dp)
Expand Down Expand Up @@ -910,8 +902,8 @@ private fun Flash(c: CreatePartDataHolder) {
c.vm.logic.extractToolkit(terminal)
if (c.t == null) { // OS install
val parts = ArrayMap<Int, Int>()
val fn = c.t2.value
val gn = c.t3.value
val fn = c.t2
val gn = c.t3
terminal.add(vm.activity.getString(R.string.term_f_name, fn))
terminal.add(vm.activity.getString(R.string.term_g_name, gn))
val tmpFile = c.chosen["_install.sh_"]!!.toFile(vm)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ import java.io.File
import java.io.IOException
import java.net.URL

class DroidBootWizardPageFactory(private val vm: WizardActivityState) {
fun get(): List<IWizardPage> {
class DroidBootFlow() : WizardFlow() {
override fun get(vm: WizardActivityState): List<IWizardPage> {
return listOf(WizardPage("start",
NavButton(vm.activity.getString(R.string.cancel)) { it.finish() },
NavButton(vm.activity.getString(R.string.next)) { it.navigate("input") })
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/andbootmgr/app/FixDroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import org.andbootmgr.app.util.Terminal
import java.io.File
import java.io.IOException

class FixDroidBootWizardPageFactory(private val vm: WizardActivityState) {
fun get(): List<IWizardPage> {
class FixDroidBootFlow(): WizardFlow() {
override fun get(vm: WizardActivityState): List<IWizardPage> {
return listOf(WizardPage("start",
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") })
Expand Down
32 changes: 3 additions & 29 deletions app/src/main/java/org/andbootmgr/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
Expand All @@ -34,7 +33,6 @@ import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -68,36 +66,12 @@ import org.andbootmgr.app.themes.ThemeViewModel
import org.andbootmgr.app.themes.Themes
import org.andbootmgr.app.util.AbmTheme
import org.andbootmgr.app.util.ConfigFile
import org.andbootmgr.app.util.SDUtils
import org.andbootmgr.app.util.StayAliveService
import org.andbootmgr.app.util.Terminal
import java.util.concurrent.atomic.AtomicBoolean

class MainActivityState(val activity: MainActivity?) {
var wizardCompat by mutableStateOf<String?>(null)

fun startFlow(flow: String) {
wizardCompat = flow
}

var wizardCompatSid: Long? = null
fun startCreateFlow(freeSpace: SDUtils.Partition.FreeSpace) {
wizardCompatSid = freeSpace.startSector
startFlow("create_part")
}

var wizardCompatE: String? = null
fun startUpdateFlow(e: String) {
wizardCompatE = e
startFlow("update")
}

var wizardCompatPid: Int? = null
fun startBackupAndRestoreFlow(partition: SDUtils.Partition) {
wizardCompatPid = partition.id
startFlow("backup_restore")
}

var currentWizardFlow by mutableStateOf<WizardFlow?>(null)
var noobMode by mutableStateOf(false)
var deviceInfo: DeviceInfo? = null
val theme = ThemeViewModel(this)
Expand Down Expand Up @@ -284,8 +258,8 @@ class MainActivity : ComponentActivity() {
}
}
}
} else if (vm.wizardCompat != null) {
WizardCompat(vm, vm.wizardCompat!!)
} else if (vm.currentWizardFlow != null) {
WizardCompat(vm, vm.currentWizardFlow!!)
} else {
val navController = rememberNavController()
AppContent(vm, navController) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/andbootmgr/app/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fun Settings(vm: MainActivityState) {
Text(stringResource(R.string.save_changes))
}
Button(onClick = {
vm.startFlow("update_droidboot")
vm.currentWizardFlow = UpdateDroidBootFlow()
}) {
Text(stringResource(R.string.update_droidboot))
}
Expand Down
19 changes: 13 additions & 6 deletions app/src/main/java/org/andbootmgr/app/Start.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,15 @@ fun Start(vm: MainActivityState) {
} else if (metaOnSd && !sdPresent) {
Text(stringResource(R.string.need_sd), textAlign = TextAlign.Center)
} else if (!installed && !mounted) {
Button(onClick = { vm.startFlow("droidboot") }) {
Button(onClick = {
vm.currentWizardFlow = DroidBootFlow()
}) {
Text(stringResource(if (metaOnSd) R.string.setup_sd else R.string.install))
}
} else if (!booted && mounted) {
Text(stringResource(R.string.installed_not_booted), textAlign = TextAlign.Center)
Button(onClick = {
vm.startFlow("fix_droidboot")
vm.currentWizardFlow = FixDroidBootFlow()
}) {
Text(stringResource(R.string.repair_droidboot))
}
Expand Down Expand Up @@ -462,11 +464,15 @@ private fun PartTool(vm: MainActivityState) {
}
}
}
Button(onClick = { vm.startBackupAndRestoreFlow(p) }) {
Button(onClick = {
vm.currentWizardFlow = BackupRestoreFlow(p.id)
}) {
Text(stringResource(R.string.backupnrestore))
}
} else {
Button(onClick = { vm.startCreateFlow(p as SDUtils.Partition.FreeSpace) }) {
Button(onClick = {
vm.currentWizardFlow = CreatePartFlow(p.startSector)
}) {
Text(stringResource(R.string.create))
}
}
Expand Down Expand Up @@ -715,8 +721,9 @@ private fun PartTool(vm: MainActivityState) {
Column(Modifier.verticalScroll(rememberScrollState())) {
Button(
onClick = {
if (e.has("xupdate") && !e["xupdate"].isNullOrBlank())
vm.startUpdateFlow(entries!![e]!!.absolutePath)
if (e.has("xupdate") && !e["xupdate"].isNullOrBlank()) {
vm.currentWizardFlow = UpdateFlow(entries!![e]!!.name)
}
}, enabled = e.has("xupdate") && !e["xupdate"].isNullOrBlank()) {
Text(stringResource(R.string.update))
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/andbootmgr/app/UpdateDroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import org.andbootmgr.app.util.Terminal
import java.io.File
import java.io.IOException

class UpdateDroidBootWizardPageFactory(private val vm: WizardActivityState) {
fun get(): List<IWizardPage> {
class UpdateDroidBootFlow(): WizardFlow() {
override fun get(vm: WizardActivityState): List<IWizardPage> {
return listOf(WizardPage("start",
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") })
Expand Down
Loading

0 comments on commit d2db999

Please sign in to comment.