Last active
March 4, 2016 20:31
-
-
Save pardom-zz/fb0838dfe7311ecb0d2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Plumbed(MyViewIntent.class) | |
class MyView extends FrameLayout { | |
@Inject | |
public MyViewIntent viewIntent; | |
@Out("red") | |
public Observable<Integer> red; | |
@Out("green") | |
public Observable<Integer> green; | |
@Out("blue") | |
public Observable<Integer> blue; | |
@In("color") | |
public Observable<Integer> color; | |
// ... | |
private CompositeSubscription subscriptions = CompositeSubscription.empty(); | |
public MyView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
DaggerService.getDaggerComponent<AddGoogleAccountsViewInjector>(context).inject(this); | |
Plumbing.plumb(this, viewIntent); | |
} | |
@Override | |
public void onAttachedToWindow() { | |
super.onAttachedToWindow(); | |
red = RxSeekBar.changes(redSeekBar); | |
green = RxSeekBar.changes(greenSeekBar); | |
blue = RxSeekBar.changes(blueSeekBar); | |
subscriptions.add(color.subscribe((color) -> { colorView.setBackgroundColor(color) })) | |
} | |
@Override | |
public void onDetachedFromWindow() { | |
subscriptions.unsubscribe(); | |
Plumbing.demolish(this); | |
super.onDetachedFromWindow() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyViewIntent { | |
@In("red") | |
public Observable<Integer> red; | |
@In("green") | |
public Observable<Integer> green; | |
@In("blue") | |
public Observable<Integer> blue; | |
@Out("color") | |
public Observable<Integer> color = Observable.combineLatest(red, green, blue, (r, g, b) -> { | |
return ((r & 0x0ff) << 16) | ((g & 0x0ff) << 8) | (b & 0x0ff); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated class. Do not edit! | |
package same.package.as.view; | |
class MyViewModel_Plumber implements Plumber { | |
private CompositeSubscription subscriptions = CompositeSubscription.create(); | |
public void plumb(MyView myView, MyViewModel myViewIntent) { | |
// Red | |
subscriptions.add(Utils.replcate(myView.red, myViewIntent.red)); | |
// Green | |
subscriptions.add(Utils.replcate(myView.green, myViewIntent.green)); | |
// Blue | |
subscriptions.add(Utils.replcate(myView.blue, myViewIntent.blue)); | |
// Color | |
subscriptions.add(Utils.replcate(myViewIntent.color, myView.color)); | |
} | |
public void demolish() { | |
subscriptions.unsubscribe(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Plumber<T1, T2> { | |
void plumb(T1 t1, T2 t2); | |
void demolish(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface PlumberMap { | |
public static final String IMPL_CLASS_FQCN = "plumbing.PlumbingMapImpl"; | |
public <R implements Plumber> plumbingFor(Class cls); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated class. Do not edit! | |
class PlumberMapImpl implements PlumberMap { | |
private Map<Class, Plumber> plumbers = HashMap<>(); | |
static { | |
plumbers.put(MyView.class, new MyViewModel_Plumber()); | |
} | |
public <R implements Plumber> plumbingFor(Class cls) { | |
return (R) plumbers.get(cls); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Plumbing { | |
private static PlumberMap plumberMap = null; | |
public static void <T1, T2> plumb(T1 t1, T2 t2) { | |
ensureInitialized(); | |
plumberMap.plumberFor(t1).plumb(t1, t2); | |
} | |
public static void demolish(Object plumbed) { | |
ensureInitialized(); | |
plumberMap.plumberFor(t1).demolish(); | |
} | |
private static void ensureInitialized() { | |
if (plumberMap == null) { | |
try { | |
Class plumberMapClass = Class.forName(PlumberMap.IMPL_CLASS_FQCN); | |
plumberMap = (PlumberMap) plumberMapClass.newInstance(); | |
} catch (Exception e) { | |
Log.e(TAG, "Failed to initialize.", e); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Utils { | |
public <T> Subscription replicate(Observable<T> source, Observable<T> subject) { | |
return source.subscribe(new Subscriber<T>() { | |
@Override | |
public void onNext(T t) { | |
subject.onNext(t); | |
} | |
@Override | |
public void onError(Throwable e) { | |
subject.onError(e); | |
} | |
@Override | |
public void onCompleted() { | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment