Improved how text input based settings work

This commit is contained in:
Sylvain Berfini 2021-05-20 17:54:04 +02:00
parent 7d6b72e40f
commit 72433f2a2f
4 changed files with 22 additions and 27 deletions

View file

@ -109,7 +109,10 @@ class AccountSettingsViewModel(val account: Account) : GenericSettingsViewModel(
override fun onTextValueChanged(newValue: String) { override fun onTextValueChanged(newValue: String) {
val authInfo = account.findAuthInfo() val authInfo = account.findAuthInfo()
if (authInfo != null) { if (authInfo != null) {
authInfo.userid = newValue val newAuthInfo = authInfo.clone()
newAuthInfo.userid = newValue
core.removeAuthInfo(authInfo)
core.addAuthInfo(newAuthInfo)
} else { } else {
Log.e("[Account Settings] Failed to find the matching auth info") Log.e("[Account Settings] Failed to find the matching auth info")
} }
@ -121,7 +124,10 @@ class AccountSettingsViewModel(val account: Account) : GenericSettingsViewModel(
override fun onTextValueChanged(newValue: String) { override fun onTextValueChanged(newValue: String) {
val authInfo = account.findAuthInfo() val authInfo = account.findAuthInfo()
if (authInfo != null) { if (authInfo != null) {
authInfo.password = newValue val newAuthInfo = authInfo.clone()
newAuthInfo.password = newValue
core.removeAuthInfo(authInfo)
core.addAuthInfo(newAuthInfo)
} else { } else {
Log.w("[Account Settings] Failed to find the matching auth info") Log.w("[Account Settings] Failed to find the matching auth info")
val params = account.params val params = account.params
@ -145,7 +151,10 @@ class AccountSettingsViewModel(val account: Account) : GenericSettingsViewModel(
if (identity != null) { if (identity != null) {
val authInfo = account.findAuthInfo() val authInfo = account.findAuthInfo()
if (authInfo != null) { if (authInfo != null) {
authInfo.domain = newValue val newAuthInfo = authInfo.clone()
newAuthInfo.domain = newValue
core.removeAuthInfo(authInfo)
core.addAuthInfo(newAuthInfo)
} else { } else {
Log.e("[Account Settings] Failed to find the matching auth info") Log.e("[Account Settings] Failed to find the matching auth info")
} }
@ -264,6 +273,8 @@ class AccountSettingsViewModel(val account: Account) : GenericSettingsViewModel(
params.serverAddress = address params.serverAddress = address
account.params = params account.params = params
transportIndex.value = account.params.transport.toInt() transportIndex.value = account.params.transport.toInt()
} else {
Log.e("[Account Settings] Couldn't parse address: $address")
} }
} }
} }

View file

@ -29,7 +29,6 @@ import android.util.Patterns
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.* import android.widget.*
import android.widget.SeekBar.OnSeekBarChangeListener import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
@ -175,23 +174,9 @@ fun switchSetting(view: View, switchId: Int) {
@BindingAdapter("onValueChanged") @BindingAdapter("onValueChanged")
fun editTextSetting(view: EditText, lambda: () -> Unit) { fun editTextSetting(view: EditText, lambda: () -> Unit) {
view.setOnFocusChangeListener { _, hasFocus ->
if (!hasFocus) lambda()
}
view.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
lambda()
true
}
false
}
view.addTextChangedListener(object : TextWatcher { view.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) { override fun afterTextChanged(s: Editable?) {
if (s?.isEmpty() == true) { lambda()
lambda()
}
} }
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

View file

@ -94,10 +94,10 @@
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
android:id="@+id/settings_input" android:id="@+id/settings_input"
onValueChanged="@{() -> listener.onTextValueChanged(settingsInput.getText().toString())}"
android:text="@{defaultValue}" android:text="@{defaultValue}"
android:imeOptions="actionDone" android:imeOptions="actionDone"
android:inputType="@{InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS}" android:inputType="@{InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS}"
onValueChanged="@{() -> listener.onTextValueChanged(settingsInput.getText().toString())}"
android:singleLine="true" android:singleLine="true"
android:background="@color/transparent_color" android:background="@color/transparent_color"
android:layout_width="match_parent" android:layout_width="match_parent"

View file

@ -30,15 +30,15 @@
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<ImageView <ImageView
android:id="@+id/settings_clear_input" android:id="@+id/settings_commit_input"
android:onClick="@{() -> settingsInput.setText(defaultValue)}" android:onClick="@{() -> listener.onTextValueChanged(settingsInput.getText().toString())}"
android:visibility="invisible" android:visibility="invisible"
android:contentDescription="@string/content_description_clear_field" android:contentDescription="@string/content_description_confirm_contact_edit"
android:layout_width="20dp" android:layout_width="20dp"
android:layout_height="20dp" android:layout_height="20dp"
android:layout_marginLeft="10dp" android:layout_marginLeft="10dp"
android:layout_marginRight="20dp" android:layout_marginRight="20dp"
android:src="@drawable/field_clean" android:src="@drawable/valid"
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
android:layout_centerVertical="true" /> android:layout_centerVertical="true" />
@ -46,7 +46,7 @@
android:id="@+id/settings_input_layout" android:id="@+id/settings_input_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_toLeftOf="@id/settings_clear_input" android:layout_toLeftOf="@id/settings_commit_input"
android:layout_marginTop="5dp" android:layout_marginTop="5dp"
android:layout_marginLeft="10dp" android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" android:layout_marginRight="10dp"
@ -57,8 +57,7 @@
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
android:id="@+id/settings_input" android:id="@+id/settings_input"
onValueChanged="@{() -> listener.onTextValueChanged(settingsInput.getText().toString())}" onFocusChangeVisibilityOf="@{settingsCommitInput}"
onFocusChangeVisibilityOf="@{settingsClearInput}"
android:text="@{defaultValue}" android:text="@{defaultValue}"
android:imeOptions="actionDone" android:imeOptions="actionDone"
android:inputType="@{inputType ?? InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS}" android:inputType="@{inputType ?? InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS}"