app: add local cache busting

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-09-28 11:03:32 +05:30
parent 0ecf21467b
commit 837970147c
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80

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)
if (firstLoad) {
_posts.value = newPosts
dao.deleteAllPosts()
} else {
_posts.value += newPosts
}
apiPage += 1
dao.insertPosts(*_posts.value.toTypedArray())
}