Skip to content

Instantly share code, notes, and snippets.

@w9jds
Created May 17, 2018 13:25
Show Gist options
  • Save w9jds/af73e7508af50e46f805884b69e6dd3b to your computer and use it in GitHub Desktop.
Save w9jds/af73e7508af50e46f805884b69e6dd3b to your computer and use it in GitHub Desktop.
abstract class LoaderModel: ViewModel() {
private var loadingCount: AtomicInteger = AtomicInteger(0)
private val isLoading: MutableLiveData<Boolean> = MutableLiveData()
init {
isLoading.value = false
}
fun loading(): LiveData<Boolean> {
return isLoading
}
fun loadStarted() {
loadingCount.incrementAndGet()
if (isLoading.value != true) {
isLoading.value = true
}
}
fun loadFinished() {
loadingCount.decrementAndGet()
if (loadingCount.get() == 0 && isLoading.value != false) {
isLoading.value = false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment