Skip to content

PresenterProxy

Ahmad K. Bawaneh edited this page Nov 7, 2021 · 1 revision

Even though manually overriding the presenters methods works and is flexible, yet it has boilerplate code and requires a lot of knowledge about the super classes and how they work, plus lots of wiring, to overcome that Domino-mvp provide what is called a presenter proxy, which a class that extends from the base presenters but instead of annotating the class with @Presenter we annotate it with @PressenterProxy, once we have this annotation on the class we can use declarative annotations to inject code inside an auto generated presenter, this will reduce the code we need to write and makes our presenters code more readable, but also adds some interesting feature as a built-in part of the framework that we would have implemented manually without the proxy.

Following is a simple presenter proxy :

@PresenterProxy
public class SimpleViewPresenter extends ViewBaseClientPresenter<SimpleView> {

}

This looks very similar to a normal presenter, and if we look at the generated code we won't see lots of differences between the proxy and the presenter:

/**
 * This is a generated class, please don't modify
 */
@Presenter(
    name = "",
    parent = ""
)
public class SimpleViewPresenter_Presenter extends SimpleViewPresenter {

}

Notice that the generated presenter is extending from the proxy.

Then if we modify the proxy by setting the name and parent values like this :

@PresenterProxy(name = "simpleViewPresenter", parent = "shell")
public class SimpleViewPresenter extends ViewBaseClientPresenter<SimpleView> {

}

Then the generated presenter will look like this :

/**
 * This is a generated class, please don't modify
 */
@Presenter(
    name = "simpleViewPresenter",
    parent = "shell"
)
public class SimpleViewPresenter_Presenter extends SimpleViewPresenter {
  @Override
  public Optional<String> getName() {
    return Optional.of("simpleViewPresenter");
  }

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

Now you can see the getName and getParent methods are both auto generated.

Next we will show how we can define method and bind them to the presenter life-cycle using proxy declarative style :

  • @PostConstruct

    In a presenter proxy we can annotate as many methods with @PostConstruct and they will be called during the presenter post construct life cycle stage.

    Example :

    import javax.annotation.PostConstruct;
    
    @PresenterProxy()
    public class SimpleViewPresenter extends ViewBaseClientPresenter<SimpleView> {
    
        @PostConstruct
        public void doSomething(){
            //Do something here
        }
    
        @PostConstruct
        public void doSomethingElse(){
            //Do another thing here
        }
    }

    Will generate :

    /**
     * This is a generated class, please don't modify
     */
    @Presenter(
        name = "",
        parent = ""
    )
    public class SimpleViewPresenter_Presenter extends SimpleViewPresenter {
      @Override
      protected void postConstruct() {
        doSomething();
        doSomethingElse();
      }
    }

    Notice how the annotated methods are being called, and the order of the methods calls is the same as how they appear in the proxy, also in those methods the view instance will be already created and can be accessed even though it is not yet revealed, for example you can call a method in the view view.doSomeUiStuff().

  • @OnInit

    In a presenter proxy we can annotate as many methods with @OnInit and they will be called during the presenter activation life cycle stage.

    Example :

    import org.dominokit.domino.api.client.annotations.presenter.OnInit;
    
    @PresenterProxy()
    public class SimpleViewPresenter extends ViewBaseClientPresenter<SimpleView> {
    
        @OnInit
        public void doSomething(){
            //Do something here
        }
    
        @OnInit
        public void doSomethingElse(){
            //Do another thing here
        }
    }

    Will generate :

    /**
     * This is a generated class, please don't modify
     */
    @Presenter(
        name = "",
        parent = ""
    )
    public class SimpleViewPresenter_Presenter extends SimpleViewPresenter {
      @Override
      protected void onActivated() {
        doSomething();
        doSomethingElse();
      }
    }

    Notice how the annotated methods are being called, and the order of the methods calls is the same as how they appear in the proxy, also in those methods the view instance will be already created and can be accessed even though it is not yet revealed, for example you can call a method in the view view.doSomeUiStuff().

  • OnBeforeReveal

    In a presenter proxy we can annotate as many methods with @OnBeforeReveal and they will be called during the presenter before reveal life cycle stage.

    Example :

    import org.dominokit.domino.api.client.annotations.presenter.OnBeforeReveal;
    
    @PresenterProxy()
    public class SimpleViewPresenter extends ViewBaseClientPresenter<SimpleView> {
    
        @OnBeforeReveal
        public void doSomething(){
            //Do something here
        }
    
        @OnBeforeReveal
        public void doSomethingElse(){
            //Do another thing here
        }
    }

    Will generate :

    /**
     * This is a generated class, please don't modify
     */
    @Presenter(
        name = "",
        parent = ""
    )
    public class SimpleViewPresenter_Presenter extends SimpleViewPresenter {
      @Override
      protected void onBeforeReveal() {
        doSomething();
        doSomethingElse();
      }
    }

    Notice how the annotated methods are being called, and the order of the methods calls is the same as how they appear in the proxy, also in those methods the view instance will be already created and can be accessed even though it is not yet revealed, for example you can call a method in the view view.doSomeUiStuff().

  • OnReveal

    In a presenter proxy we can annotate as many methods with @OnReveal and they will be called during the presenter reveal life cycle stage.

    Example :

    import org.dominokit.domino.api.client.annotations.presenter.OnReveal;
    
    @PresenterProxy()
    public class SimpleViewPresenter extends ViewBaseClientPresenter<SimpleView> {
    
        @OnReveal
        public void doSomething(){
            //Do something here
        }
    
        @OnReveal
        public void doSomethingElse(){
            //Do another thing here
        }
    }

    Will generate :

    /**
     * This is a generated class, please don't modify
     */
    @Presenter(
        name = "",
        parent = ""
    )
    public class SimpleViewPresenter_Presenter extends SimpleViewPresenter {
      @Override
      public ViewBaseClientPresenter.RevealedHandler getRevealHandler() {
        return ()-> {
          doSomething();
          doSomethingElse();
        } ;
      }
    }

    Notice how the annotated methods are being called, and the order of the methods calls is the same as how they appear in the proxy, also in those methods the view instance will be already created and revealed and can be accessed, for example you can call a method in the view view.doSomeUiStuff().

  • OnRemove

    In a presenter proxy we can annotate as many methods with @OnRemove and they will be called during the presenter remove life cycle stage.

    Example :

    import org.dominokit.domino.api.client.annotations.presenter.OnRemove;
    
    @PresenterProxy()
    public class SimpleViewPresenter extends ViewBaseClientPresenter<SimpleView> {
    
        @OnRemove
        public void doSomething(){
            //Do something here
        }
    
        @OnRemove
        public void doSomethingElse(){
            //Do another thing here
        }
    }

    Will generate :

    /**
     * This is a generated class, please don't modify
     */
    @Presenter(
        name = "",
        parent = ""
    )
    public class SimpleViewPresenter_Presenter extends SimpleViewPresenter {
      @Override
      public ViewBaseClientPresenter.RemovedHandler getRemoveHandler() {
        return ()-> {
          doSomething();
          doSomethingElse();
        } ;
      }
    }

    Notice how the annotated methods are being called, and the order of the methods calls is the same as how they appear in the proxy, we use the remove methods to do any needed clean-up when the presenter get deactivated.