Improved remote provisioning wrong url format error display

This commit is contained in:
Sylvain Berfini 2020-04-01 10:55:43 +02:00
parent 4a2e31f16d
commit baf2c10755
4 changed files with 38 additions and 17 deletions

View file

@ -20,7 +20,6 @@
package org.linphone.activities.assistant.fragments package org.linphone.activities.assistant.fragments
import android.os.Bundle import android.os.Bundle
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
@ -61,16 +60,6 @@ class RemoteProvisioningFragment : Fragment() {
viewModel = ViewModelProvider(this).get(RemoteProvisioningViewModel::class.java) viewModel = ViewModelProvider(this).get(RemoteProvisioningViewModel::class.java)
binding.viewModel = viewModel binding.viewModel = viewModel
binding.setApplyClickListener {
val url = viewModel.urlToFetch.value.orEmpty()
if (Patterns.WEB_URL.matcher(url).matches()) {
viewModel.fetchAndApply(url)
} else {
val activity = requireActivity() as AssistantActivity
activity.showSnackBar(R.string.assistant_remote_provisioning_wrong_format)
}
}
binding.setQrCodeClickListener { binding.setQrCodeClickListener {
if (findNavController().currentDestination?.id == R.id.remoteProvisioningFragment) { if (findNavController().currentDestination?.id == R.id.remoteProvisioningFragment) {
findNavController().navigate(R.id.action_remoteProvisioningFragment_to_qrCodeFragment) findNavController().navigate(R.id.action_remoteProvisioningFragment_to_qrCodeFragment)

View file

@ -19,6 +19,7 @@
*/ */
package org.linphone.activities.assistant.viewmodels package org.linphone.activities.assistant.viewmodels
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.coreContext
@ -30,6 +31,9 @@ import org.linphone.utils.Event
class RemoteProvisioningViewModel : ViewModel() { class RemoteProvisioningViewModel : ViewModel() {
val urlToFetch = MutableLiveData<String>() val urlToFetch = MutableLiveData<String>()
val urlError = MutableLiveData<String>()
val fetchEnabled: MediatorLiveData<Boolean> = MediatorLiveData()
val fetchInProgress = MutableLiveData<Boolean>() val fetchInProgress = MutableLiveData<Boolean>()
val fetchSuccessfulEvent = MutableLiveData<Event<Boolean>>() val fetchSuccessfulEvent = MutableLiveData<Event<Boolean>>()
@ -50,6 +54,14 @@ class RemoteProvisioningViewModel : ViewModel() {
init { init {
fetchInProgress.value = false fetchInProgress.value = false
coreContext.core.addListener(listener) coreContext.core.addListener(listener)
fetchEnabled.value = false
fetchEnabled.addSource(urlToFetch) {
fetchEnabled.value = isFetchEnabled()
}
fetchEnabled.addSource(urlError) {
fetchEnabled.value = isFetchEnabled()
}
} }
override fun onCleared() { override fun onCleared() {
@ -57,11 +69,16 @@ class RemoteProvisioningViewModel : ViewModel() {
super.onCleared() super.onCleared()
} }
fun fetchAndApply(url: String) { fun fetchAndApply() {
val url = urlToFetch.value.orEmpty()
coreContext.core.provisioningUri = url coreContext.core.provisioningUri = url
Log.w("[Remote Provisioning] Url set to [$url], restarting Core") Log.w("[Remote Provisioning] Url set to [$url], restarting Core")
fetchInProgress.value = true fetchInProgress.value = true
coreContext.core.stop() coreContext.core.stop()
coreContext.core.start() coreContext.core.start()
} }
private fun isFetchEnabled(): Boolean {
return urlToFetch.value.orEmpty().isNotEmpty() && urlError.value.orEmpty().isEmpty()
}
} }

View file

@ -368,6 +368,22 @@ fun addEmailEditTextValidation(editText: EditText, enabled: Boolean) {
}) })
} }
@BindingAdapter("urlConfirmationValidation")
fun addUrlEditTextValidation(editText: EditText, enabled: Boolean) {
if (!enabled) return
editText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) { }
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { }
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (!Patterns.WEB_URL.matcher(s).matches()) {
editText.error = editText.context.getString(R.string.assistant_remote_provisioning_wrong_format)
}
}
})
}
@BindingAdapter("passwordConfirmationValidation") @BindingAdapter("passwordConfirmationValidation")
fun addPasswordConfirmationEditTextValidation(password: EditText, passwordConfirmation: EditText) { fun addPasswordConfirmationEditTextValidation(password: EditText, passwordConfirmation: EditText) {
password.addTextChangedListener(object : TextWatcher { password.addTextChangedListener(object : TextWatcher {

View file

@ -5,9 +5,6 @@
<data> <data>
<import type="android.view.View"/> <import type="android.view.View"/>
<variable
name="applyClickListener"
type="android.view.View.OnClickListener"/>
<variable <variable
name="qrCodeClickListener" name="qrCodeClickListener"
type="android.view.View.OnClickListener"/> type="android.view.View.OnClickListener"/>
@ -68,6 +65,8 @@
android:layout_height="wrap_content"/> android:layout_height="wrap_content"/>
<EditText <EditText
errorMessage="@={viewModel.urlError}"
urlConfirmationValidation="@{true}"
android:text="@={viewModel.urlToFetch}" android:text="@={viewModel.urlToFetch}"
android:background="@drawable/resizable_text_field" android:background="@drawable/resizable_text_field"
android:textColor="@color/black_color" android:textColor="@color/black_color"
@ -78,8 +77,8 @@
android:maxLines="1"/> android:maxLines="1"/>
<TextView <TextView
android:onClick="@{applyClickListener}" android:onClick="@{() -> viewModel.fetchAndApply()}"
android:enabled="@{viewModel.urlToFetch.length() > 0}" android:enabled="@{viewModel.fetchEnabled}"
android:text="@string/assistant_fetch_apply" android:text="@string/assistant_fetch_apply"
android:background="@drawable/assistant_button" android:background="@drawable/assistant_button"
android:textColor="@drawable/assistant_button_text_color" android:textColor="@drawable/assistant_button_text_color"