-
Notifications
You must be signed in to change notification settings - Fork 0
Yoke
Privvy Components don't always exist in a vacuum, however much we would like them to; communication between components is sometimes necessary. Say one wants to click a button of a Fragment, and have an adjacent Fragment's ListView be able to update its data or perform an animation: the first Fragment would have to push a notification to the second, if each Fragment's logic was perfectly self contained (as it should be).
Enter: Yokes, which is a name referring to "the wooden crosspiece that harnesses two animals together". Yokes are simply wrappers for RxJava PublishSubjects (or PublishRelay, as we're using). Yokes are injected into the Presenters of the communicating Privvy Components, and behave much like a message queue.
Yokes are given a separate name because of how limited they're designed to be. Yokes should map to single methods within Presenters and serve only 1 purpose, with no reuse in other areas of the application -- as far away from a Bus implementation as possible. Yokes also are only designed for interactions ABOVE the data layer. If two Fragments need to interact, but data needs to change, then the normal flow of operations applies.
Sample:
// An interface and implementation need creating for each Yoke for testing purposes
public class NumberUpdateYokeImpl implements NumberUpdateYoke {
//This doesn't have to be a PublishSubject, but most of the time it will be
private PublishRelay<Integer> numberSubject = PublishRelay.create();
public NumberUpdateYokeImpl() {
}
@Override
public Observable<Integer> onNumberChanged() {
return numberSubject.asObservable();
}
@Override
public void changeNumber(int newNumber) {
numberSubject.call(newNumber);
}
}