refactor: consolidate QR code scanning methods

This commit is contained in:
andrekir 2024-11-20 18:40:24 -03:00 committed by Andre K
parent 75003bb6f0
commit f73d909cd0
44 changed files with 89 additions and 226 deletions

View file

@ -85,7 +85,6 @@ import com.geeksville.mesh.service.MeshService
import com.geeksville.mesh.ui.components.AdaptiveTwoPane
import com.geeksville.mesh.ui.components.DropDownPreference
import com.geeksville.mesh.ui.components.PreferenceFooter
import com.geeksville.mesh.ui.components.ScannedQrCodeDialog
import com.geeksville.mesh.ui.components.config.ChannelCard
import com.geeksville.mesh.ui.components.config.ChannelSelection
import com.geeksville.mesh.ui.components.config.EditChannelDialog
@ -147,11 +146,10 @@ fun ChannelScreen(
val channelUrl = channelSet.getChannelUrl()
val modemPresetName = Channel(loraConfig = channelSet.loraConfig).name
var scannedChannelSet by remember { mutableStateOf<ChannelSet?>(null) }
val barcodeLauncher = rememberLauncherForActivityResult(ScanContract()) { result ->
if (result.contents != null) {
try {
scannedChannelSet = Uri.parse(result.contents).toChannelSet()
viewModel.requestChannelSet(Uri.parse(result.contents).toChannelSet())
} catch (ex: Throwable) {
errormsg("Channel url error: ${ex.message}")
viewModel.showSnackbar(R.string.channel_invalid)
@ -266,17 +264,6 @@ fun ChannelScreen(
.show()
}
if (scannedChannelSet != null) {
val incoming = scannedChannelSet ?: return
/* Prompt the user to modify channels after scanning a QR code. */
ScannedQrCodeDialog(
channels = channels,
incoming = incoming,
onDismiss = { scannedChannelSet = null },
onConfirm = { newChannelSet -> installSettings(newChannelSet) }
)
}
var showEditChannelDialog: Int? by remember { mutableStateOf(null) }
if (showEditChannelDialog != null) {
@ -375,7 +362,7 @@ fun ChannelScreen(
IconButton(onClick = {
when {
isError -> valueState = channelUrl
!isUrlEqual -> viewModel.setRequestChannelUrl(channelUrl)
!isUrlEqual -> viewModel.requestChannelSet(channels)
else -> {
// track how many times users share channels
GeeksvilleApplication.analytics.track(

View file

@ -51,13 +51,16 @@ fun ScannedQrCodeDialog(
onDismiss: () -> Unit,
onConfirm: (ChannelSet) -> Unit
) {
var shouldReplace by remember { mutableStateOf(true) }
var shouldReplace by remember { mutableStateOf(incoming.hasLoraConfig()) }
val channelSet = remember(shouldReplace) {
if (shouldReplace) {
incoming
} else {
channels.copy {
// To guarantee consistent ordering, using a LinkedHashSet which iterates through
// it's entries according to the order an item was *first* inserted.
// https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-linked-hash-set/
val result = LinkedHashSet(settings + incoming.settingsList)
settings.clear()
settings.addAll(result)
@ -134,6 +137,7 @@ fun ScannedQrCodeDialog(
modifier = Modifier
.height(48.dp)
.weight(1f),
enabled = incoming.hasLoraConfig(),
colors = if (shouldReplace) selectedColors else unselectedColors,
) { Text(text = stringResource(R.string.replace)) }
}