Code Monkey home page Code Monkey logo

lightcycle's Introduction

LightCycle

Hex.pm Platform

LightCycle is an Android library that helps break logic out of Activity and Fragment classes into small, self-contained components called LightCycles.

Fields that are annotated @LightCycle and implement the LightCycle API within a LightCycleActivity or LightCycleFragment will be bound to that Activity or Fragment lifecycle.

Usage

public class MyActivity extends LightCycleAppCompatActivity<MyActivity> {
    @LightCycle MyController controller = new MyController();

    @Override
    protected void setActivityContentView() {
        setContentView(R.layout.main);
    }
}
public class MyController extends DefaultActivityLightCycle<MyActivity> {

    @Override
    public void onPause(MyActivity activity) {
        // MyActivity was paused
    }
    
    [...]

    @Override
    public void onResume(MyActivity activity) {
        // MyActivity was resumed
    }
}

Philosophy

LightCycle lets self-contained classes respond to Android’s lifecycle events. This supports composition over inheritance and promotes components that follow the single responsibility principle. We believe it helps us write more readable, maintainable and testable code. It works particularly well alongside dependency injection.

  • Activity & Fragment classes:
  • Inflate layouts and configure Android specifics
  • Declare LightCycles
  • A LightCycle component is responsible for an isolated chunk of logic (such as presentation, tracking etc.)

A LightCycle doesn't know about other LightCycles. There is no guarantee for ordering when multiple LightCycles receive the same lifecycle callback.

Examples

Documentation

LightCycle

There are 3 types of LightCycles - the API is comparable to the ActivityLifecycleCallbacks from the Android SDK:

  • ActivityLightCycle
  • FragmentLightCycle
  • SupportFragmentLightCycle

For convenience, default implementations are provided:

  • DefaultActivityLightCycle
  • DefaultFragmentLightCycle
  • DefaultSupportFragmentLightCycle

Dispatcher

This dispatches an Activity or Fragment lifecycle callback to attached LightCycles. The API defines a single bind method. See the LightCycleDispatcher interface.

Built-in dispatchers

Three types of dispatchers are provided:

  • ActivityLightCycleDispatcher
  • FragmentLightCycleDispatcher
  • SupportFragmentLightCycleDispatcher

Note: these built-in classes are both dispatchers and LightCycles, meaning that you can nest LightCycles.

public class MyActivity extends LightCycleAppCompatActivity<MyBaseActivity> {
    @LightCycle MyController controller = new MyController();

    @Override
    protected void setActivityContentView() {
        setContentView(R.layout.main);
    }
}
public class MyController extends ActivityLightCycleDispatcher<MyActivity> {
    @LightCycle MySubController1 controller = new MyController1();
    @LightCycle MySubController2 controller = new MyController2();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        [...] // <- specific init 
        super.onCreate(savedInstanceState) // <- call super to dispatch.
    }
}

Provided base class dispatchers

The following base activities are provided so far:

  • LightCycleActionBarActivity
  • LightCycleAppCompatActivity
  • LightCycleFragment
  • LightCycleSupportFragment

Adding LightCycle to your own base Activity or Fragment

To add LightCycles to your MyBaseActivity, your Activity must:

  • Implement the LightCycleDispatcher interface
  • Dispatch all the lifecycle methods
  • Bind fields annotated @LightCycle with LightCycles.bind(this)

The same technique applies for Fragment.

public class MyBaseActivity extends Activity implements LightCycleDispatcher<ActivityLightCycle<MyBaseActivity>> {

    private final ActivityLightCycleDispatcher<MyBaseActivity> lightCycleDispatcher;

    @Override
    public void bind(ActivityLightCycle<MyBaseActivity> lightCycle) {
        lightCycleDispatcher.bind(lightCycle);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LightCycles.bind(this);
        lightCycleDispatcher.onCreate((MyBaseActivity) this, savedInstanceState);
    }
    
    [...]
    
    @Override
    protected void onDestroy() {
        lightCycleDispatcher.onDestroy((MyBaseActivity) this);
        super.onDestroy();
    }
}

See for example LightCycleActionBarActivity or LightCycleSupportFragment.

Build integration

Gradle:

buildscript {
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}

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

ext.lightCycleVersion=<LATEST_VERSION>

dependencies {
  compile 'com.soundcloud.lightcycle:lightcycle-lib:$lightCycleVersion'
  apt 'com.soundcloud.lightcycle:lightcycle-processor:$lightCycleVersion'
}

lightcycle's People

Contributors

glung avatar mttkay avatar sc-mobile-ci avatar jdamcd avatar android10 avatar

Watchers

James Cloos avatar Jens Driller 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.