Класс Wrapper для LiveData - PullRequest
       10

Класс Wrapper для LiveData

1 голос
/ 29 сентября 2019

У меня есть класс ViewModel следующим образом:

class MainViewModel(
    private val schedulerProvider: BaseSchedulerProvider
) : BaseViewModel() {

    private val _posts = MutableLiveData<List<Post>>()
    val posts: LiveData<List<Post>>
        get() = _posts

    private val _isDataLoadingError = MutableLiveData<Boolean>()
    val isDataLoadingError: LiveData<Boolean>
        get() = _isDataLoadingError

    private val _isLoading = MutableLiveData<Boolean>()
    val isLoading: LiveData<Boolean>
        get() = _isLoading

    init {
        showPhotos()
    }

    fun showPhotos() {
        EspressoIdlingResource.increment() // App is busy until further notice
        _isLoading.postValue(true)
        compositeDisposable.add(Network.items.getPhotos()
            .subscribeOn(schedulerProvider.io())
            .observeOn(schedulerProvider.ui())
            .doFinally {
                if (!EspressoIdlingResource.countingIdlingResource.isIdleNow) {
                    EspressoIdlingResource.decrement() // Set app as idle.
                }
                _isLoading.postValue(false)
            }
            .subscribe({
                _isDataLoadingError.postValue(false)
                showPosts(it)
            }) {
                _isDataLoadingError.postValue(true)
                Timber.e(it)
            })
    }

    private fun showPosts(networkPhotos: List<NetworkPhoto>) {
        EspressoIdlingResource.increment() // App is busy until further notice
        _isLoading.postValue(true)
        compositeDisposable.add(Network.items.getPosts()
            .subscribeOn(schedulerProvider.io())
            .observeOn(schedulerProvider.ui())
            .doFinally {
                if (!EspressoIdlingResource.countingIdlingResource.isIdleNow) {
                    EspressoIdlingResource.decrement() // Set app as idle.
                }
                _isLoading.postValue(false)
            }
            .subscribe({ networkPosts ->
                _isDataLoadingError.postValue(false)
                _posts.postValue(
                    PostAndImages(networkPosts, networkPhotos).asDomaineModel()
                )
            }) {
                _isDataLoadingError.postValue(true)
                Timber.e(it)
            })
    }
}

Лучше ли создать класс-оболочку для _isDataLoadingError и _isLoading?Если да, не могли бы вы показать мне, как?

1 Ответ

0 голосов
/ 30 сентября 2019

Я создал перечисление следующим образом:

enum class Status {
    LOADING,
    SUCCESS,
    ERROR
}

И использую его во ViewModel:

    private val _posts = MutableLiveData<List<Post>>()
    val posts: LiveData<List<Post>>
        get() = _posts

    private val _status = MutableLiveData<Status>()
    val status: LiveData<Status>
        get() = _status

    init {
        showPhotos()
    }

    fun showPhotos() {
        EspressoIdlingResource.increment() // App is busy until further notice
        _status.postValue(Status.LOADING)
        compositeDisposable.add(Network.items.getPhotos()
            .subscribeOn(schedulerProvider.io())
            .observeOn(schedulerProvider.ui())
            .doFinally {
                if (!EspressoIdlingResource.countingIdlingResource.isIdleNow) {
                    EspressoIdlingResource.decrement() // Set app as idle.
                }
            }
            .subscribe({
                _status.postValue(Status.SUCCESS)
                showPosts(it)
            }) {
                _status.postValue(Status.ERROR)
                Timber.e(it)
            })
    }
...