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

Commit

Permalink
Implementation of write() in android.
Browse files Browse the repository at this point in the history
Summary: Fixes #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
  • Loading branch information
Mark Wei committed Dec 8, 2016
1 parent 7faedcc commit 02902b7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <a href="https://material-motion.github.io/material-motion/starmap/specifications/streams/operators/$._map">The
* filter() specification</a>
Expand All @@ -176,7 +177,7 @@ public void next(MotionObserver<U> 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 <a href="https://material-motion.github.io/material-motion/starmap/specifications/streams/operators/$._filter">The
* filter() specification</a>
Expand All @@ -191,4 +192,20 @@ public void next(MotionObserver<T> observer, T value) {
}
});
}

/**
* Writes the values from an Observable onto the given target and property.
*
* @see <a href="https://material-motion.github.io/material-motion/starmap/specifications/streams/operators/$.write">The
* write() specification</a>
*/
public <O> MotionObservable<T> write(final O target, final Property<O, T> property) {
return operator(new Operation<T, T>() {
@Override
public void next(MotionObserver<T> observer, T value) {
property.set(target, value);
observer.next(value);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,11 +92,12 @@ public CharSequence transform(String value) {
return italicizeAndCapitalize(value);
}
})
.write(text, TEXT_PROPERTY)
.subscribe(new MotionObserver<CharSequence>() {

@Override
public void next(CharSequence value) {
text.setText(value);
// No-op. Handled by write() above.
}

@Override
Expand Down Expand Up @@ -151,4 +153,17 @@ private void registerButtonCallback(MotionObserver<String> observer) {
private void unregisterButtonCallback() {
callback = null;
}

private static Property<TextView, CharSequence> TEXT_PROPERTY = new Property<TextView, CharSequence>(CharSequence.class, "text") {

@Override
public CharSequence get(TextView object) {
return object.getText();
}

@Override
public void set(TextView object, CharSequence value) {
object.setText(value);
}
};
}

0 comments on commit 02902b7

Please sign in to comment.