Skip to content

Instantly share code, notes, and snippets.

@justintuchek
Created November 11, 2016 13:29
Show Gist options
  • Save justintuchek/ed026457a409e9894bf369f2bf08e07d to your computer and use it in GitHub Desktop.
Save justintuchek/ed026457a409e9894bf369f2bf08e07d to your computer and use it in GitHub Desktop.
/**
* This is a wrapper around the original component to transform
* interactions from their original callback style to a more
* reactive approach.
**/
public final class DJICameraKit {
private final Camera component;
@Override
public Observable<CameraMode> getCameraMode() {
return Observable.fromEmitter(new DJIFunctionWith<CameraMode>() {
@Override
public void onCall(DJICompletionCallbackWith<DJICameraSettingsDef.CameraMode> callback) {
component.getCameraMode(callback);
}
}, BackpressureMode.BUFFER);
}
}
public final class CameraKitExample {
private final DJICameraKit kit;
publiic void logCameraMode() {
kit.getCameraMode()
.subscribe(new Observer<Void>() {
@Override
public void onCompleted() {
// some action.
}
@Override
public void onError(Throwable e) {
// some action.
}
@Override
public void onNext(Void aVoid) {
// some action.
}
});
}
}
/**
* Originnal callback mechanisms used.
**/
public class DJICommonCallbacks {
public DJICommonCallbacks() {
}
public interface DJICompletionCallbackWithThreeParam<X, Y, Z> {
void onSuccess(X var1, Y var2, Z var3);
void onFailure(DJIError var1);
}
public interface DJICompletionCallbackWithTwoParam<X, Y> {
void onSuccess(X var1, Y var2);
void onFailure(DJIError var1);
}
public interface DJICompletionCallbackWith<T> {
void onSuccess(T var1);
void onFailure(DJIError var1);
}
public interface DJICompletionCallback {
void onResult(DJIError var1);
}
}
/**
* Convenience method that is passed as a CommonCallback forwards
* the results to the returned Observable.
**/
public abstract class DJIFunction implements Action1<Emitter<Void>> {
@Override
public void call(final Emitter<Void> emitter) {
onCall(new DJICompletionCallback() {
@Override
public void onResult(DJIError error) {
if(error == null) {
emitter.onNext(null);
emitter.onCompleted();
} else {
emitter.onError(new DJIException(error));
}
}
});
}
public abstract void onCall(DJICompletionCallback callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment