Skip to content

Instantly share code, notes, and snippets.

@seoft
Last active November 13, 2021 09:46
Show Gist options
  • Save seoft/13767eb5481c20403d908aa7a500381a to your computer and use it in GitHub Desktop.
Save seoft/13767eb5481c20403d908aa7a500381a to your computer and use it in GitHub Desktop.
class CommonViewModel(private val api: MockApi = MockService()) : ViewModel() // 생성자 구조
private val _uiModels = MutableLiveData<List<NormalUiModel>>(emptyList())
val uiModels: LiveData<List<NormalUiModel>> = _uiModels
val uiModelsValue get() = uiModels.value ?: emptyList()
fun loadMoreUiModels() {
if (requestLock.getAndSet(true)) return
isShowProgress.value = true
api.loadMore()
.map { NormalModelMapper.from(it) }
.doFinally {
requestLock.set(false)
isShowProgress.postValue(false)
}
.subscribe({
_uiModels.postValue(uiModelsValue + it)
}, {
_throwable.postValue(it)
}).addTo(compositeDisposable)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment