-
Notifications
You must be signed in to change notification settings - Fork 226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add untilEvent overload for AndroidLifecycleScopeProvider#from #107
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
public final class AndroidLifecycleScopeProvider | ||
implements LifecycleScopeProvider<Lifecycle.Event> { | ||
|
||
private static final Function<Lifecycle.Event, Lifecycle.Event> CORRESPONDING_EVENTS = | ||
private static final Function<Lifecycle.Event, Lifecycle.Event> DEFAULT_CORRESPONDING_EVENTS = | ||
new Function<Lifecycle.Event, Lifecycle.Event>() { | ||
@Override public Lifecycle.Event apply(Lifecycle.Event lastEvent) throws Exception { | ||
switch (lastEvent) { | ||
|
@@ -54,41 +54,84 @@ public final class AndroidLifecycleScopeProvider | |
} | ||
}; | ||
|
||
private final Function<Lifecycle.Event, Lifecycle.Event> correspondingEvents; | ||
|
||
/** | ||
* Creates a {@link AndroidLifecycleScopeProvider} for Android LifecycleOwners. | ||
* | ||
* @param owner the owner to scope for | ||
* @param owner the owner to scope for. | ||
* @return a {@link AndroidLifecycleScopeProvider} against this owner. | ||
*/ | ||
public static AndroidLifecycleScopeProvider from(LifecycleOwner owner) { | ||
return from(owner.getLifecycle()); | ||
} | ||
|
||
/** | ||
* Creates a {@link AndroidLifecycleScopeProvider} for Android LifecycleOwners. | ||
* | ||
* @param owner the owner to scope for. | ||
* @param untilEvent the event until the scope is valid. | ||
* @return a {@link AndroidLifecycleScopeProvider} against this owner. | ||
*/ | ||
public static AndroidLifecycleScopeProvider from(LifecycleOwner owner, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: put both of these on the next line or at least make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed! |
||
Lifecycle.Event untilEvent) { | ||
return from(owner.getLifecycle(), untilEvent); | ||
} | ||
|
||
/** | ||
* Creates a {@link AndroidLifecycleScopeProvider} for Android Lifecycles. | ||
* | ||
* @param lifecycle the lifecycle to scope for | ||
* @param lifecycle the lifecycle to scope for. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
* @return a {@link AndroidLifecycleScopeProvider} against this lifecycle. | ||
*/ | ||
public static AndroidLifecycleScopeProvider from(Lifecycle lifecycle) { | ||
return new AndroidLifecycleScopeProvider(lifecycle); | ||
} | ||
|
||
/** | ||
* Creates a {@link AndroidLifecycleScopeProvider} for Android Lifecycles. | ||
* | ||
* @param lifecycle the lifecycle to scope for. | ||
* @param untilEvent the event until the scope is valid. | ||
* @return a {@link AndroidLifecycleScopeProvider} against this lifecycle. | ||
*/ | ||
public static AndroidLifecycleScopeProvider from(Lifecycle lifecycle, Lifecycle.Event untilEvent) { | ||
return new AndroidLifecycleScopeProvider(lifecycle, untilEvent); | ||
} | ||
|
||
private final LifecycleEventsObservable lifecycleObservable; | ||
|
||
private AndroidLifecycleScopeProvider(Lifecycle lifecycle) { | ||
this.lifecycleObservable = new LifecycleEventsObservable(lifecycle); | ||
this.correspondingEvents = DEFAULT_CORRESPONDING_EVENTS; | ||
} | ||
|
||
private AndroidLifecycleScopeProvider(Lifecycle lifecycle, Lifecycle.Event untilEvent) { | ||
this.lifecycleObservable = new LifecycleEventsObservable(lifecycle); | ||
this.correspondingEvents = new UntilEventFunction(untilEvent); | ||
} | ||
|
||
@Override public Observable<Lifecycle.Event> lifecycle() { | ||
return lifecycleObservable; | ||
} | ||
|
||
@Override public Function<Lifecycle.Event, Lifecycle.Event> correspondingEvents() { | ||
return CORRESPONDING_EVENTS; | ||
return correspondingEvents; | ||
} | ||
|
||
@Override public Lifecycle.Event peekLifecycle() { | ||
return lifecycleObservable.getValue(); | ||
} | ||
|
||
private static class UntilEventFunction implements Function<Lifecycle.Event, Lifecycle.Event> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. I'd initially thought about doing it as a filter, but with this you hook in nicely to the api There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I started trying that but could not get it to work, so came up with this approach instead. |
||
private final Lifecycle.Event untilEvent; | ||
|
||
UntilEventFunction(Lifecycle.Event untilEvent) { | ||
this.untilEvent = untilEvent; | ||
} | ||
|
||
@Override public Lifecycle.Event apply(Lifecycle.Event event) throws Exception { | ||
return untilEvent; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,15 +16,17 @@ | |
|
||
package com.uber.autodispose.sample; | ||
|
||
import android.arch.lifecycle.Lifecycle; | ||
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.util.Log; | ||
import com.uber.autodispose.AutoDispose; | ||
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider; | ||
import java.util.concurrent.TimeUnit; | ||
import io.reactivex.Observable; | ||
import io.reactivex.functions.Action; | ||
import io.reactivex.functions.Consumer; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Demo activity, shamelessly borrowed from the RxLifecycle sample | ||
|
@@ -35,7 +37,7 @@ public class MainActivity extends AppCompatActivity { | |
|
||
private static final String TAG = "AutoDispose"; | ||
|
||
@Override protected void onCreate(Bundle savedInstanceState) { | ||
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
super.onCreate(savedInstanceState); | ||
|
||
Log.d(TAG, "onCreate()"); | ||
|
@@ -95,6 +97,21 @@ public class MainActivity extends AppCompatActivity { | |
// `.<Long>forObservable` is necessary if you're compiling on JDK7 or below. | ||
// If you're using JDK8+, then you can safely remove it. | ||
.to(AutoDispose.with(AndroidLifecycleScopeProvider.from(this)).<Long>forObservable()) | ||
.subscribe(new Consumer<Long>() { | ||
@Override public void accept(Long num) throws Exception { | ||
Log.i(TAG, "Started in onResume(), running until in onPause(): " + num); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed "...running until in onDestroy" to "...running until in onPause". |
||
} | ||
}); | ||
|
||
// Setting a specific untilEvent, this should dispose in onDestroy. | ||
Observable.interval(1, TimeUnit.SECONDS) | ||
.doOnDispose(new Action() { | ||
@Override public void run() throws Exception { | ||
Log.i(TAG, "Disposing subscription from onResume() with untilEvent ON_DESTROY"); | ||
} | ||
}) | ||
.to(AutoDispose.with(AndroidLifecycleScopeProvider.from(this, | ||
Lifecycle.Event.ON_DESTROY)).<Long>forObservable()) | ||
.subscribe(new Consumer<Long>() { | ||
@Override public void accept(Long num) throws Exception { | ||
Log.i(TAG, "Started in onResume(), running until in onDestroy(): " + num); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very minor: Missing dot.