Merge pull request #32 from msfjarvis/first-load

Clear local cache of posts on first load
This commit is contained in:
Harsh Shandilya 2020-09-28 11:07:15 +05:30 committed by GitHub
commit bab3a53a53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -33,13 +33,22 @@ class LobstersViewModel @ViewModelInject constructor(
viewModelScope.launch {
dao.loadPosts().collectLatest { _posts.value = it }
}
getMorePosts()
getMorePostsInternal(true)
}
fun getMorePosts() {
getMorePostsInternal(false)
}
private fun getMorePostsInternal(firstLoad: Boolean) {
viewModelScope.launch(coroutineExceptionHandler) {
val newPosts = lobstersApi.getHottestPosts(apiPage)
_posts.value += newPosts
if (firstLoad) {
_posts.value = newPosts
dao.deleteAllPosts()
} else {
_posts.value += newPosts
}
apiPage += 1
dao.insertPosts(*_posts.value.toTypedArray())
}