android10 / android10/Android-CleanArchitecture-Kotlin

Clean architecture of input validation

Aperta
#44 1 commento 6 reazioni 0 assegnatari Vedi su GitHub
discussion question
Lingua principale
Kotlin
Stelle
4.8k
Fork
929
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Hello.
The example here covers only fetching immutable data. But for me, the most challenging is creating and updating existing data. How to handle validation of mutable data, when there is high coupling between View and ViewModel.
Should I keep track of each field separately ?
```kotlin
...
val id: MutableLiveData = MutableLiveData()
val title: MutableLiveData = MutableLiveData()
val titleError: MutableLiveData = MutableLiveData()
val year: MutableLiveData = MutableLiveData()
val yearError: MutableLiveData = MutableLiveData()
...
```
and then how to validate ?
```kotlin
fun createMovie() {
var valid = true

val title = title.value
if (title == null || title.isBlank()) {
titleError.value = "Title can not be blank"
valid = false
}

val year = year.value
if (year == null || year < 0) {
yearError.value = "Year must be positive number"
valid = false
}
...

if(valid){
val newMovie = MovieDetails(
id = UUID.randomUUID().toString().hashCode(),
title = title!!,
year = year!!,
...)

createMovieDetails.execute({ it.either(::handleFailure, ::handleMovieDetails) }, Params(newMovie))

}
}
```
And then observe on each error ?
```kotlin
override fun onCreate(savedInstanceState: Bundle?) {
...
with(movieDetailsViewModel) {
observe(titleError, ::handleTitleError)
observe(yearError, ::handleYearError)
}
}

private fun handleTitleError(erorrMessage: String?) {
editTextTitle.error = erorrMessage
}
private fun handleYearError(errorMessage: String?) {
Toast.makeText(context,errorMessage,Toast.LENGTH_SHORT).show()
}
```

I'm not sure of any of those lines
And how to update `title` in `ViewModel` ? Via `TextWatcher` ? It becomes quite tricky, when it comes to update `TextView` from `ViewModel`'s `Observable` and updating `ViewModel` from `TextView`'s `TextWatcher` (recursive updates).

An example of creating new and updating existing movies would be great 👍

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.