android - Android开发中如何正确处理View State

标签 android kotlin mvvm android-livedata android-architecture-components

目前我的应用程序中有这种方法:

ViewState(每个屏幕一个viewState)

sealed class CategoriesViewState {
    object Loading : CategoriesViewState()

    data class Error(
        val errorMessage: String,
        val messageType: UIComponentType
    ) : CategoriesViewState()

    data class CategoryList(
        val categories: List<Category>
    ) : CategoriesViewState()
}

我使用实时数据在我的 fragment/Activity 中观察到这种状态:

  viewModel.viewState.observe(viewLifecycleOwner, Observer {
            when (it) {
                is CategoriesViewState.Loading -> {
                progress_bar.visibility = View.VISIBLE

                    Log.d(TAG, "LOADING")
                }

                is CategoriesViewState.Error -> {
                progress_bar.visibility = View.GONE

                    Log.d(TAG, "ERROR")

                }

                is CategoriesViewState.CategoryList -> {
                progress_bar.visibility = View.GONE

                    Log.d(TAG, "DATA")

                }
            }
        })

它运行良好。

但是在我看来,随着应用程序的增长,它似乎效率低下。

假设我的应用程序中有 20 个屏幕:我需要 20 个 View 状态,我需要在每个屏幕中编写相同的 when 语句,我需要在每个屏幕中编写这个丑陋的 Visible/Gone(更不用说我需要在每次调用中设置加载状态)

也许我完全错了,这是常见的方法,但对我来说,这似乎是很多代码重复。

我没有具体的问题,我只是想知道这是否是 Android 开发中的常见方法,如果不是,我的代码哪里做错了?

最佳答案

关于不同 Activity 的状态,您不需要每次制作新屏幕时都制作它。您可以按照下面的方法进行相应的修改:

sealed class UIState<out T> where T : Any? {

    object Loading : UIState<Nothing>()

    data class Success<T>(val data: T) : UIState<T>()

    data class Failure(val errorMessage: String, val messageType: UIComponentType) : UIState<Nothing>()
}

那么,现在您的 CategoriesViewState可以表示为 UiState<List<Category>> .

我还创建了一些扩展函数来让事情更容易观察:

infix fun <T> UIState<T>.takeIfSuccess(onSuccess: UIState.Success<T>.() -> Unit): UIState<T> {
    return when (this) {
        is UIState.Success -> {
            onSuccess(this)
            this
        }
        else -> {
            this
        }
    }
}

infix fun <T> UIState<T>.takeIfError(onError: UIState.Failure.() -> Unit): UIState<T> {
    return when (this) {
        is UIState.Failure -> {
            onError(this)
            this
        }
        else -> {
            this
        }
    }
}

在实时数据的观察方法中:

viewModel.viewState.observe(viewLifecycleOwner) { state ->
    state takeIfSuccess {
        // Here's the success state
    } takeIfError {
        // Here's the error state
    }
}

编辑:

如果你不想写成菱形括号(<>),这里是使用类型别名的方法;

// for UIState of UserData class you can do something like this,
typealias UserDataState = UIState<UserData>
...

// Then use this typealias where you should be writing UIState, I.e.
val userLiveData = MutableLiveData<UserDataState>(value)

关于android - Android开发中如何正确处理View State,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63767676/

相关文章:

c# - 在代码隐藏中将参数传递给 RelayCommand

java - 我应该将应用程序子类对象保留为成员变量还是局部变量?

java - 从 JSONArray android 填充 ListView

java - 小部件的 ScrollView

android - RecyclerView 与 ListAdapter

android - 使用 Dagger 2 + Kotlin + ViewModel 注入(inject) ViewModel

android - 如何更改 Spinner 下拉项的文本颜色?

android - 在 fragment 中获取颜色

c# - 清除文本框时属性的值不会更改

flutter - 在 Flutter、MVVM 架构中将模型转换为实体的正确方法?