From 02902b7cf11036deeb546cc4cf4137a89e9efbd4 Mon Sep 17 00:00:00 2001 From: Mark Wei Date: Wed, 7 Dec 2016 18:39:45 -0800 Subject: [PATCH] Implementation of write() in android. Summary: Fixes https://github.com/material-motion/streams-android/issues/5 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/D2160 --- .../motion/streams/MotionObservable.java | 21 +++++++++++++++++-- .../motion/streams/sample/MainActivity.java | 17 ++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/google/android/material/motion/streams/MotionObservable.java b/library/src/main/java/com/google/android/material/motion/streams/MotionObservable.java index 6b64579..c9aeb8b 100644 --- a/library/src/main/java/com/google/android/material/motion/streams/MotionObservable.java +++ b/library/src/main/java/com/google/android/material/motion/streams/MotionObservable.java @@ -17,6 +17,7 @@ import android.support.annotation.IntDef; import android.support.annotation.Nullable; +import android.util.Property; import com.google.android.material.motion.observable.IndefiniteObservable; import com.google.android.material.motion.observable.Observer; @@ -161,7 +162,7 @@ public void unsubscribe() { } /** - * Transform the items emitted by an Observable by applying a function to each item. + * Transforms the items emitted by an Observable by applying a function to each item. * * @see The * filter() specification @@ -176,7 +177,7 @@ public void next(MotionObserver observer, T value) { } /** - * Only emit those values from an Observable that satisfy a predicate. + * Only emits those values from an Observable that satisfy a predicate. * * @see The * filter() specification @@ -191,4 +192,20 @@ public void next(MotionObserver observer, T value) { } }); } + + /** + * Writes the values from an Observable onto the given target and property. + * + * @see The + * write() specification + */ + public MotionObservable write(final O target, final Property property) { + return operator(new Operation() { + @Override + public void next(MotionObserver observer, T value) { + property.set(target, value); + observer.next(value); + } + }); + } } diff --git a/sample/src/main/java/com/google/android/material/motion/streams/sample/MainActivity.java b/sample/src/main/java/com/google/android/material/motion/streams/sample/MainActivity.java index 6bc7080..1b602b0 100644 --- a/sample/src/main/java/com/google/android/material/motion/streams/sample/MainActivity.java +++ b/sample/src/main/java/com/google/android/material/motion/streams/sample/MainActivity.java @@ -23,6 +23,7 @@ import android.text.Spannable; import android.text.SpannableString; import android.text.style.StyleSpan; +import android.util.Property; import android.view.View; import android.widget.Button; import android.widget.TextView; @@ -91,11 +92,12 @@ public CharSequence transform(String value) { return italicizeAndCapitalize(value); } }) + .write(text, TEXT_PROPERTY) .subscribe(new MotionObserver() { @Override public void next(CharSequence value) { - text.setText(value); + // No-op. Handled by write() above. } @Override @@ -151,4 +153,17 @@ private void registerButtonCallback(MotionObserver observer) { private void unregisterButtonCallback() { callback = null; } + + private static Property TEXT_PROPERTY = new Property(CharSequence.class, "text") { + + @Override + public CharSequence get(TextView object) { + return object.getText(); + } + + @Override + public void set(TextView object, CharSequence value) { + object.setText(value); + } + }; }