Skip to content

Commit

Permalink
Merge pull request #5 from Reyurnible/2.0.0
Browse files Browse the repository at this point in the history
version 2.0.0
  • Loading branch information
hosshan authored Jan 29, 2017
2 parents a4fdb1c + 23e5602 commit f143f34
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 178 deletions.
72 changes: 30 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ This is Animation wrapping Observable.
You can get event Observable.
And you can call animation chain more simple.

for 2.x.x is Using [RxJava2](https://github.com/ReactiveX/RxJava)

## Download
Animation bindings

```
compile 'com.reyurnible.rxanimation:rxanimations:1.0.2'
compile 'com.reyurnible.rxanimation:rxanimations:2.0.0'
```

using Kotlin:

```
compile 'com.reyurnible.rxanimation:rxanimations-kotlin:1.0.2'
compile 'com.reyurnible.rxanimation:rxanimations-kotlin:2.0.0'
```

## Public Classes
Expand All @@ -33,15 +35,7 @@ in Java
TextView textView = (TextView) findViewById(R.id.textView);
Animation animation = new ScaleAnimation(0.0f, 0.0f, 1.0f, 1.0f);
RxAnimation.events(animation, textView)
.subscribe(new Subscriber<AnimationEvent>() {
@Override public void onCompleted() {
}
@Override public void onError(Throwable e) {
}
.subscribe {
@Override public void onNext(AnimationEvent animationEvent) {
switch (animationEvent.kind()) {
case START:
Expand All @@ -52,37 +46,27 @@ RxAnimation.events(animation, textView)
break;
}
}
});
};
```

in Kotlin

```
val view: TextView = findViewById(R.id.textView) as TextView
val animation: Animation = ScaleAnimation(0.0f, 0.0f, 1.0f, 1.0f)
animation.bindView(view).subscribe(object : Subscriber<AnimationEvent>() {
override fun onCompleted() {
}
override fun onNext(t: AnimationEvent?) {
when(t!!.kind()) {
AnimationEvent.Kind.START -> {
// START
}
AnimationEvent.Kind.END -> {
// END
}
AnimationEvent.Kind.REPEAT -> {
// REPEAT
}
animation.bindView(view).subscribe { t ->
when(t.kind()) {
AnimationEvent.Kind.START -> {
// START
}
AnimationEvent.Kind.END -> {
// END
}
AnimationEvent.Kind.REPEAT -> {
// REPEAT
}
}
override fun onError(e: Throwable?) {
}
})
}
```

### Combine Usage
Expand All @@ -102,15 +86,19 @@ private fun startAnimations() {
Log.d(TAG, it.toString())
it.kind() == AnimatorEvent.Kind.END
}
Observable.combineLatest(translateAnimatorObserver, alphaAnimationObserver, { t1, t2 ->
Log.d(TAG, "T1:" + t1.toString())
Log.d(TAG, "T2:" + t2.toString())
t1.and(t2)
}).filter { it }.subscribe { validate ->
// post event
Log.d(TAG, "End All Animation")
}
Observable.combineLatest(
translateAnimatorObserver,
alphaAnimationObserver,
BiFunction<Boolean, Boolean, Boolean> { t1, t2 ->
Log.d(TAG, "T1:" + t1.toString())
Log.d(TAG, "T2:" + t2.toString())
t1.and(t2)
})
.filter { it }
.subscribe { validate ->
// post event
Log.d(TAG, "End All Animation")
}
}
```

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ ext {
junit = 'junit:junit:4.12'
truth = 'com.google.truth:truth:0.27'

rxJava = 'io.reactivex:rxjava:1.2.5'
rxAndroid = 'io.reactivex:rxandroid:1.2.1'
rxJava = 'io.reactivex.rxjava2:rxjava:2.0.5'
rxAndroid = 'io.reactivex.rxjava2:rxandroid:2.0.1'

bintrayRelease = 'com.novoda:bintray-release:0.3.4'

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GROUP_ID=com.reyurnible.rxanimation
VERSION_NAME=1.0.2
VERSION_NAME=2.0.0

# Project-wide Gradle settings.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import android.view.View
import android.view.animation.Animation
import com.reyurnible.rxanimation.animation.AnimationEvent
import com.reyurnible.rxanimation.animation.RxAnimation
import rx.Observable
import io.reactivex.Observable

fun Animation.events(view: View): Observable<AnimationEvent> = RxAnimation.events(this, view)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.reyurnible.rxanimation.animator.AnimatorEvent
import com.reyurnible.rxanimation.animator.AnimatorPauseEvent
import com.reyurnible.rxanimation.animator.AnimatorUpdateEvent
import com.reyurnible.rxanimation.animator.RxAnimator
import rx.Observable
import io.reactivex.Observable

fun Animator.events(): Observable<AnimatorEvent> = RxAnimator.events(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

import rx.Observer;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.google.common.truth.Truth.assertThat;

Expand All @@ -17,7 +19,12 @@ public final class RecordingObserver<T> implements Observer<T> {
private final BlockingDeque<Object> events = new LinkedBlockingDeque<>();

@Override
public void onCompleted() {
public void onSubscribe(Disposable d) {

}

@Override
public void onComplete() {
Log.v(TAG, "onCompleted");
events.addLast(new OnCompleted());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import io.reactivex.android.schedulers.AndroidSchedulers;

import static com.google.common.truth.Truth.assertThat;

Expand All @@ -43,8 +42,7 @@ public void events() throws InterruptedException {
final AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
alphaAnimation.setStartOffset(0);
alphaAnimation.setDuration(100);

final Subscription subscription = RxAnimation.events(alphaAnimation, child).subscribeOn(AndroidSchedulers.mainThread()).subscribe(o);
RxAnimation.events(alphaAnimation, child).subscribeOn(AndroidSchedulers.mainThread()).subscribe(o);
{ // ロードが完了するまで待つ
waitForLoadingFinished(500);
// Stack Start event
Expand All @@ -57,7 +55,6 @@ public void events() throws InterruptedException {
waitForLoadingFinished(2000);
o.assertOnCompleted();
}
subscription.unsubscribe();
o.assertNoMoreEvents();
}

Expand All @@ -69,7 +66,7 @@ public void repeat() throws InterruptedException {
alphaAnimation.setStartOffset(0);
alphaAnimation.setDuration(100);
alphaAnimation.setRepeatCount(2);
final Subscription subscription = RxAnimation.events(alphaAnimation, child).subscribeOn(AndroidSchedulers.mainThread()).subscribe(o);
RxAnimation.events(alphaAnimation, child).subscribeOn(AndroidSchedulers.mainThread()).subscribe(o);
{ // ロードが完了するまで待つ
waitForLoadingFinished(500);
// Stack Start event
Expand All @@ -86,7 +83,6 @@ public void repeat() throws InterruptedException {
waitForLoadingFinished(2000);
o.assertOnCompleted();
}
subscription.unsubscribe();
o.assertNoMoreEvents();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import io.reactivex.android.schedulers.AndroidSchedulers;

import static com.google.common.truth.Truth.assertThat;

Expand All @@ -45,7 +44,7 @@ public void events() throws InterruptedException {
ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(child, "alpha", 0f, 1f);
alphaAnimator.setDuration(100);

final Subscription subscription = RxAnimator.events(alphaAnimator).subscribeOn(AndroidSchedulers.mainThread()).subscribe(o);
RxAnimator.events(alphaAnimator).subscribeOn(AndroidSchedulers.mainThread()).subscribe(o);
{ // ロードが完了するまで待つ
waitForLoadingFinished(500);
// Stack Start event
Expand All @@ -58,7 +57,6 @@ public void events() throws InterruptedException {
waitForLoadingFinished(2000);
o.assertOnCompleted();
}
subscription.unsubscribe();
o.assertNoMoreEvents();
}

Expand All @@ -69,7 +67,7 @@ public void updates() throws InterruptedException {
ValueAnimator animator = ValueAnimator.ofInt(0, 3);
animator.setDuration(100);

final Subscription subscription = RxAnimator.updateEvents(animator).subscribeOn(AndroidSchedulers.mainThread()).subscribe(o);
RxAnimator.updateEvents(animator).subscribeOn(AndroidSchedulers.mainThread()).subscribe(o);
{ // ロードが完了するまで待つ
waitForLoadingFinished(500);
// Event 0
Expand All @@ -79,7 +77,6 @@ public void updates() throws InterruptedException {
// Event 2
assertThat(o.takeNext() != null);
}
subscription.unsubscribe();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
import android.view.View;
import android.view.animation.Animation;

import com.reyurnible.rxanimation.internal.MainThreadSubscription;

import rx.Observable;
import rx.Subscriber;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Cancellable;

import static com.reyurnible.rxanimation.internal.Preconditions.checkUiThread;

/**
* A animation event observer.
*/
final class AnimationEventOnSubscribe implements Observable.OnSubscribe<AnimationEvent> {
final class AnimationEventOnSubscribe implements ObservableOnSubscribe<AnimationEvent> {
private final Animation animation;
private final View view;

Expand All @@ -23,29 +22,29 @@ final class AnimationEventOnSubscribe implements Observable.OnSubscribe<Animatio
}

@Override
public void call(final Subscriber<? super AnimationEvent> subscriber) {
public void subscribe(final ObservableEmitter<AnimationEvent> emitter) throws Exception {
checkUiThread();

Animation.AnimationListener listener = new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(AnimationEvent.create(animation, AnimationEvent.Kind.START));
if (!emitter.isDisposed()) {
emitter.onNext(AnimationEvent.create(animation, AnimationEvent.Kind.START));
}
}

@Override
public void onAnimationEnd(Animation animation) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(AnimationEvent.create(animation, AnimationEvent.Kind.END));
subscriber.onCompleted();
if (!emitter.isDisposed()) {
emitter.onNext(AnimationEvent.create(animation, AnimationEvent.Kind.END));
emitter.onComplete();
}
}

@Override
public void onAnimationRepeat(Animation animation) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(AnimationEvent.create(animation, AnimationEvent.Kind.REPEAT));
if (!emitter.isDisposed()) {
emitter.onNext(AnimationEvent.create(animation, AnimationEvent.Kind.REPEAT));
}
}
};
Expand All @@ -54,13 +53,11 @@ public void onAnimationRepeat(Animation animation) {
// subscribeするタイミングはユーザーが決められるのでsetではなくstartにする
view.startAnimation(animation);

subscriber.add(new MainThreadSubscription() {
emitter.setCancellable(new Cancellable() {
@Override
protected void onUnSubscribe() {
public void cancel() throws Exception {
animation.setAnimationListener(null);
}
});
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import android.view.View;
import android.view.animation.Animation;

import rx.Observable;
import io.reactivex.Observable;

/**
* Static factory methods for animation event observable.
Expand Down
Loading

0 comments on commit f143f34

Please sign in to comment.