Vladimir Fisher
Vladimir Fisher

Reputation: 2719

MVVM in Android vs MVP

Now I am studying MVVM, before that I wrote on MVP. One gets the feeling that View has much more authority on MVVM than on MVP. For example, if you need to tell in MVP what SwipeRefreshLayout should do, you call something like seupSwipeBehaviour () in the presenter, which tells View "assign behavior to swipe", in the view in this method, when you swipe, you pull the presenter method. What about MVVM in this case? It turns out that the view itself will set up the SwipeToRefresh behavior, without calling the view model? That is, in onCreate, for example, do we immediately write setupSwipeToRefresh ()?

Upvotes: 0

Views: 271

Answers (1)

Matija Sokol
Matija Sokol

Reputation: 167

In MVVM everything is about observing LiveData objects. If you want to make visible SwipeRefreshLayout or ProgressBar you can make something like this in your ViewModel:

private val _progress = MutableLiveData<Boolean>()
val progress: LiveData<Boolean> get() = _progress

When calling heavy function SwipeRefreshLayout or ProgressBar should be visible:

fun fetchData() {
    _progress.value = true

    //fetch data

    //after success response or error hide SwipeRefreshLayout or ProgressBar
    _progress.value = false
}

In your Activity/Fragment you can observe changes on LiveData objects:

viewModel.progress.observe(this) { visible ->
    if (visible) showProgress() else hideProgress()
}

Upvotes: 1

Related Questions