-
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
Could you pls provide some sample? #80
Comments
What would you want to see in a sample? Basically, for every RxJava type (Observable, Single, etc) there's a corresponding
ScopeProvider is an abstraction that allows non-rxjava types to provide their own scope. LifecycleScopeProvider is something a bit more complex that allows you to have multiple lifecycle events and scope against their symmetric destruction events (aka create-destroy, start-stop, etc). Still an interface though, so you could define your own lifecycle (different architectures like Conductor, etc). I may pull this out to a separate component in the future though. You simply use rxjava's So something like this: myStringObservable
.to(new ObservableScoper<String>(myScope))
.subscribe(); |
What about just a simple Android app? |
@hzsweers thank you for the answer, I am still not very clear, what's myScope in the code you give? |
Would you like to contribute a sample in a PR? This is a java project first and foremost, so I hadn't really thought to add an android sample for fear of making people think it's just for android. If we just called it It can be whatever you want. Something that implements |
@hzsweers public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
private MaybeSubject<String> maybeScope = MaybeSubject.create();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate called");
Observable.interval(1, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnDispose(new Action() {
@Override
public void run() throws Exception {
Log.d(TAG, "Dispose called");
}
})
.to(new ObservableScoper<Long>(maybeScope))
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long count) throws Exception {
Log.d(TAG, "Started from onCreate, running: " + count);
}
});
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause called (Observable is still running)");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy called (dispose should be called now)");
maybeScope.onSuccess("Done");
}
} |
Yep! That's a good simple example. You would likely further abstract this by pushing it into a enum ActivityEvent {
CREATE, START, RESUME, PAUSE, STOP, DESTROY;
}
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
private BehaviorSubject<ActivityEvent> lifecycle = BehaviorSubject.create();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lifecycle.onNext(CREATE);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate called");
Observable.interval(1, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnDispose(new Action() {
@Override
public void run() throws Exception {
Log.d(TAG, "Dispose called");
}
})
.to(new ObservableScoper<Long>(lifecycle.filter(e -> e == DESTROY).firstElement()))
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long count) throws Exception {
Log.d(TAG, "Started from onCreate, running: " + count);
}
});
}
@Override
protected void onStart() {
super.onStart();
lifecycle.onNext(START);
}
@Override
protected void onResume() {
super.onResume();
lifecycle.onNext(RESUME);
}
@Override
protected void onPause() {
super.onPause();
lifecycle.onNext(PAUSE);
Log.d(TAG, "onPause called (Observable is still running)");
}
@Override
protected void onStop() {
super.onStop();
lifecycle.onNext(STOP);
}
@Override
protected void onDestroy() {
lifecycle.onNext(DESTROY);
super.onDestroy();
Log.d(TAG, "onDestroy called (dispose should be called now)");
}
} You can wrap that up into nice helper methods like: protected Maybe<?> until(ActivityEvent event) {
return lifecycle.filter(e -> e == event).firstElement();
}
// then your activity subclasses could do this
someObservable
.to(new ObservableScoper(until(DESTROY))) Or you could use the RxLifecycle-style automatic symmetry with public class MainActivity extends AppCompatActivity implements LifecycleScopeProvider<ActivityEvent> {
private static final Function<ActivityEvent, ActivityEvent> CORRESPONDING_EVENTS =
new Function<ActivityEvent, ActivityEvent>() {
@Override public ActivityEvent apply(ActivityEvent lastEvent) throws Exception {
switch (lastEvent) {
case CREATE:
return DESTROY;
case START:
return STOP;
case RESUME:
return PAUSE;
case PAUSE:
case STOP:
return DESTROY;
case DESTROY:
throw new OutsideLifecycleException("Activity is already destroyed!");
}
}
};
public static final String TAG = MainActivity.class.getSimpleName();
private BehaviorSubject<ActivityEvent> lifecycle = BehaviorSubject.create();
@Override
public Observable<ActivityEvent> lifecycle() {
return lifecycle.hide();
}
@Override
public ActivityEvent peekLifecycle() {
return lifecycle.getValue();
}
@Override
public Function<ActivityEvent, ActivityEvent> correspondingEvents() {
return CORRESPONDING_EVENTS;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lifecycle.onNext(CREATE);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate called");
Observable.interval(1, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnDispose(new Action() {
@Override
public void run() throws Exception {
Log.d(TAG, "Dispose called");
}
})
.to(new ObservableScoper<Long>(this)) // The important part
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long count) throws Exception {
Log.d(TAG, "Started from onCreate, running: " + count);
}
});
}
@Override
protected void onStart() {
super.onStart();
lifecycle.onNext(START);
}
@Override
protected void onResume() {
super.onResume();
lifecycle.onNext(RESUME);
}
@Override
protected void onPause() {
super.onPause();
lifecycle.onNext(PAUSE);
Log.d(TAG, "onPause called (Observable is still running)");
}
@Override
protected void onStop() {
super.onStop();
lifecycle.onNext(STOP);
}
@Override
protected void onDestroy() {
lifecycle.onNext(DESTROY);
super.onDestroy();
Log.d(TAG, "onDestroy called (dispose should be called now)");
}
} |
@hzsweers Now that's a good sample 👍 Thanks |
Could you pls provide some sample? I still not very clear to use it from read me.
The text was updated successfully, but these errors were encountered: