diff --git a/library/src/main/java/com/google/android/material/motion/streams/MotionObservable.java b/library/src/main/java/com/google/android/material/motion/streams/MotionObservable.java index 81dcc3a..633383b 100644 --- a/library/src/main/java/com/google/android/material/motion/streams/MotionObservable.java +++ b/library/src/main/java/com/google/android/material/motion/streams/MotionObservable.java @@ -97,6 +97,17 @@ public interface Operation { void next(MotionObserver observer, T value); } + /** + * A predicate evaluates whether to pass a value downstream. + */ + public interface Predicate { + + /** + * Evaluates whether to pass the value downstream. + */ + boolean evaluate(T value); + } + /** * A light-weight operator builder. *

@@ -135,4 +146,22 @@ public void unsubscribe() { } }); } + + /** + * Only emit those values from an Observable that satisfy a predicate. + * + * @see The + * filter() specification + */ + public MotionObservable filter(final Predicate predicate) { + return operator(new Operation() { + + @Override + public void next(MotionObserver observer, T value) { + if (predicate.evaluate(value)) { + observer.next(value); + } + } + }); + } } diff --git a/sample/src/main/java/com/google/android/material/motion/streams/sample/MainActivity.java b/sample/src/main/java/com/google/android/material/motion/streams/sample/MainActivity.java index 3ad73d0..eaa170d 100644 --- a/sample/src/main/java/com/google/android/material/motion/streams/sample/MainActivity.java +++ b/sample/src/main/java/com/google/android/material/motion/streams/sample/MainActivity.java @@ -44,7 +44,7 @@ public class MainActivity extends AppCompatActivity { private static final String[] values = new String[]{ - "foo", "bar", "baz", "qux" + "foo", "skip", "bar", "baz", "qux" }; private MotionObserver callback; @@ -77,32 +77,40 @@ public void unsubscribe() { } }); - final Subscription subscription = observable.operator(new Operation() { + final Subscription subscription = observable + .filter(new MotionObservable.Predicate() { - @Override - public void next(MotionObserver observer, String value) { - CharSequence charSequence = italicizeAndCapitalize(value); - observer.next(charSequence); - } - }).subscribe(new MotionObserver() { + @Override + public boolean evaluate(String value) { + return value != "skip"; + } + }) + .operator(new Operation() { - @Override - public void next(CharSequence value) { - text.setText(value); - } + @Override + public void next(MotionObserver observer, String value) { + CharSequence charSequence = italicizeAndCapitalize(value); + observer.next(charSequence); + } + }).subscribe(new MotionObserver() { - @Override - public void state(@MotionState int state) { - switch (state) { - case AT_REST: - text.setTextColor(Color.BLACK); - break; - case ACTIVE: - text.setTextColor(Color.RED); - break; + @Override + public void next(CharSequence value) { + text.setText(value); } - } - }); + + @Override + public void state(@MotionState int state) { + switch (state) { + case AT_REST: + text.setTextColor(Color.BLACK); + break; + case ACTIVE: + text.setTextColor(Color.RED); + break; + } + } + }); nextButton.setOnClickListener(new View.OnClickListener() { @Override