A while ago I started playing with the new architecture components, especially and LiveData.

So far I tried to keep out all dependencies out of my ViewModels for portability, but I’m no longer sure if that is the right approach, especially when it comes to formatting and transforming .

Right now some data transformations happen in my ViewModel (everything that does not require a Context), and the is done in my activities//adapters.

This often leads to more complex intermediate “view state” objects (generated in the ViewModel) which contain data in their raw form (dates, numbers), and my activities or fragments are not as dumb as they could be.

For example, think of a Activity that displays a pie chart:

Every piece of the pie got a percentage value, a label and a colour, and the activity displays a title and a summarizing text.

The summarizing text highly depends on the displayed data, think of different templates with multiple placeholders, and there must be some logic to decice which template to use and to calculate the placeholder values (e.g. median, sum, min/max).

The title, summary, colours and labels are defined in xml resource files.

Would it be a problem if the ViewModel would fetch these string and colour resources from the context? Or would that be a job of the activity?

It seems like AndroidViewModel was created for this purpose.

Source link


Please enter your comment!
Please enter your name here