Skip to content

Instantly share code, notes, and snippets.

@vamjakuldip
Created January 24, 2020 04:17
Show Gist options
  • Save vamjakuldip/eecbb8cf44d3b0e6d243e921de9a6ed9 to your computer and use it in GitHub Desktop.
Save vamjakuldip/eecbb8cf44d3b0e6d243e921de9a6ed9 to your computer and use it in GitHub Desktop.
BaseViewModel for DataBinding
package com.vk.android.base
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.vk.android.network.ApiService
import com.vk.android.utils.EventMsg
import io.reactivex.disposables.CompositeDisposable
import java.lang.ref.WeakReference
open class BaseViewModel<T>(application: Application) : AndroidViewModel(application) {
protected val apiClient by lazy { ApiService.create() }
var isLoading = MutableLiveData<Boolean>()
protected var navigator: WeakReference<T>? = null
protected val compositeDisposable = CompositeDisposable()
private val _snackbarText = MutableLiveData<EventMsg<String>>()
val snackbarMessage: LiveData<EventMsg<String>> = _snackbarText
open fun getNavigator(): T? {
return navigator?.get()
}
open fun setNavigator(navigator: T) {
this.navigator = WeakReference(navigator)
}
fun showSnackBarMessage(message: String) {
_snackbarText.value = EventMsg(message)
}
override fun onCleared() {
compositeDisposable.dispose()
super.onCleared()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment