mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-17 22:37:03 +05:30
Merge pull request #32 from msfjarvis/first-load
Clear local cache of posts on first load
This commit is contained in:
commit
bab3a53a53
1 changed files with 11 additions and 2 deletions
|
@ -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())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue