refactor: simplify developer options click handling and toast messages #221
1 changed files with 15 additions and 26 deletions
|
@ -34,7 +34,7 @@ open class SettingsFragment: PreferenceFragmentCompat() {
|
||||||
// Show / hide developer preference based on whether it is enabled
|
// Show / hide developer preference based on whether it is enabled
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
preferenceRepository.developerOptionsEnabledFlow
|
preferenceRepository.developerOptionsEnabledFlow
|
||||||
.onEach { developerPref.isVisible = it }
|
.onEach(developerPref::setVisible)
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,41 +100,30 @@ open class SettingsFragment: PreferenceFragmentCompat() {
|
||||||
@Suppress("UNUSED_PARAMETER")
|
@Suppress("UNUSED_PARAMETER")
|
||||||
private fun onAppVersionClicked(pref: Preference): Boolean {
|
private fun onAppVersionClicked(pref: Preference): Boolean {
|
||||||
if (developerPref.isVisible) return false
|
if (developerPref.isVisible) return false
|
||||||
|
|
||||||
val now = System.currentTimeMillis()
|
val now = System.currentTimeMillis()
|
||||||
if (now - lastClickTimestamp >= 1000) {
|
numClicks = if (now - lastClickTimestamp >= 1000) 1 else numClicks + 1
|
||||||
numClicks = 1
|
|
||||||
} else {
|
|
||||||
numClicks++
|
|
||||||
}
|
|
||||||
lastClickTimestamp = now
|
lastClickTimestamp = now
|
||||||
|
|
||||||
if (numClicks == 7) {
|
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
preferenceRepository.developerOptionsEnabledFlow.updatePreference(true)
|
preferenceRepository.developerOptionsEnabledFlow.updatePreference(numClicks >= 7)
|
||||||
|
}
|
||||||
|
|
||||||
|
val toastText = when {
|
||||||
|
numClicks == 7 -> getString(R.string.developer_options_enabled)
|
||||||
|
numClicks > 1 -> getString(R.string.developer_options_steps, 7 - numClicks)
|
||||||
|
else -> return true
|
||||||
|
}
|
||||||
|
|
||||||
lastToast?.cancel()
|
lastToast?.cancel()
|
||||||
Toast.makeText(
|
lastToast = Toast.makeText(requireContext(), toastText, Toast.LENGTH_SHORT)
|
||||||
requireContext(),
|
|
||||||
R.string.developer_options_enabled,
|
|
||||||
Toast.LENGTH_SHORT
|
|
||||||
).show()
|
|
||||||
}
|
|
||||||
} else if (numClicks > 1) {
|
|
||||||
lastToast?.cancel()
|
|
||||||
lastToast = Toast.makeText(
|
|
||||||
requireContext(),
|
|
||||||
getString(R.string.developer_options_steps, 7 - numClicks),
|
|
||||||
Toast.LENGTH_SHORT
|
|
||||||
)
|
|
||||||
lastToast!!.show()
|
lastToast!!.show()
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun CheckBoxPreference.bindBooleanFlow(flow: PreferenceFlowWrapper<Boolean>) {
|
protected fun CheckBoxPreference.bindBooleanFlow(flow: PreferenceFlowWrapper<Boolean>) {
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
flow.collect { isChecked = it }
|
flow.collect(::setChecked)
|
||||||
}
|
}
|
||||||
|
|
||||||
setOnPreferenceChangeListener { _, newValue ->
|
setOnPreferenceChangeListener { _, newValue ->
|
||||||
|
|
Loading…
Add table
Reference in a new issue