Skip to content

Commit 922e21b

Browse files
committed
Update: Replace unsafe string context menu action with enum
1 parent f5bbde9 commit 922e21b

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/main/java/com/machiav3lli/backup/Constants.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ const val PREFS_BACKUP_FILE = "${ADMIN_PREFIX}app.preferences"
5454
const val PROP_NAME = "properties"
5555
const val LOG_INSTANCE = "%s.log.txt"
5656

57+
enum class MenuAction { PUT, GET, DEL }
58+
5759
// optional millisec to include old format
5860
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+"""
5961

src/main/java/com/machiav3lli/backup/ui/compose/item/MainPackageItem.kt

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import androidx.compose.ui.unit.dp
4949
import coil.ImageLoader
5050
import com.machiav3lli.backup.MODE_ALL
5151
import com.machiav3lli.backup.MODE_UNSET
52+
import com.machiav3lli.backup.MenuAction
5253
import com.machiav3lli.backup.OABX
5354
import com.machiav3lli.backup.OABX.Companion.addInfoLogText
5455
import com.machiav3lli.backup.OABX.Companion.beginBusy
@@ -228,7 +229,7 @@ fun TextInputMenuItem(
228229

229230
@Composable
230231
fun Selections(
231-
action: String,
232+
action: MenuAction,
232233
selection: List<String> = emptyList(),
233234
onAction: (List<String>) -> Unit = {},
234235
) {
@@ -253,17 +254,17 @@ fun Selections(
253254
text = { Text(" $name") },
254255
onClick = {
255256
when (action) {
256-
"get" -> {
257+
MenuAction.GET -> {
257258
val newSelection = file.readText().lines()
258259
onAction(newSelection)
259260
}
260261

261-
"put" -> {
262+
MenuAction.PUT -> {
262263
file.writeText(selection.joinToString("\n"))
263264
onAction(selection)
264265
}
265266

266-
"del" -> {
267+
MenuAction.DEL -> {
267268
file.delete()
268269
onAction(selection)
269270
}
@@ -274,7 +275,7 @@ fun Selections(
274275
}
275276
}
276277

277-
if (action in listOf("get", "put")) {
278+
if (action != MenuAction.DEL) {
278279
val scheduleDao = OABX.db.getScheduleDao()
279280
val schedules = OABX.main?.viewModel?.schedules?.value ?: emptyList()
280281
if (schedules.isEmpty())
@@ -293,19 +294,21 @@ fun Selections(
293294
text = { Text(" $name") },
294295
onClick = {
295296
when (action) {
296-
"get" -> {
297+
MenuAction.GET -> {
297298
val newSelection = schedule.customList.toList()
298299
onAction(newSelection)
299300
}
300301

301-
"put" -> {
302+
MenuAction.PUT -> {
302303
Thread {
303304
scheduleDao.update(
304305
schedule.copy(customList = selection.toSet())
305306
)
306307
}.start()
307308
onAction(selection)
308309
}
310+
311+
else -> {}
309312
}
310313
}
311314
)
@@ -321,19 +324,21 @@ fun Selections(
321324
text = { Text(" $name") },
322325
onClick = {
323326
when (action) {
324-
"get" -> {
327+
MenuAction.GET -> {
325328
val newSelection = schedule.blockList.toList()
326329
onAction(newSelection)
327330
}
328331

329-
"put" -> {
332+
MenuAction.PUT -> {
330333
Thread {
331334
scheduleDao.update(
332335
schedule.copy(blockList = selection.toSet())
333336
)
334337
}.start()
335338
onAction(selection)
336339
}
340+
341+
else -> {}
337342
}
338343
}
339344
)
@@ -348,17 +353,19 @@ fun Selections(
348353
text = { Text(" blocklist") },
349354
onClick = {
350355
when (action) {
351-
"get" -> {
356+
MenuAction.GET -> {
352357
val newSelection =
353358
OABX.main?.viewModel?.getBlocklist()
354359
?: emptyList()
355360
onAction(newSelection)
356361
}
357362

358-
"put" -> {
363+
MenuAction.PUT -> {
359364
OABX.main?.viewModel?.setBlocklist(selection.toSet())
360365
onAction(selection)
361366
}
367+
368+
else -> {}
362369
}
363370
}
364371
)
@@ -369,7 +376,7 @@ fun Selections(
369376
fun SelectionGetMenu(
370377
onAction: (List<String>) -> Unit = {},
371378
) {
372-
Selections(action = "get", onAction = onAction)
379+
Selections(action = MenuAction.GET, onAction = onAction)
373380
}
374381

375382
@Composable
@@ -392,14 +399,14 @@ fun SelectionPutMenu(
392399
onAction()
393400
}
394401

395-
Selections(action = "put", selection = selection) { onAction() }
402+
Selections(action = MenuAction.PUT, selection = selection) { onAction() }
396403
}
397404

398405
@Composable
399406
fun SelectionRemoveMenu(
400407
onAction: () -> Unit = {},
401408
) {
402-
Selections(action = "del") { onAction() }
409+
Selections(action = MenuAction.DEL) { onAction() }
403410
}
404411

405412
fun openSubMenu(

0 commit comments

Comments
 (0)