Skip to content

Commit

Permalink
Home - Add fail safe when forcibly removing elements from arrays in `…
Browse files Browse the repository at this point in the history
…buildSnaphot` (#1056)

fix(home): update HomeViewModel, add some fail safe in buildSnapshot() before force removing elements from arrays
  • Loading branch information
Gio2018 authored Aug 5, 2024
1 parent 94610c0 commit dd405b3
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions PocketKit/Sources/PocketKit/Home/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ extension HomeViewModel {

for slateSection in slateSections {
guard var recommendations = slateSection.objects as? [Recommendation],
!recommendations.isEmpty,
let slateId = recommendations.first?.slate?.objectID
else {
continue
Expand All @@ -286,8 +287,10 @@ extension HomeViewModel {
[.recommendationHero(hero.objectID)],
toSection: .slateHero(slateId)
)

if numberOfHeroItems == 2 {
// Check if recommendations is empty. It shouldn't, but there
// is still a theoretic scenario where we removed an element
// as the first hero, and the list becomes empty.
if numberOfHeroItems == 2, !recommendations.isEmpty {
let hero2 = recommendations.removeFirst()
snapshot.appendItems(
[.recommendationHero(hero2.objectID)],
Expand Down

0 comments on commit dd405b3

Please sign in to comment.