add switches to advanced settings

This commit is contained in:
andrekir 2022-01-24 18:23:09 -03:00
parent cfb699753a
commit 11e6332d5d
3 changed files with 80 additions and 24 deletions

View file

@ -21,7 +21,6 @@ import com.geeksville.mesh.database.entity.Packet
import com.geeksville.mesh.service.MeshService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlin.math.max
/// Given a human name, strip out the first letter of the first three words and return that as the initials for
/// that user. If the original name is only one word, strip vowels from the original name and if the result is
@ -36,7 +35,7 @@ fun getInitials(nameIn: String): String {
val initials = when (words.size) {
in 0..minchars - 1 -> {
val nm = if (name.length >= 1)
name.first() + name.drop(1).filterNot { c -> c.toLowerCase() in "aeiou" }
name.first() + name.drop(1).filterNot { c -> c.lowercase() in "aeiou" }
else
""
if (nm.length >= nchars) nm else name
@ -98,7 +97,6 @@ class UIViewModel(private val app: Application) : AndroidViewModel(app), Logging
var positionBroadcastSecs: Int?
get() {
radioConfig.value?.preferences?.let {
if (it.locationShare == RadioConfigProtos.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
@ -110,17 +108,7 @@ class UIViewModel(private val app: Application) : AndroidViewModel(app), Logging
val config = radioConfig.value
if (value != null && config != null) {
val builder = config.toBuilder()
if (value > 0) {
builder.preferencesBuilder.positionBroadcastSecs = value
builder.preferencesBuilder.gpsUpdateInterval = value
builder.preferencesBuilder.sendOwnerInterval = max(1, 3600 / value).toInt()
builder.preferencesBuilder.locationShare =
RadioConfigProtos.LocationSharing.LocEnabled
} else {
builder.preferencesBuilder.positionBroadcastSecs = Int.MAX_VALUE
builder.preferencesBuilder.locationShare =
RadioConfigProtos.LocationSharing.LocDisabled
}
builder.preferencesBuilder.positionBroadcastSecs = value
setRadioConfig(builder.build())
}
}
@ -136,6 +124,36 @@ class UIViewModel(private val app: Application) : AndroidViewModel(app), Logging
}
}
var locationShare: Boolean?
get() {
return radioConfig.value?.preferences?.locationShare == RadioConfigProtos.LocationSharing.LocEnabled
}
set(value) {
val config = radioConfig.value
if (value != null && config != null) {
val builder = config.toBuilder()
if (value == true) {
builder.preferencesBuilder.locationShare =
RadioConfigProtos.LocationSharing.LocEnabled
} else {
builder.preferencesBuilder.locationShare =
RadioConfigProtos.LocationSharing.LocDisabled
}
setRadioConfig(builder.build())
}
}
var isPowerSaving: Boolean?
get() = radioConfig.value?.preferences?.isPowerSaving
set(value) {
val config = radioConfig.value
if (value != null && config != null) {
val builder = config.toBuilder()
builder.preferencesBuilder.isPowerSaving = value
setRadioConfig(builder.build())
}
}
var isAlwaysPowered: Boolean?
get() = radioConfig.value?.preferences?.isAlwaysPowered
set(value) {

View file

@ -36,17 +36,22 @@ class AdvancedSettingsFragment : ScreenFragment("Advanced Settings"), Logging {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
model.radioConfig.observe(viewLifecycleOwner, { _ ->
model.radioConfig.observe(viewLifecycleOwner, {
binding.positionBroadcastPeriodEditText.setText(model.positionBroadcastSecs.toString())
binding.lsSleepEditText.setText(model.lsSleepSecs.toString())
binding.isAlwaysPoweredCheckbox.isChecked = model.isAlwaysPowered?: false
binding.positionBroadcastSwitch.isChecked = model.locationShare ?: true
binding.lsSleepView.isEnabled = model.isPowerSaving ?: false
binding.lsSleepSwitch.isChecked = model.isPowerSaving ?: false
binding.isAlwaysPoweredSwitch.isChecked = model.isAlwaysPowered ?: false
})
model.isConnected.observe(viewLifecycleOwner, Observer { connectionState ->
model.isConnected.observe(viewLifecycleOwner, { connectionState ->
val connected = connectionState == MeshService.ConnectionState.CONNECTED
binding.positionBroadcastPeriodView.isEnabled = connected
binding.lsSleepView.isEnabled = connected
binding.isAlwaysPoweredCheckbox.isEnabled = connected
binding.lsSleepView.isEnabled = connected && model.isPowerSaving ?: false
binding.positionBroadcastSwitch.isEnabled = connected
binding.lsSleepSwitch.isEnabled = connected
binding.isAlwaysPoweredSwitch.isEnabled = connected
})
binding.positionBroadcastPeriodEditText.on(EditorInfo.IME_ACTION_DONE) {
@ -74,6 +79,14 @@ class AdvancedSettingsFragment : ScreenFragment("Advanced Settings"), Logging {
requireActivity().hideKeyboard()
}
binding.positionBroadcastSwitch.setOnCheckedChangeListener { view, isChecked ->
if (view.isPressed) {
model.locationShare = isChecked
debug("User changed locationShare to $isChecked")
}
}
// TODO - disable all sleep settings for non-ESP32 devices
binding.lsSleepEditText.on(EditorInfo.IME_ACTION_DONE) {
val str = binding.lsSleepEditText.text.toString()
val n = str.toIntOrNull()
@ -87,7 +100,14 @@ class AdvancedSettingsFragment : ScreenFragment("Advanced Settings"), Logging {
requireActivity().hideKeyboard()
}
binding.isAlwaysPoweredCheckbox.setOnCheckedChangeListener { view, isChecked ->
binding.lsSleepSwitch.setOnCheckedChangeListener { view, isChecked ->
if (view.isPressed) {
model.isPowerSaving = isChecked
debug("User changed isPowerSaving to $isChecked")
}
}
binding.isAlwaysPoweredSwitch.setOnCheckedChangeListener { view, isChecked ->
if (view.isPressed) {
model.isAlwaysPowered = isChecked
debug("User changed isAlwaysPowered to $isChecked")

View file

@ -27,6 +27,15 @@
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/positionBroadcastSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="@id/positionBroadcastPeriodView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/positionBroadcastPeriodView" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/lsSleepView"
android:layout_width="0dp"
@ -48,15 +57,24 @@
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
<CheckBox
android:id="@+id/isAlwaysPoweredCheckbox"
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/lsSleepSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="@id/lsSleepView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/lsSleepView" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/isAlwaysPoweredSwitch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="isAlwaysPowered"
android:text="is_always_powered"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintStart_toStartOf="@+id/lsSleepView"
app:layout_constraintTop_toBottomOf="@+id/lsSleepView" />
</androidx.constraintlayout.widget.ConstraintLayout>