Skip to content

Instantly share code, notes, and snippets.

@sagix
Created December 21, 2017 20:12
Show Gist options
  • Save sagix/457904ab8f41da5d91d343cc915abbd1 to your computer and use it in GitHub Desktop.
Save sagix/457904ab8f41da5d91d343cc915abbd1 to your computer and use it in GitHub Desktop.
@file:JvmName("MutableDecoratorUtils")
package com.nicolasmouchel.executordecorator.android.arch.lifecycle
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
@Suppress("unused")
class MutableDecoratorLifecycle<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)
}
}
@Suppress("unused")
fun <T> LifecycleOwner.addDecorator(decorated: T, decorator: MutableDecorator<T>) {
lifecycle.addObserver(MutableDecoratorLifecycle(decorated, decorator))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment