refactor: move snackbar out of ScrollView

This commit is contained in:
andrekir 2023-07-27 06:30:15 -03:00
parent 06798ad102
commit f222fe4d5e
3 changed files with 31 additions and 25 deletions

View file

@ -632,6 +632,7 @@ class MainActivity : AppCompatActivity(), Logging {
model.connectionState.removeObservers(this)
bluetoothViewModel.enabled.removeObservers(this)
model.requestChannelUrl.removeObservers(this)
model.snackbarText.removeObservers(this)
super.onStop()
}
@ -665,7 +666,7 @@ class MainActivity : AppCompatActivity(), Logging {
}
}
// Call perhapsChangeChannel() whenever [changeChannelUrl] updates with a non-null value
// Call perhapsChangeChannel() whenever [requestChannelUrl] updates with a non-null value
model.requestChannelUrl.observe(this) { url ->
url?.let {
requestedChannelUrl = url
@ -674,6 +675,13 @@ class MainActivity : AppCompatActivity(), Logging {
}
}
// Call showSnackbar() whenever [snackbarText] updates with a non-null value
model.snackbarText.observe(this) { text ->
if (text is Int) showSnackbar(text)
if (text is String) showSnackbar(text)
if (text != null) model.clearSnackbarText()
}
try {
bindMeshService()
} catch (ex: BindFailedException) {

View file

@ -371,6 +371,20 @@ class UIViewModel @Inject constructor(
_requestChannelUrl.value = null
}
private val _snackbarText = MutableLiveData<Any?>(null)
val snackbarText: LiveData<Any?> get() = _snackbarText
fun showSnackbar(resString: Any) {
_snackbarText.value = resString
}
/**
* Called immediately after activity observes [snackbarText]
*/
fun clearSnackbarText() {
_snackbarText.value = null
}
var txEnabled: Boolean
get() = config.lora.txEnabled
set(value) {

View file

@ -22,8 +22,6 @@ import android.view.inputmethod.EditorInfo
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.RadioButton
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.activityViewModels
@ -57,7 +55,6 @@ import com.geeksville.mesh.util.exceptionToSnackbar
import com.geeksville.mesh.util.getParcelableExtraCompat
import com.geeksville.mesh.util.onEditorAction
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject
@ -259,7 +256,7 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
binding.provideLocationCheckbox.isChecked = true
} else {
debug("User denied background permission")
showSnackbar(getString(R.string.why_background_required))
model.showSnackbar(getString(R.string.why_background_required))
}
}
@ -272,7 +269,7 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
} else requestBackgroundAndCheckLauncher.launch(requireContext().getBackgroundPermissions())
} else {
debug("User denied location permission")
showSnackbar(getString(R.string.why_background_required))
model.showSnackbar(getString(R.string.why_background_required))
}
}
@ -435,7 +432,7 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
}
.setPositiveButton(getString(R.string.report)) { _, _ ->
reportError("Clicked Report A Bug")
Toast.makeText(requireContext(), "Bug report sent!", Toast.LENGTH_LONG).show()
model.showSnackbar("Bug report sent!")
}
.show()
}
@ -670,7 +667,7 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
scanLeDevice()
} else {
errormsg("User denied scan permissions")
showSnackbar(requireContext().permissionMissing)
model.showSnackbar(requireContext().permissionMissing)
}
bluetoothViewModel.permissionsUpdated()
}
@ -703,17 +700,15 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
warningReason: String = getString(R.string.location_disabled_warning)
) {
if (requireContext().gpsDisabled()) {
warn("Telling user we need need location access")
showSnackbar(warningReason)
warn("Telling user we need location access")
model.showSnackbar(warningReason)
}
}
private fun checkBTEnabled(
warningReason: String = getString(R.string.bluetooth_disabled)
) {
private fun checkBTEnabled() {
if (bluetoothViewModel.enabled.value == false) {
warn("Telling user bluetooth is disabled")
Toast.makeText(requireContext(), warningReason, Toast.LENGTH_LONG).show()
model.showSnackbar(R.string.bluetooth_disabled)
}
}
@ -725,17 +720,6 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
}
}
private fun showSnackbar(msg: String) {
if (isAdded) {
Snackbar.make(binding.root, msg, Snackbar.LENGTH_INDEFINITE)
.apply { view.findViewById<TextView>(R.id.snackbar_text).isSingleLine = false }
.setAction(R.string.okay) {
// dismiss
}
.show()
}
}
override fun onPause() {
super.onPause()