Moved broadcast and ls_sleep in advanced settings

This commit is contained in:
Vadim Furman 2021-02-13 21:35:57 -08:00
parent 380ca1017f
commit 757c3867f2
6 changed files with 147 additions and 84 deletions

View file

@ -52,6 +52,7 @@ import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.tasks.Task
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.tabs.TabLayoutMediator
import com.google.protobuf.InvalidProtocolBufferException
import com.vorlonsoft.android.rate.AppRate
@ -978,6 +979,15 @@ class MainActivity : AppCompatActivity(), Logging,
handler.removeCallbacksAndMessages(null)
return true
}
R.id.advanced_settings -> {
val fragmentManager: FragmentManager = supportFragmentManager
val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
val nameFragment = AdvancedSettingsFragment()
fragmentTransaction.add(R.id.mainActivityLayout, nameFragment)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
return true
}
else -> super.onOptionsItemSelected(item)
}
}

View file

@ -0,0 +1,82 @@
package com.geeksville.mesh.ui
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import com.geeksville.android.Logging
import com.geeksville.android.hideKeyboard
import com.geeksville.mesh.R
import com.geeksville.mesh.databinding.AdvancedSettingsBinding
import com.geeksville.mesh.model.ChannelOption
import com.geeksville.mesh.model.UIViewModel
import com.geeksville.mesh.service.MeshService
import com.google.android.material.snackbar.Snackbar
class AdvancedSettingsFragment : ScreenFragment("Advanced Settings"), Logging {
private val MAX_INT_DEVICE = 0xFFFFFFFF
private var _binding: AdvancedSettingsBinding? = null
// This property is only valid between onCreateView and onDestroyView.
private val binding get() = _binding!!
private val model: UIViewModel by activityViewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = AdvancedSettingsBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
model.radioConfig.observe(viewLifecycleOwner, { _ ->
binding.positionBroadcastPeriodEditText.setText(model.positionBroadcastSecs.toString())
binding.lsSleepEditText.setText(model.lsSleepSecs.toString())
})
model.isConnected.observe(viewLifecycleOwner, Observer { connectionState ->
val connected = connectionState == MeshService.ConnectionState.CONNECTED
binding.positionBroadcastPeriodView.isEnabled = connected
binding.lsSleepView.isEnabled = connected
})
binding.positionBroadcastPeriodEditText.on(EditorInfo.IME_ACTION_DONE) {
val textEdit = binding.positionBroadcastPeriodEditText
val n = textEdit.text.toString().toIntOrNull()
val minBroadcastPeriodSecs =
ChannelOption.fromConfig(model.radioConfig.value?.channelSettings?.modemConfig)?.minBroadcastPeriodSecs
?: 9000
info("edit broadcast $n min $minBroadcastPeriodSecs")
if (n != null && n < MAX_INT_DEVICE && (n == 0 || n >= minBroadcastPeriodSecs)) {
model.positionBroadcastSecs = n
} else {
// restore the value in the edit field
textEdit.setText(model.positionBroadcastSecs.toString())
val errorText = when {
(n == null || n < 0 || n >= MAX_INT_DEVICE) -> "Bad value: ${textEdit.text.toString()}"
else -> getString(R.string.broadcast_period_too_small).format(minBroadcastPeriodSecs)
}
Snackbar.make(requireView(), errorText, Snackbar.LENGTH_LONG).show()
}
requireActivity().hideKeyboard()
}
binding.lsSleepEditText.on(EditorInfo.IME_ACTION_DONE) {
val str = binding.lsSleepEditText.text.toString()
val n = str.toIntOrNull()
if (n != null && n < MAX_INT_DEVICE && n >= 0) {
model.lsSleepSecs = n
} else {
Snackbar.make(requireView(), "Bad value: $str", Snackbar.LENGTH_LONG).show()
}
requireActivity().hideKeyboard()
}
}
}

View file

@ -463,7 +463,6 @@ class BTScanModel(app: Application) : AndroidViewModel(app), Logging {
@SuppressLint("NewApi")
class SettingsFragment : ScreenFragment("Settings"), Logging {
private val MAX_INT_DEVICE = 0xFFFFFFFF
private var _binding: SettingsFragmentBinding? = null
// This property is only valid between onCreateView and onDestroyView.
@ -575,28 +574,18 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
/// Setup the ui widgets unrelated to BLE scanning
private fun initCommonUI() {
// We want to leave these visible in the IDE, but make sure they default to not visible until we have valid data
binding.positionBroadcastPeriodView.visibility = View.GONE
binding.lsSleepView.visibility = View.GONE
model.ownerName.observe(viewLifecycleOwner, { name ->
binding.usernameEditText.setText(name)
})
model.radioConfig.observe(viewLifecycleOwner, { _ ->
binding.positionBroadcastPeriodEditText.setText(model.positionBroadcastSecs.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 { connectionState ->
val connected = connectionState == MeshService.ConnectionState.CONNECTED
binding.usernameView.isEnabled = connected
// Don't even show advanced fields until after we have a connection
binding.positionBroadcastPeriodView.visibility = if (connected) View.VISIBLE else View.GONE
binding.lsSleepView.visibility = if (connected) View.VISIBLE else View.GONE
if (connectionState == MeshService.ConnectionState.DISCONNECTED)
model.ownerName.value = ""
@ -620,36 +609,6 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
requireActivity().hideKeyboard()
}
binding.positionBroadcastPeriodEditText.on(EditorInfo.IME_ACTION_DONE) {
val textEdit = binding.positionBroadcastPeriodEditText
val n = textEdit.text.toString().toIntOrNull()
val minBroadcastPeriodSecs =
ChannelOption.fromConfig(model.radioConfig.value?.channelSettings?.modemConfig)?.minBroadcastPeriodSecs
?: 9000
info("edit broadcast $n min $minBroadcastPeriodSecs")
if (n != null && n < MAX_INT_DEVICE && (n == 0 || n >= minBroadcastPeriodSecs)) {
model.positionBroadcastSecs = n
} else {
// restore the value in the edit field
textEdit.setText(model.positionBroadcastSecs.toString())
val errorText = if (n == null || n < 0 || n >= MAX_INT_DEVICE) "Bad value: ${textEdit.text.toString()}" else
getString(R.string.broadcast_period_too_small).format(minBroadcastPeriodSecs)
Snackbar.make(requireView(), errorText, Snackbar.LENGTH_LONG).show()
}
requireActivity().hideKeyboard()
}
binding.lsSleepEditText.on(EditorInfo.IME_ACTION_DONE) {
val str = binding.lsSleepEditText.text.toString()
val n = str.toIntOrNull()
if (n != null && n < MAX_INT_DEVICE && n >= 0) {
model.lsSleepSecs = n
} else {
binding.scanStatusText.text = "Bad value: $str"
}
requireActivity().hideKeyboard()
}
val app = (requireContext().applicationContext as GeeksvilleApplication)