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

Commit

Permalink
Implement operator() for android.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Mark Wei committed Dec 8, 2016
1 parent c4c5e45 commit 15d5c24
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.android.material.motion.streams;

import android.support.annotation.IntDef;
import android.support.annotation.Nullable;

import com.google.android.material.motion.observable.IndefiniteObservable;
import com.google.android.material.motion.observable.Observer;
Expand Down Expand Up @@ -77,4 +78,61 @@ public interface MotionObserver<T> extends Observer<T> {
*/
void state(@MotionState int state);
}

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

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

/**
* A light-weight operator builder.
* <p>
* This is the preferred method for building new operators. This builder can be used to create
* any operator that only needs to modify or block values. All state events are forwarded
* along.
*
* @see <a href="https://material-motion.github.io/material-motion/starmap/specifications/streams/operators/$._operator">The
* operator() specification</a>
*/
public <U> MotionObservable<U> operator(final Operation<T, U> operation) {
final MotionObservable<T> upstream = MotionObservable.this;

return new MotionObservable<>(new Subscriber<MotionObserver<U>>() {
@Nullable
@Override
public Unsubscriber subscribe(final MotionObserver<U> observer) {
final Subscription subscription = upstream.subscribe(new MotionObserver<T>() {
@Override
public void next(T value) {
operation.next(observer, value);
}

@Override
public void state(@MotionState int state) {
observer.state(state);
}
});

return new Unsubscriber() {
@Override
public void unsubscribe() {
subscription.unsubscribe();
}
};
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
package com.google.android.material.motion.streams.sample;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
Expand All @@ -29,6 +33,7 @@
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 All @@ -55,13 +60,15 @@ protected void onCreate(Bundle savedInstanceState) {
Button nextButton = (Button) findViewById(R.id.next_button);
Button unsubscribeButton = (Button) findViewById(R.id.unsubscribe_button);

MotionObservable<String> observable = new MotionObservable<>(
final MotionObservable<String> observable = new MotionObservable<>(
new Subscriber<MotionObserver<String>>() {

@Nullable
@Override
public Unsubscriber subscribe(MotionObserver<String> observer) {
registerButtonCallback(observer);
return new Unsubscriber() {

@Override
public void unsubscribe() {
unregisterButtonCallback();
Expand All @@ -70,9 +77,17 @@ public void unsubscribe() {
}
});

final Subscription subscription = observable.subscribe(new MotionObserver<String>() {
final Subscription subscription = observable.operator(new Operation<String, CharSequence>() {

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

@Override
public void next(String value) {
public void next(CharSequence value) {
text.setText(value);
}

Expand Down Expand Up @@ -116,6 +131,12 @@ public void onClick(View v) {
});
}

private CharSequence italicizeAndCapitalize(String value) {
Spannable spannable = new SpannableString(value.toUpperCase());
spannable.setSpan(new StyleSpan(Typeface.ITALIC), 0, spannable.length(), 0);
return spannable;
}

private void registerButtonCallback(MotionObserver<String> observer) {
callback = observer;
}
Expand Down

0 comments on commit 15d5c24

Please sign in to comment.