Skip to content

Commit

Permalink
Update: Replace unsafe string context menu action with enum
Browse files Browse the repository at this point in the history
  • Loading branch information
machiav3lli committed Oct 26, 2024
1 parent f5bbde9 commit 922e21b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/machiav3lli/backup/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const val PREFS_BACKUP_FILE = "${ADMIN_PREFIX}app.preferences"
const val PROP_NAME = "properties"
const val LOG_INSTANCE = "%s.log.txt"

enum class MenuAction { PUT, GET, DEL }

// optional millisec to include old format
const val BACKUP_INSTANCE_REGEX_PATTERN = """\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d(-\d\d\d)?-user_\d+"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import androidx.compose.ui.unit.dp
import coil.ImageLoader
import com.machiav3lli.backup.MODE_ALL
import com.machiav3lli.backup.MODE_UNSET
import com.machiav3lli.backup.MenuAction
import com.machiav3lli.backup.OABX
import com.machiav3lli.backup.OABX.Companion.addInfoLogText
import com.machiav3lli.backup.OABX.Companion.beginBusy
Expand Down Expand Up @@ -228,7 +229,7 @@ fun TextInputMenuItem(

@Composable
fun Selections(
action: String,
action: MenuAction,
selection: List<String> = emptyList(),
onAction: (List<String>) -> Unit = {},
) {
Expand All @@ -253,17 +254,17 @@ fun Selections(
text = { Text(" $name") },
onClick = {
when (action) {
"get" -> {
MenuAction.GET -> {
val newSelection = file.readText().lines()
onAction(newSelection)
}

"put" -> {
MenuAction.PUT -> {
file.writeText(selection.joinToString("\n"))
onAction(selection)
}

"del" -> {
MenuAction.DEL -> {
file.delete()
onAction(selection)
}
Expand All @@ -274,7 +275,7 @@ fun Selections(
}
}

if (action in listOf("get", "put")) {
if (action != MenuAction.DEL) {
val scheduleDao = OABX.db.getScheduleDao()
val schedules = OABX.main?.viewModel?.schedules?.value ?: emptyList()
if (schedules.isEmpty())
Expand All @@ -293,19 +294,21 @@ fun Selections(
text = { Text(" $name") },
onClick = {
when (action) {
"get" -> {
MenuAction.GET -> {
val newSelection = schedule.customList.toList()
onAction(newSelection)
}

"put" -> {
MenuAction.PUT -> {
Thread {
scheduleDao.update(
schedule.copy(customList = selection.toSet())
)
}.start()
onAction(selection)
}

else -> {}
}
}
)
Expand All @@ -321,19 +324,21 @@ fun Selections(
text = { Text(" $name") },
onClick = {
when (action) {
"get" -> {
MenuAction.GET -> {
val newSelection = schedule.blockList.toList()
onAction(newSelection)
}

"put" -> {
MenuAction.PUT -> {
Thread {
scheduleDao.update(
schedule.copy(blockList = selection.toSet())
)
}.start()
onAction(selection)
}

else -> {}
}
}
)
Expand All @@ -348,17 +353,19 @@ fun Selections(
text = { Text(" blocklist") },
onClick = {
when (action) {
"get" -> {
MenuAction.GET -> {
val newSelection =
OABX.main?.viewModel?.getBlocklist()
?: emptyList()
onAction(newSelection)
}

"put" -> {
MenuAction.PUT -> {
OABX.main?.viewModel?.setBlocklist(selection.toSet())
onAction(selection)
}

else -> {}
}
}
)
Expand All @@ -369,7 +376,7 @@ fun Selections(
fun SelectionGetMenu(
onAction: (List<String>) -> Unit = {},
) {
Selections(action = "get", onAction = onAction)
Selections(action = MenuAction.GET, onAction = onAction)
}

@Composable
Expand All @@ -392,14 +399,14 @@ fun SelectionPutMenu(
onAction()
}

Selections(action = "put", selection = selection) { onAction() }
Selections(action = MenuAction.PUT, selection = selection) { onAction() }
}

@Composable
fun SelectionRemoveMenu(
onAction: () -> Unit = {},
) {
Selections(action = "del") { onAction() }
Selections(action = MenuAction.DEL) { onAction() }
}

fun openSubMenu(
Expand Down

0 comments on commit 922e21b

Please sign in to comment.