Skip to content

Instantly share code, notes, and snippets.

@mcassiano
Created February 6, 2018 17:17
Show Gist options
  • Save mcassiano/215d537f0ffccb71b8e18c8e53b21617 to your computer and use it in GitHub Desktop.
Save mcassiano/215d537f0ffccb71b8e18c8e53b21617 to your computer and use it in GitHub Desktop.
package me.cassiano.estimate.chain;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
public abstract class ActivityResultChain {
private ActivityResultChain next;
public boolean handle(int requestCode, int resultCode,
@NonNull Context context, @NonNull Intent intent) {
if (canHandle(requestCode, resultCode)) {
handle(context, intent);
return true;
} else if (next != null)
return next.handle(requestCode, resultCode, context, intent);
return false;
}
void setNext(@NonNull ActivityResultChain next) {
if (this.next == null) this.next = next;
else this.next.setNext(next);
}
protected abstract boolean canHandle(int requestCode, int resultCode);
protected abstract void handle(@NonNull Context context, @NonNull Intent intent);
public static final class Builder {
private ActivityResultChain chainHead;
public Builder next(ActivityResultChain next) {
if (chainHead == null) chainHead = next;
else chainHead.setNext(next);
return this;
}
public ActivityResultChain build() {
return chainHead;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment