Last active
December 1, 2016 09:10
-
-
Save rovkinmax/c2889ee04683cf21c5a9ba0113270323 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LifecycleProvider { | |
private var disposable: Disposable? = null | |
private var observable: BehaviorSubject<Any>? = null | |
internal fun unsubscribe(): Unit { | |
if (disposable?.isDisposed?.not() ?: false) { | |
observable?.onNext(Any()) | |
disposable?.dispose() | |
} | |
disposable = null | |
observable = null | |
} | |
fun <T> lifecycle(): LifecycleTransformer<T> { | |
if (observable == null || disposable == null) { | |
observable = BehaviorSubject.create() | |
disposable = observable!!.subscribe {} | |
} | |
return LifecycleTransformer(observable!!) | |
} | |
companion object { | |
fun get(fm: FragmentManager): LifecycleProvider { | |
val fragment = fm.findFragmentByTag(BundleFragment::class.java.name) as BundleFragment? ?: throw NullPointerException("BundleFragment not attached to FragmentManager") | |
return fragment.provider | |
} | |
} | |
} | |
class LifecycleTransformer<T>(private val observable: Observable<Any>) : ObservableTransformer<T, T>, SingleTransformer<T, T>, FlowableTransformer<T, T> { | |
override fun apply(upstream: Flowable<T>): Publisher<T> = upstream.takeUntil(observable.toFlowable(BackpressureStrategy.LATEST)) | |
override fun apply(upstream: Single<T>): SingleSource<T> = upstream.takeUntil(observable.firstOrError()) | |
override fun apply(upstream: Observable<T>): ObservableSource<T> = upstream.takeUntil(observable) | |
} | |
class BundleFragment : Fragment() { | |
internal val provider = LifecycleProvider() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
retainInstance = true | |
} | |
override fun onStop() { | |
provider.unsubscribe() | |
super.onStop() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Lifecycler : Application.ActivityLifecycleCallbacks { | |
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { | |
initLifecycleProvider(activity) | |
} | |
private fun initLifecycleProvider(activity: Activity) { | |
val fm = activity.fragmentManager | |
if (fm.findFragmentByTag(BundleFragment::class.java.name) == null) { | |
fm.beginTransaction().add(BundleFragment(), BundleFragment::class.java.name).commitAllowingStateLoss() | |
fm.executePendingTransactions() | |
} | |
} | |
override fun onActivityStarted(activity: Activity) { | |
} | |
override fun onActivityResumed(activity: Activity) { | |
} | |
override fun onActivityPaused(activity: Activity) { | |
} | |
override fun onActivityStopped(activity: Activity) { | |
} | |
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) { | |
} | |
override fun onActivityDestroyed(activity: Activity) { | |
} | |
companion object { | |
fun register(app: Application) { | |
app.registerActivityLifecycleCallbacks(Lifecycler()) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class App : Application() { | |
override fun onCreate() { | |
super.onCreate() | |
Lifecycler.register(this) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
observable | |
.async() | |
.compose(LifecycleProvider.get(fragmentManager).lifecycle()) | |
.subscribe(Consumer { }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class RxLifecycleFragment : Fragment() { | |
protected val lifecycleProvider = LifecycleProvider() | |
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { | |
observable | |
.async() | |
.compose(LifecycleProvider.get(fragmentManager).lifecycle()) | |
.subscribe(Consumer { }) | |
} | |
override fun onStop() { | |
lifecycleProvider.unsubscribe() | |
super.onStop() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment