-
Notifications
You must be signed in to change notification settings - Fork 3k
Move Activity and Fragment related operations to rx.android.app #92
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
Changes from all 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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package rx.android.app; | ||
|
||
import android.app.Activity; | ||
import android.app.Fragment; | ||
import android.os.Build; | ||
|
||
import rx.Observable; | ||
import rx.android.internal.Assertions; | ||
import rx.functions.Func1; | ||
|
||
import static rx.android.schedulers.AndroidSchedulers.mainThread; | ||
|
||
public final class AppObservable { | ||
private AppObservable() { | ||
throw new AssertionError("No instances"); | ||
} | ||
|
||
static { | ||
boolean supportFragmentsAvailable = false; | ||
try { | ||
Class.forName("android.support.v4.app.Fragment"); | ||
supportFragmentsAvailable = true; | ||
} catch (ClassNotFoundException e) { | ||
} | ||
|
||
USES_SUPPORT_FRAGMENTS = supportFragmentsAvailable; | ||
} | ||
|
||
private static final Func1<Activity, Boolean> ACTIVITY_VALIDATOR = new Func1<Activity, Boolean>() { | ||
@Override | ||
public Boolean call(Activity activity) { | ||
return !activity.isFinishing(); | ||
} | ||
}; | ||
private static final Func1<Fragment, Boolean> FRAGMENT_VALIDATOR = new Func1<Fragment, Boolean>() { | ||
@Override | ||
public Boolean call(Fragment fragment) { | ||
return fragment.isAdded() && !fragment.getActivity().isFinishing(); | ||
} | ||
}; | ||
private static final Func1<android.support.v4.app.Fragment, Boolean> FRAGMENTV4_VALIDATOR = | ||
new Func1<android.support.v4.app.Fragment, Boolean>() { | ||
@Override | ||
public Boolean call(android.support.v4.app.Fragment fragment) { | ||
return fragment.isAdded() && !fragment.getActivity().isFinishing(); | ||
} | ||
}; | ||
public static final boolean USES_SUPPORT_FRAGMENTS; | ||
|
||
/** | ||
* Binds the given source sequence to an activity. | ||
* <p> | ||
* This helper will schedule the given sequence to be observed on the main UI thread and ensure | ||
* that no notifications will be forwarded to the activity in case it is scheduled to finish. | ||
* <p> | ||
* You should unsubscribe from the returned Observable in onDestroy at the latest, in order to not | ||
* leak the activity or an inner subscriber. Conversely, when the source sequence can outlive the activity, | ||
* make sure to bind to new instances of the activity again, e.g. after going through configuration changes. | ||
* Refer to the samples project for actual examples. | ||
* | ||
* @param activity the activity to bind the source sequence to | ||
* @param source the source sequence | ||
*/ | ||
public static <T> Observable<T> bindActivity(Activity activity, Observable<T> source) { | ||
Assertions.assertUiThread(); | ||
return source.observeOn(mainThread()).lift(new OperatorConditionalBinding<T, Activity>(activity, ACTIVITY_VALIDATOR)); | ||
} | ||
|
||
/** | ||
* Binds the given source sequence to a fragment (native or support-v4). | ||
* <p> | ||
* This helper will schedule the given sequence to be observed on the main UI thread and ensure | ||
* that no notifications will be forwarded to the fragment in case it gets detached from its | ||
* activity or the activity is scheduled to finish. | ||
* <p> | ||
* You should unsubscribe from the returned Observable in onDestroy for normal fragments, or in onDestroyView | ||
* for retained fragments, in order to not leak any references to the host activity or the fragment. | ||
* Refer to the samples project for actual examples. | ||
* | ||
* @param fragment the fragment to bind the source sequence to | ||
* @param source the source sequence | ||
*/ | ||
public static <T> Observable<T> bindFragment(Object fragment, Observable<T> source) { | ||
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. What's the advantage of this vs. just having two overloads? 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. Nevermind. This is copied code. I'll open an issue. 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. |
||
Assertions.assertUiThread(); | ||
final Observable<T> o = source.observeOn(mainThread()); | ||
if (USES_SUPPORT_FRAGMENTS && fragment instanceof android.support.v4.app.Fragment) { | ||
android.support.v4.app.Fragment f = (android.support.v4.app.Fragment) fragment; | ||
return o.lift(new OperatorConditionalBinding<T, android.support.v4.app.Fragment>(f, FRAGMENTV4_VALIDATOR)); | ||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && fragment instanceof Fragment) { | ||
Fragment f = (Fragment) fragment; | ||
return o.lift(new OperatorConditionalBinding<T, Fragment>(f, FRAGMENT_VALIDATOR)); | ||
} else { | ||
throw new IllegalArgumentException("Target fragment is neither a native nor support library Fragment"); | ||
} | ||
} | ||
} |
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.
Is there reasoning behind these names being singular? Normally utility classes are plural (e.g.,
Collections.emptyList()
).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.
I guess it came from RxSwing's convention. If we ditch the pattern, we could pick something much shorter. rxjava-async-util has
Async
, for example.