Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Trim white spaces from person and location name(EXPOSUREAPP-4408) #1969

Merged
merged 8 commits into from
Dec 21, 2020
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ class ContactDiaryLocationBottomSheetDialogFragment : BottomSheetDialogFragment(
binding.contactDiaryLocationBottomSheetTextInputEditText.setOnEditorActionListener { v, actionId, event ->
return@setOnEditorActionListener when (actionId) {
EditorInfo.IME_ACTION_DONE -> {
binding.contactDiaryLocationBottomSheetSaveButton.performClick()
if (viewModel.isValid.value == true) {
binding.contactDiaryLocationBottomSheetSaveButton.performClick()
}
false
}
else -> true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,33 @@ class ContactDiaryLocationBottomSheetDialogViewModel @AssistedInject constructor
@Assisted private val addedAt: String?,
private val contactDiaryRepository: ContactDiaryRepository
) : CWAViewModel(dispatcherProvider = dispatcherProvider) {

private val text = MutableStateFlow("")

val isValid = text.map {
it.isNotEmpty() && it.length <= MAX_LOCATION_NAME_LENGTH
it.isNotEmpty()
}.asLiveData()

val shouldClose = SingleLiveEvent<Unit>()

private val formattedName: String
ralfgehrer marked this conversation as resolved.
Show resolved Hide resolved
get() {
var newName = text.value
ralfgehrer marked this conversation as resolved.
Show resolved Hide resolved
// allow only spaces as a name
if (newName.isNotBlank()) {
newName = newName.trim()
}
return newName.take(MAX_LOCATION_NAME_LENGTH)
}

fun textChanged(locationName: String) {
text.value = locationName
}

fun addLocation() = launch {
val location = contactDiaryRepository.addLocation(
DefaultContactDiaryLocation(
locationName = text.value.take(MAX_LOCATION_NAME_LENGTH)
locationName = formattedName
)
)

Expand All @@ -56,7 +67,7 @@ class ContactDiaryLocationBottomSheetDialogViewModel @AssistedInject constructor
contactDiaryRepository.updateLocation(
DefaultContactDiaryLocation(
location.locationId,
locationName = text.value.take(MAX_LOCATION_NAME_LENGTH)
locationName = formattedName
)
)
shouldClose.postValue(null)
Expand All @@ -75,12 +86,10 @@ class ContactDiaryLocationBottomSheetDialogViewModel @AssistedInject constructor
shouldClose.postValue(null)
}

companion object {
private const val MAX_LOCATION_NAME_LENGTH = 250
}

@AssistedInject.Factory
interface Factory : CWAViewModelFactory<ContactDiaryLocationBottomSheetDialogViewModel> {
fun create(addedAt: String?): ContactDiaryLocationBottomSheetDialogViewModel
}
}

private const val MAX_LOCATION_NAME_LENGTH = 250
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.EditorInfo.IME_ACTION_DONE
import androidx.core.widget.doAfterTextChanged
import androidx.navigation.fragment.navArgs
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
Expand Down Expand Up @@ -69,8 +69,10 @@ class ContactDiaryPersonBottomSheetDialogFragment : BottomSheetDialogFragment(),

binding.contactDiaryPersonBottomSheetTextInputEditText.setOnEditorActionListener { v, actionId, event ->
return@setOnEditorActionListener when (actionId) {
EditorInfo.IME_ACTION_DONE -> {
binding.contactDiaryPersonBottomSheetSaveButton.performClick()
IME_ACTION_DONE -> {
if (viewModel.isValid.value == true) {
binding.contactDiaryPersonBottomSheetSaveButton.performClick()
}
false
}
else -> true
Expand All @@ -83,9 +85,9 @@ class ContactDiaryPersonBottomSheetDialogFragment : BottomSheetDialogFragment(),
dismiss()
}

viewModel.isValid.observe2(this) {
binding.contactDiaryPersonBottomSheetTextInputLayout.isErrorEnabled = it
binding.contactDiaryPersonBottomSheetSaveButton.isEnabled = it
viewModel.isValid.observe2(this) { isValid ->
binding.contactDiaryPersonBottomSheetTextInputLayout.isErrorEnabled = isValid
binding.contactDiaryPersonBottomSheetSaveButton.isEnabled = isValid
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,33 @@ class ContactDiaryPersonBottomSheetDialogViewModel @AssistedInject constructor(
@Assisted private val addedAt: String?,
private val contactDiaryRepository: ContactDiaryRepository
) : CWAViewModel(dispatcherProvider = dispatcherProvider) {

private val text = MutableStateFlow("")

val isValid = text.map {
it.isNotEmpty() && it.length <= MAX_PERSON_NAME_LENGTH
ralfgehrer marked this conversation as resolved.
Show resolved Hide resolved
it.isNotEmpty()
}.asLiveData()

val shouldClose = SingleLiveEvent<Unit>()

private val formattedName: String
ralfgehrer marked this conversation as resolved.
Show resolved Hide resolved
get() {
var newName = text.value
// allow only spaces as a name
if (newName.isNotBlank()) {
newName = newName.trim()
}
return newName.take(MAX_PERSON_NAME_LENGTH)
}

fun textChanged(locationName: String) {
text.value = locationName
}

fun addPerson() = launch {
val person = contactDiaryRepository.addPerson(
DefaultContactDiaryPerson(
fullName = text.value.take(MAX_PERSON_NAME_LENGTH)
fullName = formattedName
)
)

Expand All @@ -56,7 +67,7 @@ class ContactDiaryPersonBottomSheetDialogViewModel @AssistedInject constructor(
contactDiaryRepository.updatePerson(
DefaultContactDiaryPerson(
person.personId,
fullName = text.value.take(MAX_PERSON_NAME_LENGTH)
fullName = formattedName
)
)
shouldClose.postValue(null)
Expand All @@ -75,12 +86,10 @@ class ContactDiaryPersonBottomSheetDialogViewModel @AssistedInject constructor(
shouldClose.postValue(null)
}

companion object {
private const val MAX_PERSON_NAME_LENGTH = 250
}

@AssistedInject.Factory
interface Factory : CWAViewModelFactory<ContactDiaryPersonBottomSheetDialogViewModel> {
fun create(addedAt: String?): ContactDiaryPersonBottomSheetDialogViewModel
}
}

private const val MAX_PERSON_NAME_LENGTH = 250
ralfgehrer marked this conversation as resolved.
Show resolved Hide resolved