Skip to content

Commit

Permalink
Merge pull request #14 from scribd/stephane/delta_safety_check
Browse files Browse the repository at this point in the history
DRY up safety check, move delegate call onto main thread.
  • Loading branch information
Stephane Magne authored Nov 7, 2023
2 parents d2fc890 + 0e58040 commit e647cda
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,7 @@ private extension ItemDataCalculator {
self?._performNextCalculation()
}

let isDeltaAccurate: Bool = (itemProvider.items.count + delta.insertions.count - delta.deletions.count) == updatedItems.count

if isDeltaAccurate == false {
calculationDelegate?.inaccurateDeltaDetected(delta)
}

guard delta.hasChanges, isDeltaAccurate else {
guard delta.hasChanges else {
updateData()
calculationCompletion()
return
Expand All @@ -228,8 +222,13 @@ private extension ItemDataCalculator {
targetView.reloadData()
}

let isDeltaInaccurate: Bool = (itemProvider.items.count + delta.insertions.count - delta.deletions.count) != updatedItems.count
if isDeltaInaccurate {
self.calculationDelegate?.inaccurateDeltaDetected(delta)
}

let itemAnimationStlye: AnimationStyle = {
if targetView.frame.isEmpty { return .reloadData }
if targetView.frame.isEmpty || isDeltaInaccurate { return .reloadData }
guard let animationDelegate = animationDelegate else { return .preciseAnimations }
return animationDelegate.preferredItemAnimationStyle(for: delta)
}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,8 @@ private extension SectionDataCalculator {

let currentItemCount: Int = sectionProvider.sections.reduce(0) { $0 + $1.items.count }
let updatedItemCount: Int = updatedSections.reduce(0) { $0 + $1.items.count }
let isDeltaAccurate: Bool = (currentItemCount + itemDelta.insertions.count - itemDelta.deletions.count) == updatedItemCount

if isDeltaAccurate == false {
self.calculationDelegate?.inaccurateDeltaDetected(itemDelta)
}

guard (sectionDelta.hasChanges || itemDelta.hasChanges), isDeltaAccurate else {
guard sectionDelta.hasChanges || itemDelta.hasChanges else {
sectionProvider.calculatingSections = nil
calculationCompletion()
return // don't need to update with no changes
Expand All @@ -310,8 +305,13 @@ private extension SectionDataCalculator {
return
}

let isDeltaInaccurate: Bool = (currentItemCount + itemDelta.insertions.count - itemDelta.deletions.count) != updatedItemCount
if isDeltaInaccurate {
self.calculationDelegate?.inaccurateDeltaDetected(itemDelta)
}

let itemAnimationStlye: AnimationStyle = {
if view.frame.isEmpty { return .reloadData }
if view.frame.isEmpty || isDeltaInaccurate { return .reloadData }
guard let animationDelegate = animationDelegate else { return .preciseAnimations }
return animationDelegate.preferredItemAnimationStyle(for: itemDelta)
}()
Expand Down

0 comments on commit e647cda

Please sign in to comment.