Home android MVVM Android implementation

MVVM Android implementation

Author

Date

Category

implement MVVM according to the scheme presented below. Tell me, I understand this scheme correctly:

  1. If I need to download data from an external source and add to the internal storage, then I appeal to the Remote Data Source, then send from Repository to the internal storage?
  2. If I got data from the internal repository, and I need to additionally make any additional operations with them (calculations), I also do it in repository?
  3. in Fragment I have a RecyClerview, there are several edittext that contain numbers. When one of the numbers in one of the editText is changed, the contents of the remaining editText of this RecyclerView recalculate (all new data should be stored). Previously, it’s just all this was lying in the adapter. How to implement it right with MVVM (which should lie), i.e. Where it will be right to endure this recalculation, updating the RecyclerView adapter values, the subsequent saving of values ​​in the repository.

Naturally, I watched application examples with MVVM implementation, but they were all too simple, so the questions above were left.


Answer 1, Authority 100%

In MVVM, everything concerns logic to write inside ViewModel. It is designed for this. In large applications, as a rule, it is repelled from clean architecture , in which a little more layers. To further it was easier to explain the difference diagram as follows:

Activity / Fragment – Layer View

ViewModel – divided into several layers: ViewModel + Interactor (or UseCase)

All other blocks remain

Now regarding your questions:

  1. yes. All maniipulations with caching of remote data, switching streams on IO Thread or the transformation of an object in the view convenient for further work with the object – all lies in the repository. The business logic in the repository does not write, but it is responsible for the logic of caching, storing the data and receiving the object convenient for the work. That is, this is a certain box that we say: “Give us such a thing”, but we do not know where and how it will get this object.
  2. If you need to perform some calculations on the object that are not tied to the context of the Android platform (in general, everything concerns business logic) – It’s all written in the Interactor layer (this is a separate object that disks for all calculations above the specific object).
  3. All recalculation should be in the Interactor layer. But in the end there should be the following nesting machine:
    • Recycleradapter (Layer View) – says AnyViewModel, that he needs something to recalculate
    • AnyViewModel – it relices the data itself if this is something tied on the Android platform (for example, if you need to know the sizes of some view) or says to produce anyInteractor calculations (if this is a general business logic)
    • anyInteractor (Layer Interactor) requests current data from anyrepository and makes calculations, if necessary, updates the data in the repository
      Meanwhile, the VIEW layer is signed via ViewModel to change data in repository using LiveData. Thus, as soon as all calculations are produced, the data in view will be updated. The most subscription through LiveData inside ViewModel is an important part of the MVVM architecture!

It seems to me that you will be more clear what and where to write if you read several articles on Cleanarchitecture (it can go to any model MV *)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions