android10 / android10/Android-CleanArchitecture-Kotlin

Clean architecture of input validation

未关闭
#44 1 条评论 6 个 reaction 已指派 0 人 在 GitHub 查看
discussion question
主要语言
Kotlin
星标
4.8k
派生
929
PR 合并指标
30 天内没有已合并 PR

描述

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 👍

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。