Skip to content

Instantly share code, notes, and snippets.

@GauravChaddha1996
Last active June 25, 2017 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GauravChaddha1996/6aa24d69722bcf3ae918b24f8b7d6d46 to your computer and use it in GitHub Desktop.
Save GauravChaddha1996/6aa24d69722bcf3ae918b24f8b7d6d46 to your computer and use it in GitHub Desktop.
The following gist shows the job of a model in MVP via an example.
class Model {
UserModelHelper userModel;
RecipeModelHelper recipeModel;
NetworkManager networkManager;
/*
Make the constructor here and initialize the model helpers
like UserModel and other managers
*/
/* Checks if the account is premium or not. Throws an
exception if not, otherwise proceeds with network call.
The business logic is to only give this option to
the premium account holders. This type of logic goes
in the model*/
List<Recipe> onLoadRecipes() {
if (userModel.isAccountPremium()) {
// Fetches the recipes
Result result = networkManager.getRecipes();
// Saves the recipes data
recipeModel.saveRecipes(result.getRecipes());
// Send the result back to the presenter.
return result.getRecipes();
} else {
/* We can show an upgrade dialog when we catch
this */
throw new UpgradeAccountException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment