Last active
September 16, 2018 13:03
-
-
Save SeongUgJung/25d4f794d9f3c670fc70d3e21e9ce059 to your computer and use it in GitHub Desktop.
raw lifecycle callback
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 BaseLifecycleActivity : Activity { | |
private val lifecycleOwner : LifecycleOwner = LifecycleOwner() | |
fun onCreate() { | |
super.onCreate() | |
notifyEvent(LifecycleEvent.ON_CREATED) | |
} | |
fun onStart() { | |
super.onStart() | |
notifyEvent(LifecycleEvent.ON_STARTED) | |
} | |
fun onResume() { | |
super.onResume() | |
notifyEvent(LifecycleEvent.ON_RESUMED) | |
} | |
fun onPause() { | |
notifyEvent(LifecycleEvent.ON_PAUSED) | |
super.onPause() | |
} | |
fun onStop() { | |
notifyEvent(LifecycleEvent.ON_STOPPED) | |
super.onStop() | |
} | |
fun onDestroy() { | |
notifyEvent(LifecycleEvent.ON_DESTROYED) | |
super.onDestroy() | |
} | |
fun register(callback : LifecycleCallback) { | |
lifecycleOwner.register(callback) | |
} | |
fun unregister(callback : LifecycleCallback) { | |
lifecycleOwner.unregister(callback) | |
} | |
private notifyEvent(event : LifecycleEvent) { | |
lifecycleOwner.notifyEvent(event) | |
} | |
} | |
class LifecycleOwner { | |
private val callbacks = mutualListOf<LifecycleCallback>() | |
private var lastEvent : LifecycleEvent = ON_CREATED | |
fun register(callback : LifecycleCallback) { | |
callback.apply(lastEvent) | |
callbacks.add(callback) | |
} | |
fun unregister(callback : LifecycleCallback) { | |
callbacks.remove(callback) | |
} | |
fun notifyEvent(event : LifecycleEvent) { | |
callbacks.forEach { it.apply(event) } | |
} | |
} | |
interface LifecycleCallback { | |
fun apply(event: LifecycleEvent) | |
} |
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 : Activity { | |
private lateinit var viewModel: MainViewModel | |
fun onCreate() { | |
super.onCreate() | |
viewModel = MainViewModel() | |
register(viewModel) | |
} | |
} | |
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 repository by lazy { TemporaryMsgRepository() } | |
private val msg = ObservableField<String>() | |
override apply(event: LifecycleEvent) { | |
switch (event) { | |
ON_CREATED -> onInitialize() | |
ON_DESTROYED -> onDeinitialize() | |
} | |
} | |
private fun onInitialize() { | |
val temporaryMsg = repository.getLastMessage() | |
msg.set(temporaryMsg) | |
} | |
private fun onDeinitialize() { | |
val temporaryMsg = msg.get() | |
repository.saveLastMessage(temporaryMsg) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment