-
Notifications
You must be signed in to change notification settings - Fork 0
ViewModel
##ViewModel A ViewModel is an immutable chunk of data that populates the View. It only contains the exact fields necessary to allow the View to be completed, therefore, a ViewModel is specific to the View around which it is designed. Its immutability is to prevent any discordance with the "single source of truth" that is the data source. This is critical in preventing unpredictable states in the application. To emphasize the importance of immutability, ViewModels in Privvy are AutoValue classes (more to come), which for all intents and purposes are final classes that cannot be changed.
If a ViewModel needs to change, it must alert the Presenter of that change, and the Presenter must then pass an updated ViewModel back to the View.
Sample ViewModel:
@AutoValue abstract class NumberViewModel {
public abstract int number();
//This is a required method
//Prepending AutoValue_ is required, as well
public static NumberViewModel create(int number) {
return new AutoValue_NumberViewModel(number);
}
}