Skip to content

Commit

Permalink
Fixing bug where add phrase cell was in empty state
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Morgan committed Apr 8, 2022
1 parent ded24a7 commit baaa037
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions Vocable/Features/Root/CategoryDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,7 @@ class CategoryDetailViewController: PagingCarouselViewController, NSFetchedResul
let pageCountBefore = collectionView.layout.pagesPerSection
let fetchedSnapshot = snapshot as NSDiffableDataSourceSnapshot<String, NSManagedObjectID>

var updatedSnapshot = Snapshot()

updatedSnapshot.appendSections(fetchedSnapshot.sectionIdentifiers)

for sectionId in fetchedSnapshot.sectionIdentifiers {
let categoryItems = fetchedSnapshot.itemIdentifiers(inSection: sectionId).map(CategoryItem.persistedPhrase)

updatedSnapshot.appendItems(categoryItems, toSection: sectionId)
}

updatedSnapshot.appendItems([.addNewPhrase])
let updatedSnapshot = transformFetchedSnapshot(fetchedSnapshot)

dataSourceProxy.apply(updatedSnapshot, animatingDifferences: false)

Expand Down Expand Up @@ -181,6 +171,24 @@ class CategoryDetailViewController: PagingCarouselViewController, NSFetchedResul

}

private func transformFetchedSnapshot(_ fetchedSnapshot: NSDiffableDataSourceSnapshot<String, NSManagedObjectID>) -> Snapshot {
var updatedSnapshot = Snapshot()

updatedSnapshot.appendSections(fetchedSnapshot.sectionIdentifiers)

for sectionId in fetchedSnapshot.sectionIdentifiers {
let categoryItems = fetchedSnapshot.itemIdentifiers(inSection: sectionId).map(CategoryItem.persistedPhrase)

updatedSnapshot.appendItems(categoryItems, toSection: sectionId)
}

if updatedSnapshot.numberOfItems != 0 {
updatedSnapshot.appendItems([.addNewPhrase])
}

return updatedSnapshot
}

private func installEmptyStateIfNeeded() {
guard collectionView.backgroundView == nil else { return }
paginationView.isHidden = true
Expand Down

0 comments on commit baaa037

Please sign in to comment.