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

Commit 15d5c24

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

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.google.android.material.motion.streams;
1717

1818
import android.support.annotation.IntDef;
19+
import android.support.annotation.Nullable;
1920

2021
import com.google.android.material.motion.observable.IndefiniteObservable;
2122
import com.google.android.material.motion.observable.Observer;
@@ -77,4 +78,61 @@ public interface MotionObserver<T> extends Observer<T> {
7778
*/
7879
void state(@MotionState int state);
7980
}
81+
82+
/**
83+
* An operation is able to transform incoming values before choosing whether or not to pass them
84+
* downstream.
85+
*
86+
* @param <T> The incoming value type.
87+
* @param <U> The downstream value type.
88+
*/
89+
public interface Operation<T, U> {
90+
91+
/**
92+
* Modifies the given value before passing it downstream, or blocks the value.
93+
*
94+
* @param observer Downstream.
95+
* @param value The value from upstream.
96+
*/
97+
void next(MotionObserver<U> observer, T value);
98+
}
99+
100+
/**
101+
* A light-weight operator builder.
102+
* <p>
103+
* This is the preferred method for building new operators. This builder can be used to create
104+
* any operator that only needs to modify or block values. All state events are forwarded
105+
* along.
106+
*
107+
* @see <a href="https://material-motion.github.io/material-motion/starmap/specifications/streams/operators/$._operator">The
108+
* operator() specification</a>
109+
*/
110+
public <U> MotionObservable<U> operator(final Operation<T, U> operation) {
111+
final MotionObservable<T> upstream = MotionObservable.this;
112+
113+
return new MotionObservable<>(new Subscriber<MotionObserver<U>>() {
114+
@Nullable
115+
@Override
116+
public Unsubscriber subscribe(final MotionObserver<U> observer) {
117+
final Subscription subscription = upstream.subscribe(new MotionObserver<T>() {
118+
@Override
119+
public void next(T value) {
120+
operation.next(observer, value);
121+
}
122+
123+
@Override
124+
public void state(@MotionState int state) {
125+
observer.state(state);
126+
}
127+
});
128+
129+
return new Unsubscriber() {
130+
@Override
131+
public void unsubscribe() {
132+
subscription.unsubscribe();
133+
}
134+
};
135+
}
136+
});
137+
}
80138
}

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
package com.google.android.material.motion.streams.sample;
1717

1818
import android.graphics.Color;
19+
import android.graphics.Typeface;
1920
import android.os.Bundle;
2021
import android.support.annotation.Nullable;
2122
import android.support.v7.app.AppCompatActivity;
23+
import android.text.Spannable;
24+
import android.text.SpannableString;
25+
import android.text.style.StyleSpan;
2226
import android.view.View;
2327
import android.widget.Button;
2428
import android.widget.TextView;
@@ -29,6 +33,7 @@
2933
import com.google.android.material.motion.streams.MotionObservable;
3034
import com.google.android.material.motion.streams.MotionObservable.MotionObserver;
3135
import com.google.android.material.motion.streams.MotionObservable.MotionState;
36+
import com.google.android.material.motion.streams.MotionObservable.Operation;
3237

3338
import static com.google.android.material.motion.streams.MotionObservable.ACTIVE;
3439
import static com.google.android.material.motion.streams.MotionObservable.AT_REST;
@@ -55,13 +60,15 @@ protected void onCreate(Bundle savedInstanceState) {
5560
Button nextButton = (Button) findViewById(R.id.next_button);
5661
Button unsubscribeButton = (Button) findViewById(R.id.unsubscribe_button);
5762

58-
MotionObservable<String> observable = new MotionObservable<>(
63+
final MotionObservable<String> observable = new MotionObservable<>(
5964
new Subscriber<MotionObserver<String>>() {
65+
6066
@Nullable
6167
@Override
6268
public Unsubscriber subscribe(MotionObserver<String> observer) {
6369
registerButtonCallback(observer);
6470
return new Unsubscriber() {
71+
6572
@Override
6673
public void unsubscribe() {
6774
unregisterButtonCallback();
@@ -70,9 +77,17 @@ public void unsubscribe() {
7077
}
7178
});
7279

73-
final Subscription subscription = observable.subscribe(new MotionObserver<String>() {
80+
final Subscription subscription = observable.operator(new Operation<String, CharSequence>() {
81+
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>() {
88+
7489
@Override
75-
public void next(String value) {
90+
public void next(CharSequence value) {
7691
text.setText(value);
7792
}
7893

@@ -116,6 +131,12 @@ public void onClick(View v) {
116131
});
117132
}
118133

134+
private CharSequence italicizeAndCapitalize(String value) {
135+
Spannable spannable = new SpannableString(value.toUpperCase());
136+
spannable.setSpan(new StyleSpan(Typeface.ITALIC), 0, spannable.length(), 0);
137+
return spannable;
138+
}
139+
119140
private void registerButtonCallback(MotionObserver<String> observer) {
120141
callback = observer;
121142
}

0 commit comments

Comments
 (0)