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

Commit

Permalink
Implement filter() for android.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Mark Wei committed Dec 8, 2016
1 parent 15d5c24 commit e614b74
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ public interface Operation<T, U> {
void next(MotionObserver<U> observer, T value);
}

/**
* A predicate evaluates whether to pass a value downstream.
*/
public interface Predicate<T> {

/**
* Evaluates whether to pass the value downstream.
*/
boolean evaluate(T value);
}

/**
* A light-weight operator builder.
* <p>
Expand Down Expand Up @@ -135,4 +146,22 @@ public void unsubscribe() {
}
});
}

/**
* Only emit those values from an Observable that satisfy a predicate.
*
* @see <a href="https://material-motion.github.io/material-motion/starmap/specifications/streams/operators/$._filter">The
* filter() specification</a>
*/
public MotionObservable<T> filter(final Predicate<T> predicate) {
return operator(new Operation<T, T>() {

@Override
public void next(MotionObserver<T> observer, T value) {
if (predicate.evaluate(value)) {
observer.next(value);
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> callback;
Expand Down Expand Up @@ -77,32 +77,40 @@ public void unsubscribe() {
}
});

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

@Override
public void next(MotionObserver<CharSequence> observer, String value) {
CharSequence charSequence = italicizeAndCapitalize(value);
observer.next(charSequence);
}
}).subscribe(new MotionObserver<CharSequence>() {
@Override
public boolean evaluate(String value) {
return value != "skip";
}
})
.operator(new Operation<String, CharSequence>() {

@Override
public void next(CharSequence value) {
text.setText(value);
}
@Override
public void next(MotionObserver<CharSequence> observer, String value) {
CharSequence charSequence = italicizeAndCapitalize(value);
observer.next(charSequence);
}
}).subscribe(new MotionObserver<CharSequence>() {

@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
Expand Down

0 comments on commit e614b74

Please sign in to comment.