Skip to content

Commit

Permalink
Improve item update (review comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Horta committed May 10, 2024
1 parent 8067c7b commit 4acb2b9
Showing 1 changed file with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,13 @@ class ReaderTagsFeedViewModel @Inject constructor(
val updatedLoadedData = getLoadedData(uiState)

// At this point, all tag feed items already exist in the UI.
// We need it's index to update it and keep it in place.
val existingIndex = updatedLoadedData.indexOfFirst { it.tagChip.tag == updatedItem.tagChip.tag }

// Remove the current row(s) of this tag, so we don't have duplicates.
updatedLoadedData.removeAll { it.tagChip.tag == updatedItem.tagChip.tag }

// Add the updated item in the correct position.
updatedLoadedData.add(existingIndex, updatedItem)
// We need it's index to update it and keep it in the same place.
updatedLoadedData.indexOfFirst { it.tagChip.tag == updatedItem.tagChip.tag }
.takeIf { it >= 0 }
?.let { existingIndex ->
// Update item
updatedLoadedData[existingIndex] = updatedItem
}

UiState.Loaded(updatedLoadedData)
}
Expand Down

0 comments on commit 4acb2b9

Please sign in to comment.