app: tweak first load behavior

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-10-10 17:58:14 +05:30
parent bd5647f4c7
commit b7dc92d85a
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80

View file

@ -24,16 +24,19 @@ class LobstersViewModel @ViewModelInject constructor(
private val coroutineExceptionHandler = CoroutineExceptionHandler { _, throwable -> private val coroutineExceptionHandler = CoroutineExceptionHandler { _, throwable ->
when (throwable) { when (throwable) {
// Swallow known network errors that can be recovered from. // Swallow known network errors that can be recovered from.
is UnknownHostException, is SocketTimeoutException -> {} is UnknownHostException, is SocketTimeoutException -> {
if (_posts.value.isEmpty()) {
viewModelScope.launch {
dao.loadPosts().collectLatest { _posts.value = it }
}
}
}
else -> throw throwable else -> throw throwable
} }
} }
val posts: StateFlow<List<LobstersPost>> get() = _posts val posts: StateFlow<List<LobstersPost>> get() = _posts
init { init {
viewModelScope.launch {
dao.loadPosts().collectLatest { _posts.value = it }
}
getMorePostsInternal(true) getMorePostsInternal(true)
} }