Giter VIP home page Giter VIP logo

freight's Introduction

Freight

Use this to ship data between Controller instances when you're using Conductor

Using Freight

@ControllerBuilder //This will generate a builder
public class MainController extends Controller{
  @Extra String testStringExtra;
  @Nullable @Extra int testIntExtra; //Fields annotated with @Nullable are optional
  @Extra("booleanExtraYo") boolean testBooleanExtra;

  public MainController(Bundle args) {
    super(args);
  }

  @NonNull
  @Override
  protected View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
    View view = inflater.inflate(R.layout.controller_main, container, false);
    Freight.ship(this);
    return view;
  }
}

Using the builder

public class MainActivity extends AppCompatActivity {

  private Router router;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ViewGroup container = (ViewGroup) findViewById(R.id.container);

    router = Conductor.attachRouter(this, container, savedInstanceState);
    if(!router.hasRootController()){
      router.setRoot(new MainControllerBuilder()
              .booleanExtraYo(false)
              .testStringExtra("Yeah this is a string")
              .testIntExtra(1)
              .asTransaction());
    }
  }
}

Navigator

Freight can generate scoped navigators for use outside of the main controller.

@ControllerBuilder(value = "Login",
                   scope = "Welcome",
                   popChangeHandler = FadeChangeHandler.class,
                   pushChangeHandler = FadeChangeHandler.class)
public class LoginController extends Controller {
    @Extra String username;
    @Nullable @Extra String password;

    public LoginController(Bundle bundle){
        super(bundle);
    }

}

this will generate a "scoped" navigator interface named WelcomeNavigator

public interface WelcomeNavigator extends Navigator {
  void goToLogin(final String username, @Nullable final String password);
}

as well as an implementation handling the changes via conductor's router.

public class Freight_WelcomeNavigator extends FreightNavigator implements WelcomeNavigator {
  private final Router router;

  public Freight_WelcomeNavigator(Router router) {
    this.router = router;
  }

  public void goToLogin(final String username, @Nullable final String password) {
    final LoginControllerBuilder builder = new LoginControllerBuilder();
    builder.username(username);
    builder.password(password);
    RouterTransaction rt = builder.asTransaction()
        .tag("Login");
    rt.popChangeHandler(new FadeChangeHandler());
    rt.pushChangeHandler(new FadeChangeHandler());
    router.pushController(rt);
  }
}

To use this with DI (such as dagger) you can create a NavigatorFactory and inject that

public class NavigatorFactory {
    private Router router;

    @Inject
    public NavigatorFactory(Router router) {
        this.router = router;
    }

    public WelcomeNavigator welcomeNavigator() {
        return new Freight_WelcomeNavigator(router);
    }
}

There is a lint check bundled with the API artifact that will verify you've called all required builder methods.

Setup

buildscript {
    repositories {
        maven {url "https://oss.sonatype.org/content/repositories/snapshots/" }
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    apt 'io.dwak:freight-processor:0.7-SNAPSHOT'
    compile 'io.dwak:freight:0.7-SNAPSHOT'
}

Acknowledgements

BluelineLabs for the fantastic library

Michael Evans for the name for this library

freight's People

Contributors

burntcookie90 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

erickuck

freight's Issues

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.