From 93d4cd99fdf91d33882681f9cbeadd0ce63b269e Mon Sep 17 00:00:00 2001 From: geeksville Date: Wed, 12 Aug 2020 12:31:37 -0700 Subject: [PATCH] show channel suffix per https://github.com/meshtastic/Meshtastic-device/issues/269 --- .../main/java/com/geeksville/mesh/model/Channel.kt | 12 ++++++++++++ .../java/com/geeksville/mesh/ui/ChannelFragment.kt | 11 ++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/model/Channel.kt b/app/src/main/java/com/geeksville/mesh/model/Channel.kt index 05735ad42..745fe2853 100644 --- a/app/src/main/java/com/geeksville/mesh/model/Channel.kt +++ b/app/src/main/java/com/geeksville/mesh/model/Channel.kt @@ -8,6 +8,7 @@ import com.google.zxing.BarcodeFormat import com.google.zxing.MultiFormatWriter import com.journeyapps.barcodescanner.BarcodeEncoder import java.net.MalformedURLException +import kotlin.experimental.xor /** Utility function to make it easy to declare byte arrays - FIXME move someplace better */ fun byteArrayOfInts(vararg ints: Int) = ByteArray(ints.size) { pos -> ints[pos].toByte() } @@ -52,6 +53,17 @@ data class Channel( val name: String get() = settings.name val modemConfig: MeshProtos.ChannelSettings.ModemConfig get() = settings.modemConfig + /** + * Return a name that is formatted as #channename-suffix + * + * Where suffix indicates the hash of the PSK + */ + val humanName: String + get() { + val code = settings.psk.fold(0.toByte(), { acc, x -> acc xor x }) + return "#${settings.name}-${'A' + (code % 26)}" + } + /// Can this channel be changed right now? var editable = false diff --git a/app/src/main/java/com/geeksville/mesh/ui/ChannelFragment.kt b/app/src/main/java/com/geeksville/mesh/ui/ChannelFragment.kt index a17d978f1..63b9b28ee 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/ChannelFragment.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/ChannelFragment.kt @@ -77,7 +77,7 @@ class ChannelFragment : ScreenFragment("Channel"), Logging { if (channel != null) { qrView.visibility = View.VISIBLE channelNameEdit.visibility = View.VISIBLE - channelNameEdit.setText(channel.name) + channelNameEdit.setText(channel.humanName) // For now, we only let the user edit/save channels while the radio is awake - because the service // doesn't cache radioconfig writes. @@ -134,7 +134,12 @@ class ChannelFragment : ScreenFragment("Channel"), Logging { } editableCheckbox.setOnCheckedChangeListener { _, checked -> - if (!checked) { + if (checked) { + // User just unlocked for editing - remove the # goo around the channel name + UIViewModel.getChannel(model.radioConfig.value)?.let { channel -> + channelNameEdit.setText(channel.name) + } + } else { // User just locked it, we should warn and then apply changes to radio MaterialAlertDialogBuilder(requireContext()) .setTitle(R.string.change_channel) @@ -177,7 +182,7 @@ class ChannelFragment : ScreenFragment("Channel"), Logging { // Since we are writing to radioconfig, that will trigger the rest of the GUI update (QR code etc) } catch (ex: RemoteException) { errormsg("ignoring channel problem", ex) - + setGUIfromModel() // Throw away user edits // Tell the user to try again