Do not disable save button in contact editor when mandatory fields aren't filled, TODO: notify user

This commit is contained in:
Sylvain Berfini 2023-10-03 16:15:49 +02:00
parent ada4e786ec
commit 78dd449baf
2 changed files with 16 additions and 21 deletions

View file

@ -21,7 +21,6 @@ package org.linphone.ui.main.contacts.viewmodel
import androidx.annotation.UiThread
import androidx.annotation.WorkerThread
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.linphone.LinphoneApplication.Companion.coreContext
@ -57,8 +56,6 @@ class ContactNewOrEditViewModel @UiThread constructor() : ViewModel() {
val jobTitle = MutableLiveData<String>()
val saveButtonEnabled = MediatorLiveData<Boolean>()
val saveChangesEvent: MutableLiveData<Event<String>> by lazy {
MutableLiveData<Event<String>>()
}
@ -69,16 +66,6 @@ class ContactNewOrEditViewModel @UiThread constructor() : ViewModel() {
val removeNewNumberOrAddressFieldEvent = MutableLiveData<Event<NewOrEditNumberOrAddressModel>>()
init {
saveButtonEnabled.value = isSaveButtonEnabled()
saveButtonEnabled.addSource(firstName) {
saveButtonEnabled.value = isSaveButtonEnabled()
}
saveButtonEnabled.addSource(lastName) {
saveButtonEnabled.value = isSaveButtonEnabled()
}
}
@UiThread
fun findFriendByRefKey(refKey: String?) {
reset()
@ -129,6 +116,22 @@ class ContactNewOrEditViewModel @UiThread constructor() : ViewModel() {
@UiThread
fun saveChanges() {
var check = true
if (firstName.value.orEmpty().isEmpty()) {
Log.e("$TAG Firstname is empty")
check = false
}
if (sipAddresses.isEmpty() && phoneNumbers.isEmpty()) {
Log.e("$TAG No SIP address nor phone number")
check = false
}
if (!check) {
Log.e("$TAG At least a mandatory field wasn't filled, aborting save")
return
}
// TODO FIXME: notify user
coreContext.postOnCoreThread { core ->
var status = Status.OK
@ -207,7 +210,6 @@ class ContactNewOrEditViewModel @UiThread constructor() : ViewModel() {
core.addFriendList(fl)
}
status = fl.addFriend(friend)
Log.e("UPDATE SUB")
fl.updateSubscriptions()
} else {
friend.done()
@ -263,11 +265,6 @@ class ContactNewOrEditViewModel @UiThread constructor() : ViewModel() {
removeNewNumberOrAddressFieldEvent.value = Event(model)
}
@UiThread
private fun isSaveButtonEnabled(): Boolean {
return firstName.value.orEmpty().isNotEmpty() || lastName.value.orEmpty().isNotEmpty()
}
@UiThread
private fun reset() {
isEdit.value = false
@ -278,6 +275,5 @@ class ContactNewOrEditViewModel @UiThread constructor() : ViewModel() {
phoneNumbers.clear()
company.value = ""
jobTitle.value = ""
saveButtonEnabled.value = false
}
}

View file

@ -57,7 +57,6 @@
android:layout_marginEnd="10dp"
android:padding="5dp"
android:src="@drawable/check"
android:enabled="@{viewModel.saveButtonEnabled}"
app:tint="@color/primary_color_selector"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="@id/title"