Skip to content

Commit

Permalink
[ui/settings] improve UX for settings fields
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Apr 4, 2024
1 parent a325fd8 commit 06ca8cb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
40 changes: 40 additions & 0 deletions app/src/main/java/me/capcom/smsgateway/ui/SettingsFragment.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.capcom.smsgateway.ui

import android.content.SharedPreferences
import android.os.Bundle
import android.text.InputType
import androidx.core.content.edit
Expand All @@ -11,6 +12,26 @@ import me.capcom.smsgateway.modules.gateway.GatewaySettings

class SettingsFragment : PreferenceFragmentCompat() {

override fun onResume() {
super.onResume()

onPreferenceChanged.onSharedPreferenceChanged(
preferenceManager.sharedPreferences,
"messages.limit_period"
)
preferenceManager.sharedPreferences?.registerOnSharedPreferenceChangeListener(
onPreferenceChanged
)
}

override fun onPause() {
preferenceManager.sharedPreferences?.unregisterOnSharedPreferenceChangeListener(
onPreferenceChanged
)

super.onPause()
}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)

Expand All @@ -32,18 +53,37 @@ class SettingsFragment : PreferenceFragmentCompat() {
) {
(preference as EditTextPreference).setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
it.setSelectAllOnFocus(true)
it.selectAll()
}
}

if (preference.key == "gateway.cloud_url") {
(preference as EditTextPreference).setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_URI
it.setSelectAllOnFocus(true)
}
}

if (preference.key == "messages.limit_value") {
(preference as EditTextPreference).setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER
it.setSelectAllOnFocus(true)
it.selectAll()
}
}

super.onDisplayPreferenceDialog(preference)
}

private val onPreferenceChanged =
SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
if (key == "messages.limit_period") {
findPreference<EditTextPreference>("messages.limit_value")?.isEnabled =
sharedPreferences?.getString(key, "Disabled") != "Disabled"
}
}

companion object {
fun newInstance() = SettingsFragment()
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<EditTextPreference
app:icon="@drawable/ic_server"
app:key="gateway.cloud_url"
app:title="@string/api_url" />
app:title="@string/api_url"
app:enableCopying="true"/>
<EditTextPreference
app:icon="@drawable/ic_token"
app:key="gateway.private_token"
Expand Down

0 comments on commit 06ca8cb

Please sign in to comment.