Last active
October 8, 2018 15:46
-
-
Save SeongUgJung/c4dbf0e1765298912ca56f79036a5886 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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) { | |
if (subject.value >= event) { | |
(disposables[event] ?: run { disposables[event] = mutableList(); disposables[event]; }) | |
.let { it += disposable } | |
} else { | |
disposable.dispose() | |
} | |
} | |
override fun apply(event: LifecycleEvent) { | |
disposables[event]?.foreach { it.dispose() } | |
} | |
} |
This file contains hidden or 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 RxActivity { | |
val rxBinder : RxBinder = RxBinderImpl() | |
fun onCreate() { | |
rxBinder.apply(LifecycleEvent.ON_CREATED) | |
} | |
fun onStart() { | |
rxBinder.apply(LifecycleEvent.ON_STARTED) | |
} | |
fun onResume() { | |
rxBinder.apply(LifecycleEvent.ON_RESUMED) | |
} | |
fun onPause() { | |
rxBinder.apply(LifecycleEvent.ON_PAUSED) | |
} | |
fun onStop() { | |
rxBinder.apply(LifecycleEvent.ON_STOPPED) | |
} | |
fun onDestroy() { | |
rxBinder.apply(LifecycleEvent.ON_DESTROYED) | |
} | |
} |
This file contains hidden or 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 MainActivity : RxActivity { | |
private lateinit var viewModel : MainViewModel | |
fun onCreate() { | |
viewModel = MainViewModel(rxBinder) | |
} | |
} |
This file contains hidden or 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 MainViewModel(private val rxBinder: RxBinder) { | |
private val repository by lazy { TemporaryMsgRepository() } | |
private val msg = ObservableField<String>() | |
init { | |
rxBinder.bindUntil(LifecycleEvent.ON_CREATED) { | |
Disposables.fromAction { msg.set(respository.getLastMessage()) } | |
} | |
rxBinder.bindUntil(LifecycleEvent.ON_DESTROYED) { | |
Disposables.fromAction { respository.saveLastMessage(msg) } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment