Skip to content

Instantly share code, notes, and snippets.

View SeongUgJung's full-sized avatar

Steve SeongUg Jung SeongUgJung

View GitHub Profile
// 기존
lifecycleController.addObserver(onInit { Observable.create().subscribe() })
// 코드 추가
interface LifecycleDelegate { operator fun LifecycleObserver.unaryPlus() }
class LifecycleController : LifecycleDelegate {
fun LifecycleObserver.unaryPlus() {
[email protected] += this
}
}
// 기존
RxLifecycle().apply { init = Observable.create().subscribe() }
// 코드 추가
fun onInit(disposableFunction: () -> disposable): RxLifecycle
class RxLifecycle : LifecycleObserver {
fun init(init: DisposableFunction): RxLifecycle {
this.onInit = init
class MainActivity : Activity() {
private val lifecycleController = LifecycleController()
fun onCreated() {
MainViewModel(controller)
}
}
// Controller 는 Activity 에서 주입됩니다.
class MainViewModel(private val controller : LifecycleController) {
typealias DisposableFunction = () -> Disposable // e.g) Observable.just().subscrbie()
class RxLifecycle : LifecycleObserver {
private var onInit: DisposableFunction? = null
private var initDisposable: Disposable? = null
override fun onInit() {
// create 시점에 Stream 실행
initDisposable = this.onInit?.invoke()
@SeongUgJung
SeongUgJung / LifecycleController.kt
Last active August 6, 2019 15:52
lifecycle callback managers
// lifecycle observers manager
class LifecycleController {
private val observers = mutableListOf<LifecycleObserver>()
private var state: LifecycleState = Unknown
fun onInit() {
observers.forEach { it.onInit() }
state = Init
}
@SeongUgJung
SeongUgJung / potential_dispose.kt
Last active August 6, 2019 16:40
lifecycle dispose
Observable.interval(100) // every 100 ms
.flatMap { api.data() } // network request
.compose(until(PAUSE)) // dispose when onPause
.subscribe()
@Model
data class TitleState(var title: String)
@Compose
fun Text() {
val title = TitleState("hello")
+state { title }
Text(text=title.text)
updateAfter10Seconds { title.title = "hello2" }
}
interface RxBinder {
fun bindUntil(event: LifecycleEvent)
fun apply(event: LifecycleEvent)
}
class RxBinderImpl {
private val subject = BehaviorSubject.<LifecycleEvent>create()
private val disposables = mapOf<LifecycleEvent, MutableList<() -> Disposable>>()
override fun bindUntil(event: LifecycleEvent, disposable: () -> Disposable) {
@SeongUgJung
SeongUgJung / 01_BaseActivity.kt
Last active September 16, 2018 13:03
raw lifecycle callback
class BaseLifecycleActivity : Activity {
private val lifecycleOwner : LifecycleOwner = LifecycleOwner()
fun onCreate() {
super.onCreate()
notifyEvent(LifecycleEvent.ON_CREATED)
}
fun onStart() {
super.onStart()
class SecondAppStarterUsecase(private val context: Context) {
fun showSecond(index : Int) {
context.startActivity(Intent().apply { putExtra("INDEX",index) })
}
}