convert channel builders to dsl

This commit is contained in:
andrekir 2022-09-16 18:17:47 -03:00
parent 68558e4078
commit 5612d7d7c9
2 changed files with 25 additions and 24 deletions

View file

@ -1,7 +1,9 @@
package com.geeksville.mesh.model package com.geeksville.mesh.model
import com.geeksville.mesh.ChannelProtos import com.geeksville.mesh.ChannelProtos
import com.geeksville.mesh.ConfigKt.loRaConfig
import com.geeksville.mesh.ConfigProtos import com.geeksville.mesh.ConfigProtos
import com.geeksville.mesh.channelSettings
import com.google.protobuf.ByteString import com.google.protobuf.ByteString
/** Utility function to make it easy to declare byte arrays - FIXME move someplace better */ /** Utility function to make it easy to declare byte arrays - FIXME move someplace better */
@ -25,12 +27,8 @@ data class Channel(
// The default channel that devices ship with // The default channel that devices ship with
val default = Channel( val default = Channel(
ChannelProtos.ChannelSettings.newBuilder() channelSettings { psk = ByteString.copyFrom(defaultPSK) },
.setPsk(ByteString.copyFrom(defaultPSK)) loRaConfig { modemPreset = ConfigProtos.Config.LoRaConfig.ModemPreset.LongFast }
.build(),
ConfigProtos.Config.LoRaConfig.newBuilder()
.setModemPreset(ConfigProtos.Config.LoRaConfig.ModemPreset.LongFast)
.build()
) )
} }

View file

@ -20,12 +20,14 @@ import com.geeksville.mesh.analytics.DataPair
import com.geeksville.mesh.android.GeeksvilleApplication import com.geeksville.mesh.android.GeeksvilleApplication
import com.geeksville.mesh.android.Logging import com.geeksville.mesh.android.Logging
import com.geeksville.mesh.android.hideKeyboard import com.geeksville.mesh.android.hideKeyboard
import com.geeksville.mesh.AppOnlyProtos
import com.geeksville.mesh.ChannelProtos import com.geeksville.mesh.ChannelProtos
import com.geeksville.mesh.ConfigKt.loRaConfig
import com.geeksville.mesh.ConfigProtos import com.geeksville.mesh.ConfigProtos
import com.geeksville.mesh.R import com.geeksville.mesh.R
import com.geeksville.mesh.android.getCameraPermissions import com.geeksville.mesh.android.getCameraPermissions
import com.geeksville.mesh.android.hasCameraPermission import com.geeksville.mesh.android.hasCameraPermission
import com.geeksville.mesh.channelSet
import com.geeksville.mesh.copy
import com.geeksville.mesh.databinding.ChannelFragmentBinding import com.geeksville.mesh.databinding.ChannelFragmentBinding
import com.geeksville.mesh.model.Channel import com.geeksville.mesh.model.Channel
import com.geeksville.mesh.model.ChannelOption import com.geeksville.mesh.model.ChannelOption
@ -173,13 +175,11 @@ class ChannelFragment : ScreenFragment("Channel"), Logging {
newChannel: ChannelProtos.ChannelSettings, newChannel: ChannelProtos.ChannelSettings,
newLoRaConfig: ConfigProtos.Config.LoRaConfig newLoRaConfig: ConfigProtos.Config.LoRaConfig
) { ) {
val newSet = val newSet = ChannelSet(
ChannelSet( channelSet {
AppOnlyProtos.ChannelSet.newBuilder() settings[0] = newChannel
.addSettings(newChannel) loraConfig = newLoRaConfig
.setLoraConfig(newLoRaConfig) })
.build()
)
// Try to change the radio, if it fails, tell the user why and throw away their edits // Try to change the radio, if it fails, tell the user why and throw away their edits
try { try {
model.setChannels(newSet) model.setChannels(newSet)
@ -251,7 +251,7 @@ class ChannelFragment : ScreenFragment("Channel"), Logging {
debug("Switching back to default channel") debug("Switching back to default channel")
installSettings( installSettings(
Channel.default.settings, Channel.default.settings,
Channel.default.loraConfig.toBuilder().setRegion(model.region).build() Channel.default.loraConfig.copy { region = model.region }
) )
} }
.show() .show()
@ -280,7 +280,7 @@ class ChannelFragment : ScreenFragment("Channel"), Logging {
// User just locked it, we should warn and then apply changes to radio // User just locked it, we should warn and then apply changes to radio
model.channels.value.primaryChannel?.let { oldPrimary -> model.channels.value.primaryChannel?.let { oldPrimary ->
var newSettings = oldPrimary.settings.toBuilder() var newSettings = oldPrimary.settings
val newName = binding.channelNameEdit.text.toString().trim() val newName = binding.channelNameEdit.text.toString().trim()
// Find the new modem config // Find the new modem config
@ -300,19 +300,22 @@ class ChannelFragment : ScreenFragment("Channel"), Logging {
val random = SecureRandom() val random = SecureRandom()
val bytes = ByteArray(32) val bytes = ByteArray(32)
random.nextBytes(bytes) random.nextBytes(bytes)
newSettings.name = newName.take(11) // proto max_size:12 newSettings.copy {
newSettings.psk = ByteString.copyFrom(bytes) name = newName.take(11) // proto max_size:12
psk = ByteString.copyFrom(bytes)
}
} else { } else {
debug("Switching back to default channel") debug("Switching back to default channel")
newSettings = Channel.default.settings.toBuilder() newSettings = Channel.default.settings
} }
// No matter what apply the speed selection from the user // No matter what apply the speed selection from the user
val newLoRaConfig = ConfigProtos.Config.LoRaConfig.newBuilder() val newLoRaConfig = loRaConfig {
.setRegion(model.region) region = model.region
.setModemPreset(newModemPreset) modemPreset = newModemPreset
}
val humanName = Channel(newSettings.build(), newLoRaConfig.build()).humanName val humanName = Channel(newSettings, newLoRaConfig).humanName
binding.channelNameEdit.setText(humanName) binding.channelNameEdit.setText(humanName)
val message = buildString { val message = buildString {
@ -330,7 +333,7 @@ class ChannelFragment : ScreenFragment("Channel"), Logging {
.setPositiveButton(getString(R.string.accept)) { _, _ -> .setPositiveButton(getString(R.string.accept)) { _, _ ->
// Generate a new channel with only the changes the user can change in the GUI // Generate a new channel with only the changes the user can change in the GUI
installSettings(newSettings.build(), newLoRaConfig.build()) installSettings(newSettings, newLoRaConfig)
} }
.show() .show()
} }