Skip to content

Instantly share code, notes, and snippets.

@markchristopherng
Created September 18, 2018 06:59
Show Gist options
  • Save markchristopherng/229d9e8b42c6f44e05bc3d5cb98aa673 to your computer and use it in GitHub Desktop.
Save markchristopherng/229d9e8b42c6f44e05bc3d5cb98aa673 to your computer and use it in GitHub Desktop.
abstract class BaseActivity : AppCompatActivity() {
fun <T> LiveData<T>.observe(observer: (T?) -> Unit) = observe(this@BaseActivity, Observer { observer(it) })
fun <T> LiveData<T>.observeNonNull(observer: (T) -> Unit) = observe { if (it != null) observer(it) }
fun <T> EventLiveData<T>.observeEvent(observer: (T) -> Unit) = observeEvent(this@BaseActivity, Observer { if (it != null) observer(it) })
inline fun <reified T : BaseViewModel> getViewModel(): T = ViewModelProviders.of(this)[T::class.java]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment