Skip to content
This repository was archived by the owner on Aug 13, 2021. It is now read-only.

Commit e614b74

Browse files
author
Mark Wei
committed
Implement filter() for android.
Summary: Fixes #4 Reviewers: O6 Material Motion Android platform reviewers, O2 Material Motion, featherless Reviewed By: O6 Material Motion Android platform reviewers, O2 Material Motion, featherless Tags: #material_motion Differential Revision: http://codereview.cc/D2155
1 parent 15d5c24 commit e614b74

2 files changed

Lines changed: 60 additions & 23 deletions

File tree

library/src/main/java/com/google/android/material/motion/streams/MotionObservable.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ public interface Operation<T, U> {
9797
void next(MotionObserver<U> observer, T value);
9898
}
9999

100+
/**
101+
* A predicate evaluates whether to pass a value downstream.
102+
*/
103+
public interface Predicate<T> {
104+
105+
/**
106+
* Evaluates whether to pass the value downstream.
107+
*/
108+
boolean evaluate(T value);
109+
}
110+
100111
/**
101112
* A light-weight operator builder.
102113
* <p>
@@ -135,4 +146,22 @@ public void unsubscribe() {
135146
}
136147
});
137148
}
149+
150+
/**
151+
* Only emit those values from an Observable that satisfy a predicate.
152+
*
153+
* @see <a href="https://material-motion.github.io/material-motion/starmap/specifications/streams/operators/$._filter">The
154+
* filter() specification</a>
155+
*/
156+
public MotionObservable<T> filter(final Predicate<T> predicate) {
157+
return operator(new Operation<T, T>() {
158+
159+
@Override
160+
public void next(MotionObserver<T> observer, T value) {
161+
if (predicate.evaluate(value)) {
162+
observer.next(value);
163+
}
164+
}
165+
});
166+
}
138167
}

sample/src/main/java/com/google/android/material/motion/streams/sample/MainActivity.java

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
public class MainActivity extends AppCompatActivity {
4545

4646
private static final String[] values = new String[]{
47-
"foo", "bar", "baz", "qux"
47+
"foo", "skip", "bar", "baz", "qux"
4848
};
4949

5050
private MotionObserver<String> callback;
@@ -77,32 +77,40 @@ public void unsubscribe() {
7777
}
7878
});
7979

80-
final Subscription subscription = observable.operator(new Operation<String, CharSequence>() {
80+
final Subscription subscription = observable
81+
.filter(new MotionObservable.Predicate<String>() {
8182

82-
@Override
83-
public void next(MotionObserver<CharSequence> observer, String value) {
84-
CharSequence charSequence = italicizeAndCapitalize(value);
85-
observer.next(charSequence);
86-
}
87-
}).subscribe(new MotionObserver<CharSequence>() {
83+
@Override
84+
public boolean evaluate(String value) {
85+
return value != "skip";
86+
}
87+
})
88+
.operator(new Operation<String, CharSequence>() {
8889

89-
@Override
90-
public void next(CharSequence value) {
91-
text.setText(value);
92-
}
90+
@Override
91+
public void next(MotionObserver<CharSequence> observer, String value) {
92+
CharSequence charSequence = italicizeAndCapitalize(value);
93+
observer.next(charSequence);
94+
}
95+
}).subscribe(new MotionObserver<CharSequence>() {
9396

94-
@Override
95-
public void state(@MotionState int state) {
96-
switch (state) {
97-
case AT_REST:
98-
text.setTextColor(Color.BLACK);
99-
break;
100-
case ACTIVE:
101-
text.setTextColor(Color.RED);
102-
break;
97+
@Override
98+
public void next(CharSequence value) {
99+
text.setText(value);
103100
}
104-
}
105-
});
101+
102+
@Override
103+
public void state(@MotionState int state) {
104+
switch (state) {
105+
case AT_REST:
106+
text.setTextColor(Color.BLACK);
107+
break;
108+
case ACTIVE:
109+
text.setTextColor(Color.RED);
110+
break;
111+
}
112+
}
113+
});
106114

107115
nextButton.setOnClickListener(new View.OnClickListener() {
108116
@Override

0 commit comments

Comments
 (0)