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

Commit

Permalink
Implement map() for android.
Browse files Browse the repository at this point in the history
Summary: Fixes #2

Reviewers: featherless, O2 Material Motion, O6 Material Motion Android platform reviewers

Reviewed By: featherless, O2 Material Motion, O6 Material Motion Android platform reviewers

Subscribers: featherless

Tags: #material_motion

Differential Revision: http://codereview.cc/D2157
  • Loading branch information
Mark Wei committed Dec 8, 2016
1 parent 0b5a37a commit 7faedcc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,36 +74,49 @@ public interface MotionObserver<T> extends Observer<T> {
void next(T value);

/**
* A method to handle new state values from upstream.
* A method to handle new incoming state values.
*/
void state(@MotionState int state);
}

/**
* An operation is able to transform incoming values before choosing whether or not to pass them
* downstream.
* to the observer.
*
* @param <T> The incoming value type.
* @param <U> The downstream value type.
* @param <U> The observer value type.
*/
public interface Operation<T, U> {

/**
* Modifies the given value before passing it downstream, or blocks the value.
* Transforms the incoming value before passing it to the observer, or blocks the value.
*
* @param observer Downstream.
* @param value The value from upstream.
* @param value The incoming value.
*/
void next(MotionObserver<U> observer, T value);
}

/**
* A transformation transforms incoming values before they are passed downstream.
*
* @param <T> The incoming value type.
* @param <U> The downstream value type.
*/
public interface Transformation<T, U> {

/**
* Transforms the given value.
*/
U transform(T value);
}

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

/**
* Evaluates whether to pass the value downstream.
* Evaluates whether to pass the value.
*/
boolean evaluate(T value);
}
Expand Down Expand Up @@ -147,6 +160,21 @@ public void unsubscribe() {
});
}

/**
* Transform the items emitted by an Observable by applying a function to each item.
*
* @see <a href="https://material-motion.github.io/material-motion/starmap/specifications/streams/operators/$._map">The
* filter() specification</a>
*/
public <U> MotionObservable<U> map(final Transformation<T, U> transformation) {
return operator(new Operation<T, U>() {
@Override
public void next(MotionObserver<U> observer, T value) {
observer.next(transformation.transform(value));
}
});
}

/**
* Only emit those values from an Observable that satisfy a predicate.
*
Expand All @@ -155,7 +183,6 @@ public void unsubscribe() {
*/
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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.google.android.material.motion.streams.MotionObservable;
import com.google.android.material.motion.streams.MotionObservable.MotionObserver;
import com.google.android.material.motion.streams.MotionObservable.MotionState;
import com.google.android.material.motion.streams.MotionObservable.Operation;

import static com.google.android.material.motion.streams.MotionObservable.ACTIVE;
import static com.google.android.material.motion.streams.MotionObservable.AT_REST;
Expand Down Expand Up @@ -85,14 +84,14 @@ public boolean evaluate(String value) {
return value != "skip";
}
})
.operator(new Operation<String, CharSequence>() {
.map(new MotionObservable.Transformation<String, CharSequence>() {

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

@Override
public void next(CharSequence value) {
Expand Down

0 comments on commit 7faedcc

Please sign in to comment.