Skip to content

ViewClientPresenter

Ahmad K. Bawaneh edited this page Nov 7, 2021 · 2 revisions

Viewable presenters

Viewable presenters are those that are linked with a UI view, both the presenter and the view share the life-cycle, when a presenter is activated the view will be revealed and if the view is removed the presenter will be deactivated, we define such presenter bye extending from the ViewBaseClientPresenter and specify the view in the generic type.

@Presenter
public class SimpleViewPresenter extends ViewBaseClientPresenter<SimpleView> {
  @Override
  protected void postConstruct() {
  }

  @Override
  protected void onActivated() {
  }

  @Override
  protected void onDeactivated() {

  }

  @Override
  protected void onBeforeReveal() {
  }

  @Override
  protected RevealedHandler getRevealHandler() {
    return () -> {
      //do something when view revealed
    };
  }

  @Override
  protected RemovedHandler getRemoveHandler() {
    return () -> {
      //do something when view is removed
    };
  }

  @Override
  public Optional<String> getName() {
    return Optional.of("simpleViewPresenter");
  }

}

In a viewable presenter the generic type is the type-of an interface that extends from View which represent the contract between the presenter and its view, notice that the presenter does not know about the implementation of view and the framework will inject the view implementation into the presenter at runtime.

> We will discuss views in details in later parts if the documentation.

In addition to the change in base class and the generic type we notice that we have few more method that we can override here and those methods are coupled to the life-cycle of the presenter and the view together, and the life-cycle is as the following :

  1. A routing happens that requires the presenter to be activated.
  2. A new instance of the presenter is created.
  3. A new instance of the view is created and injected into the presenter.
  4. Call postConstruct
  5. Register presenter events listeners.
  6. Fire presenter state event if such event is present. - we can make the presenter fire an event when ever its (de)activated -
  7. If the presenter has a name we register that presenter name in an internal registry.
  8. View is about to be revealed.

    We will explain more about revealing views as part of this documentation.

  9. Call onBeforeReveal.
  10. View is revealed.
  11. Call the onRevealHandler.
  12. Later view is removed.
  13. Call onRemoveHandler
  14. Call onDeactivated