E
E
Eugene2019-07-15 21:36:20
Android
Eugene, 2019-07-15 21:36:20

How is this clean architecture + mvvm implementation option?

Hello. I would like to ask you for advice. I thought about the implementation of clean architecture + mvvm and came to an option.
1) There is a viewModel, it implements an interface, let's call it ViewModelObserver and there, for example, there will be a method provideList (data: MutableList).
2) The ViewModel constructor has interactors for each action. For example, InteractorGetList has an execute method.
3) From the viewModel, the execute method is called, which passes the result to the provideList() method
4) I checked through leakCanary for no leaks. ViewModel
Source Code

class FragmentItemsViewModel(
    var testInteractor: TestInteractor
): BaseViewModel(), ViewModelObserver{

    override fun provideData(data: MutableList<String>) {
        var f = 1
        f = 2
    }

    fun doOperation(){
        testInteractor.execute(viewModelScope, this)
    }
}

Interactor
class TestInteractor {

    fun execute(scope: CoroutineScope, observer: ViewModelObserver)= scope.launch{
        var data = withContext(Dispatchers.IO){
            delay(5000)
            [email protected] generateData()
        }

        observer.provideData(data)
    }

    fun generateData(): MutableList<String>{
        val data = mutableListOf<String>()

        for(i in 0..1000)
            data.add(i.toString())

        return data
    }
}

I pass the result to the view through binding + liveData
Please tell me how correct this approach is, because while reading the articles I realized that the principle of sole responsibility is respected and you can test and the code is readable. There is also an idea to wrap all useCase (interactors) in a class in which there will be execute methods for each of them to make it easier to inject
using viewModelScope I will get rid of possible leaks because everything in the scope is canceled when the viewModel is destroyed

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question