Code Monkey home page Code Monkey logo

aura-interactor's Introduction

Build Status Bintray

aura-interactor

Inspired (read: scared) by the idea of the repetative task of implementing interactor (Use Case in Clean Architecture) wappers for the purpuse of background execution, I wanted to learn a bit more about code generation and annotation processing in Java so i set out to do this project.

Usage

Annotate your interactor with @AuraInteractor

@AuraInteractor
public interface LoginInteractor {

  void login(LoginCredentials input, LoginOutput loginOutput);

  void guestLogin(LoginOutput loginOutput);
}

This will generate:

public final class AuraInteractor_LoginInteractor implements LoginInteractor {
  private final LoginInteractor interactor;

  private final AuraExecutor executor;

  AuraInteractor_LoginInteractor(LoginInteractor interactor, AuraExecutor executor) {
    this.interactor = interactor;
    this.executor = executor;
  }

  @Override
  public final void login(final LoginCredentials input, final LoginOutput loginOutput) {
    executor.runBackground(new Runnable() {
      @Override
      public void run() {
        final AuraOutput_LoginOutput auraOutput = new AuraOutput_LoginOutput(loginOutput, executor);
        interactor.login(input, auraOutput);
      }
    });
  }

  @Override
  public final void guestLogin(final LoginOutput loginOutput) {
    executor.runBackground(new Runnable() {
      @Override
      public void run() {
        final AuraOutput_LoginOutput auraOutput = new AuraOutput_LoginOutput(loginOutput, executor);
        interactor.guestLogin(auraOutput);
      }
    });
  }
}

Annotate the output(s) of the interactor with @AuraOutput. To invoke output methods on background annotete them with @AuraOff

@AuraOutput
public interface LoginOutput {

  void successfulLogin(String message);

  void failedLogin(Exception e);

  @AuraOff
  void loginProgress(double progress);
}

This will generate:

public final class AuraOutput_LoginOutput implements LoginOutput {
  private final LoginOutput output;

  private final AuraExecutor executor;

  AuraOutput_LoginOutput(LoginOutput output, AuraExecutor executor) {
    this.output = output;
    this.executor = executor;
  }

  @Override
  public final void successfulLogin(final String message) {
    executor.runForeground(new Runnable() {
      @Override
      public void run() {
        output.successfulLogin(message);
      }
    });
  }

  @Override
  public final void failedLogin(final Exception e) {
    executor.runForeground(new Runnable() {
      @Override
      public void run() {
        output.failedLogin(e);
      }
    });
  }

  @Override
  public final void loginProgress(final double progress) {
    output.loginProgress(progress);
  }
}

To use the generated interactors

private static AuraExecutor provideAuraExecutor() {
  // return your executor however you defined it
}

public static LoginInteractor provideLoginInteractor() {
  LoginInteractor loginInteractor = new LoginInteractorImpl();
  return new AuraInteractor_LoginInteractor(loginInteractor, provideAuraExecutor());
}
loginInteractor = DependencyProvider.provideLoginInteractor();
loginInteractor.login(loginCredentials, loginOuput);

Look at the full sample in 'app' module.

Download

No stable release available yet.

aura-interactor's People

Contributors

toteto avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.