Skip to content

Instantly share code, notes, and snippets.

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/4466d6e79c973fd6466088e6f81af9e1 to your computer and use it in GitHub Desktop.
Save GauravChaddha1996/4466d6e79c973fd6466088e6f81af9e1 to your computer and use it in GitHub Desktop.
class MyPresenter {
private MyViewInterface viewInterface;
private DataManager dataManager;
private MyState state;
MyPresenter(DataManager dataManager) {
this.dataManager = dataManager;
/* Initiates state with a default value for the initial UI
of this view */
state = new MyState("This is the template data");
}
/**
* Called when the UI became visible in onResume, thus we
save the interface object and update the UI with the cached
state */
void attachView(MyViewInterface viewInterface) {
this.viewInterface = viewInterface;
viewInterface.updateUI(state);
}
/**
* Called when the UI became invisible, thus we set the
interface object to null as we can't dispatch more UI
updates but we keep our state object up to date with
the model changes and will update the UI when it attaches
again. */
void detachView() {
this.viewInterface = null;
}
/* Button click UI event from submitButton in view to save
data in the model which we do using dataManager */
void saveName(String name) {
dataManager.saveName(name);
state.setNameSaved(name);
viewInterface.updateUI(state);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment