Created
May 17, 2018 13:25
-
-
Save w9jds/af73e7508af50e46f805884b69e6dd3b to your computer and use it in GitHub Desktop.
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
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