Fix index error on home page refresh with empty rows (#1318)

## Description
Fixes an bug caused when the home page has an empty row before the last
row and there are non-empty rows later, an `IndexOutOfBoundsException`
would be thrown.

Also removes the loading indicator if an unknown error occurs during a
refresh.

### Related issues
Fixes #1314

### Testing
Emulator & shield

## Screenshots
N/A

## AI or LLM usage
None
This commit is contained in:
Damontecres 2026-04-26 16:09:19 -04:00 committed by GitHub
parent edb37f5d69
commit 8cf99900e5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -151,21 +151,14 @@ class HomeViewModel
} }
Timber.v("Got row data index=%s", rowIndex) Timber.v("Got row data index=%s", rowIndex)
remaining.removeIf { it.index == rowIndex } remaining.removeIf { it.index == rowIndex }
// Include only errors & non-empty successes _state.update { state ->
if (rowData is HomeRowLoadingState.Error || val newRows =
(rowData is HomeRowLoadingState.Success && rowData.items.isNotEmpty()) state.homeRows.toMutableList().apply {
) { set(rowIndex, rowData)
_state.update { state -> }
val newRows = state.copy(
state.homeRows.toMutableList().apply { homeRows = newRows,
set(rowIndex, rowData) )
}
state.copy(
homeRows = newRows,
)
}
} else {
Timber.d("Skipping invalid row %s: %s", rowIndex, rowData)
} }
} }
_state.update { _state.update {
@ -175,14 +168,7 @@ class HomeViewModel
) )
} }
} else { } else {
val rows = val rows = deferred.awaitAll()
deferred
.awaitAll()
.filter {
// Include only errors & non-empty successes
it is HomeRowLoadingState.Error ||
(it is HomeRowLoadingState.Success && it.items.isNotEmpty())
}
Timber.v("Got all rows") Timber.v("Got all rows")
_state.update { _state.update {
it.copy( it.copy(
@ -198,6 +184,7 @@ class HomeViewModel
Timber.e(ex, "Exception during home page loading") Timber.e(ex, "Exception during home page loading")
if (state.value.loadingState == LoadingState.Success) { if (state.value.loadingState == LoadingState.Success) {
showToast(context, "Error refreshing home: ${ex.localizedMessage}") showToast(context, "Error refreshing home: ${ex.localizedMessage}")
_state.update { it.copy(refreshState = LoadingState.Error(ex)) }
} else { } else {
_state.update { _state.update {
it.copy(loadingState = LoadingState.Error(ex)) it.copy(loadingState = LoadingState.Error(ex))