A while ago I started playing with the new architecture components, especially
So far I tried to keep out all Android 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 data.
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 10 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 application context? Or would that be a job of the activity?
It seems like AndroidViewModel was created for this purpose.