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
import android.os.Bundle
import android.util.Patterns
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -61,16 +60,6 @@ class RemoteProvisioningFragment : Fragment() {
viewModel = ViewModelProvider(this).get(RemoteProvisioningViewModel::class.java)
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 {
if (findNavController().currentDestination?.id == R.id.remoteProvisioningFragment) {
findNavController().navigate(R.id.action_remoteProvisioningFragment_to_qrCodeFragment)

View file

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

View file

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