Created
October 24, 2018 08:01
-
-
Save sagix/3011621f6b109644222db08fc724abaf to your computer and use it in GitHub Desktop.
Mutable decorator with lifecyle extensions
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
@file:JvmName("MutableDecoratorUtils") | |
package net.ilius.android.core.android | |
import android.arch.lifecycle.Lifecycle | |
import android.arch.lifecycle.LifecycleObserver | |
import android.arch.lifecycle.LifecycleOwner | |
import android.arch.lifecycle.OnLifecycleEvent | |
import com.nicolasmouchel.executordecorator.MutableDecorator | |
typealias MutableObserverWrapper<T> = (T, MutableDecorator<T>) -> LifecycleObserver | |
typealias MutableObserver<T> = () -> MutableObserverWrapper<T> | |
/** | |
* Will create the [LifecycleObserver] mutating the decorated value between <b>onCreate</b> and <b>onDestroy</b> | |
*/ | |
fun <T> observeOnCreate(): MutableObserverWrapper<T> = ::CreateMutableDecoratorLifecycle | |
/** | |
* Will create the [LifecycleObserver] mutating the decorated value between <b>onStart</b> and <b>onStop</b> | |
*/ | |
fun <T> observerOnStart(): MutableObserverWrapper<T> = ::StartMutableDecoratorLifecycle | |
/** | |
* Will create the [LifecycleObserver] mutating the decorated value between <b>onResume</b> and <b>onPause</b> | |
*/ | |
fun <T> observeOnResume(): MutableObserverWrapper<T> = ::ResumeMutableDecoratorLifecycle | |
/** | |
* Returns a [MutableDecorator] that respect the lifecycle given | |
* by mutating the decorated value between <b>onCreate</b> and <b>onDestroy</b> | |
* @param owner the lifecycle to respect | |
*/ | |
fun <T> MutableDecorator<T>.onCreate(owner: LifecycleOwner): MutableDecorator<T> = | |
on(owner, ::observeOnCreate) | |
/** | |
* Returns a [MutableDecorator] that respect the lifecycle given | |
* by mutating the decorated value between <b>onStart</b> and <b>onStop</b> | |
* @param owner the lifecycle to respect | |
*/ | |
fun <T> MutableDecorator<T>.onStart(owner: LifecycleOwner): MutableDecorator<T> = | |
on(owner, ::observerOnStart) | |
/** | |
* Returns a [MutableDecorator] that respect the lifecycle given | |
* by mutating the decorated value between <b>onResume</b> and <b>onPause</b> | |
* @param owner the lifecycle to respect | |
*/ | |
fun <T> MutableDecorator<T>.onResume(owner: LifecycleOwner): MutableDecorator<T> = | |
on(owner, ::observeOnResume) | |
/** | |
* Returns a [MutableDecorator] that respect the lifecycle given | |
* by mutating the decorated value following the observer | |
* @param owner the lifecycle to respect | |
* @param observer will create the [LifecycleObserver] with the decorator and decorated | |
* @sample onCreate | |
* @sample onStart | |
* @sample onResume | |
*/ | |
fun <T> MutableDecorator<T>.on( | |
owner: LifecycleOwner, observer: MutableObserver<T> | |
): MutableDecorator<T> { | |
return object : MutableDecorator<T> by this { | |
override fun mutate(decorated: T) { | |
owner.lifecycle.addObserver(observer().invoke(decorated, this@on)) | |
} | |
} | |
} | |
internal class CreateMutableDecoratorLifecycle<T>( | |
private val decorated: T, private val decorator: MutableDecorator<T> | |
) : LifecycleObserver { | |
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE) fun onCreate() { | |
decorator.mutate(decorated) | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) fun onDestroy() { | |
decorator.mutate(null) | |
} | |
} | |
internal class StartMutableDecoratorLifecycle<T>( | |
private val decorated: T, private val decorator: MutableDecorator<T> | |
) : LifecycleObserver { | |
@OnLifecycleEvent(Lifecycle.Event.ON_START) fun onStart() { | |
decorator.mutate(decorated) | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_STOP) fun onStop() { | |
decorator.mutate(null) | |
} | |
} | |
internal class ResumeMutableDecoratorLifecycle<T>( | |
private val decorated: T, private val decorator: MutableDecorator<T> | |
) : LifecycleObserver { | |
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun onResume() { | |
decorator.mutate(decorated) | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun onPause() { | |
decorator.mutate(null) | |
} | |
} | |
@Suppress("unused") @Deprecated( | |
message = "Should use lifecycle related methods", replaceWith = ReplaceWith( | |
expression = "decorator.onStart(this).mutate(decorated)" | |
) | |
) fun <T> LifecycleOwner.addDecorator(decorated: T, decorator: MutableDecorator<T>) { | |
decorator.onStart(this).mutate(decorated) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment