Added settings

This commit is contained in:
Vadim Furman
2021-02-05 21:29:28 -08:00
parent 0ea40967ac
commit 2d81acbee5
4 changed files with 130 additions and 37 deletions

View File

@@ -102,6 +102,49 @@ class UIViewModel(app: Application) : AndroidViewModel(app), Logging {
val radioConfig = object : MutableLiveData<MeshProtos.RadioConfig?>(null) {
}
var positionBroadcastSecs: Int?
get() {
radioConfig.value?.preferences?.let {
if (it.locationShare == MeshProtos.LocationSharing.LocDisabled) return 0
if (it.positionBroadcastSecs > 0) return it.positionBroadcastSecs
// These default values are borrowed from the device code.
if (it.isRouter) return 60 * 60
return 15 * 60
}
return null
}
set(value) {
val config = radioConfig.value
if (value != null && config != null) {
val builder = config.toBuilder()
if (value > 0) {
builder.preferencesBuilder.positionBroadcastSecs = value
builder.preferencesBuilder.locationShare =
MeshProtos.LocationSharing.LocEnabled
} else
builder.preferencesBuilder.locationShare =
MeshProtos.LocationSharing.LocDisabled
setRadioConfig(builder.build())
}
}
var lsSleepSecs: Int?
get() {
radioConfig.value?.preferences?.let {
return it.lsSecs
}
return null
}
set(value) {
val config = radioConfig.value
if (value != null && config != null) {
val builder = config.toBuilder()
builder.preferencesBuilder.lsSecs = value
setRadioConfig(builder.build())
}
}
/// hardware info about our local device
val myNodeInfo = object : MutableLiveData<MyNodeInfo>(null) {}

View File

@@ -574,15 +574,30 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
/// Setup the ui widgets unrelated to BLE scanning
private fun initCommonUI() {
model.ownerName.observe(viewLifecycleOwner, Observer { name ->
model.ownerName.observe(viewLifecycleOwner, { name ->
binding.usernameEditText.setText(name)
})
model.radioConfig.observe(viewLifecycleOwner, { _ ->
var inMinutes = model.positionBroadcastSecs
if (inMinutes != null) {
inMinutes /= 60
}
binding.positionBroadcastPeriodEditText.setText(inMinutes.toString())
binding.lsSleepEditText.setText(model.lsSleepSecs.toString())
})
// Only let user edit their name or set software update while connected to a radio
model.isConnected.observe(viewLifecycleOwner, Observer { connected ->
binding.usernameView.isEnabled = connected == MeshService.ConnectionState.CONNECTED
if (connected == MeshService.ConnectionState.DISCONNECTED)
model.isConnected.observe(viewLifecycleOwner, Observer { connectionState ->
val connected = connectionState == MeshService.ConnectionState.CONNECTED
binding.usernameView.isEnabled = connected
binding.positionBroadcastPeriodView.isEnabled = connected
binding.lsSleepView.isEnabled = connected
if (connectionState == MeshService.ConnectionState.DISCONNECTED)
model.ownerName.value = ""
initNodeInfo()
})
@@ -600,30 +615,32 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
val n = binding.usernameEditText.text.toString().trim()
if (n.isNotEmpty())
model.setOwner(n)
requireActivity().hideKeyboard()
}
binding.positionBroadcastPeriodEditText.on(EditorInfo.IME_ACTION_DONE) {
val n = binding.positionBroadcastPeriodEditText.text.toString().toIntOrNull()
debug("did IME action, text = ${binding.positionBroadcastPeriodEditText.text.toString()}, int=$n")
val meshService = model.meshService
if (n != null && meshService != null) {
try {
var config: MeshProtos.RadioConfig =
MeshProtos.RadioConfig.parseFrom(meshService.getRadioConfig())
val builder : MeshProtos.RadioConfig.Builder = config.toBuilder()
builder.preferencesBuilder.setPositionBroadcastSecs(n * 60)
builder.preferencesBuilder.setLsSecs(n * 60)
config = builder.build()
debug("config=${config.toString()}")
meshService.setRadioConfig(config.toByteArray())
} catch (ex: RemoteException) {
errormsg("Can't change parameter, is device offline? ${ex.message}")
}
val str = binding.positionBroadcastPeriodEditText.text.toString()
var n = str.toIntOrNull()
if (n != null && n < 100000 && n >= 0) {
n *= 60
model.positionBroadcastSecs = n
} else {
binding.scanStatusText.text = "Bad value: $str"
}
requireActivity().hideKeyboard()
}
binding.lsSleepEditText.on(EditorInfo.IME_ACTION_DONE) {
val str = binding.lsSleepEditText.text.toString()
var n = str.toIntOrNull()
if (n != null && n < 100000 && n >= 0) {
model.lsSleepSecs = n
} else {
binding.scanStatusText.text = "Bad value: $str"
}
requireActivity().hideKeyboard()
}
val app = (requireContext().applicationContext as GeeksvilleApplication)
// Set analytics checkbox