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) {
val authInfo = account.findAuthInfo()
if (authInfo != null) {
authInfo.userid = newValue
val newAuthInfo = authInfo.clone()
newAuthInfo.userid = newValue
core.removeAuthInfo(authInfo)
core.addAuthInfo(newAuthInfo)
} else {
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) {
val authInfo = account.findAuthInfo()
if (authInfo != null) {
authInfo.password = newValue
val newAuthInfo = authInfo.clone()
newAuthInfo.password = newValue
core.removeAuthInfo(authInfo)
core.addAuthInfo(newAuthInfo)
} else {
Log.w("[Account Settings] Failed to find the matching auth info")
val params = account.params
@ -145,7 +151,10 @@ class AccountSettingsViewModel(val account: Account) : GenericSettingsViewModel(
if (identity != null) {
val authInfo = account.findAuthInfo()
if (authInfo != null) {
authInfo.domain = newValue
val newAuthInfo = authInfo.clone()
newAuthInfo.domain = newValue
core.removeAuthInfo(authInfo)
core.addAuthInfo(newAuthInfo)
} else {
Log.e("[Account Settings] Failed to find the matching auth info")
}
@ -264,6 +273,8 @@ class AccountSettingsViewModel(val account: Account) : GenericSettingsViewModel(
params.serverAddress = address
account.params = params
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.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.*
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.constraintlayout.widget.ConstraintLayout
@ -175,24 +174,10 @@ fun switchSetting(view: View, switchId: Int) {
@BindingAdapter("onValueChanged")
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 {
override fun afterTextChanged(s: Editable?) {
if (s?.isEmpty() == true) {
lambda()
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

View file

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

View file

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