Guest User

Untitled

a guest
Mar 6th, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.77 KB | None | 0 0
  1. // interface for view/activity/fragment/whatever, can be delegated also.
  2.  
  3. interface LoginAcceptable {
  4.     fun onChangeLocation()
  5.     fun onLoginSuccess()
  6.     fun onAppUpdateAvailable()
  7.     fun renderLocationConfig(configuration: LocationConfiguration)
  8. }
  9.  
  10.  
  11. // alias for short name in single livedata
  12. typealias LoginEvent = LoginAcceptable.() -> Unit
  13.  
  14.  
  15.  
  16. // single livedata in view model
  17. val ui = MutableLiveData<LoginEvent>()
  18.  
  19.  
  20.  
  21. // just syntax sugar
  22. operator fun <T> MutableLiveData<T>.invoke(function: T) {
  23.     postValue(function)
  24. }
  25.  
  26.  
  27.  
  28. // viewmodel function
  29. fun onLogin() {
  30.  
  31.     // call to this single livedata
  32.     ui {
  33.         onLoginSuccess()
  34.         onAppUpdateAvailable()
  35.     }
  36. }
  37.  
  38.  
  39. // observe in your view
  40. viewModel.ui.observe { it(this) }
Advertisement
Add Comment
Please, Sign In to add comment