Skip to content

Commit

Permalink
Merge pull request #260 from kyleve/kve/add-some-changes-to-on-change
Browse files Browse the repository at this point in the history
Add added and removes to the onContentChanged callback
  • Loading branch information
kyleve authored Jan 23, 2021
2 parents 86af8b2 + 6f4d7e1 commit ec4c7f9
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 109 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- [Introduce support for layout customization for `Item`, `HeaderFooter`, and `Section`](https://github.com/kyleve/Listable/pull/257) for all `ListLayout` types, not just `.table`.

- [Add `inserted` and `removed` items to `.onContentChanged`](https://github.com/kyleve/Listable/pull/260), to easily determine what content was added or removed from the list a central location.

### Removed

### Changed
Expand Down
1 change: 1 addition & 0 deletions Demo/Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand Down
65 changes: 57 additions & 8 deletions ListableUI/Sources/Internal/Diff/ArrayDiff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,34 @@ struct ArrayDiff<Element, Identifier:Hashable>

struct Added
{
let identifier : Identifier

let newIndex : Int

let new : Element
}

struct Removed
{
let identifier : Identifier

let oldIndex : Int

let old : Element
}

struct Moved
{
let identifier : Identifier

let old : Removed
let new : Added
}

struct Updated
{
let identifier : Identifier

let oldIndex : Int
let newIndex : Int

Expand All @@ -52,6 +60,8 @@ struct ArrayDiff<Element, Identifier:Hashable>

struct NoChange
{
let identifier : Identifier

let oldIndex : Int
let newIndex : Int

Expand Down Expand Up @@ -105,11 +115,19 @@ struct ArrayDiff<Element, Identifier:Hashable>
let removed = old.subtractDifference(from: new)

self.added = added.map {
Added(newIndex: $0.index, new: $0.value)
Added(
identifier: $0.identifier.base,
newIndex: $0.index,
new: $0.value
)
}

self.removed = removed.map {
Removed(oldIndex: $0.index, old: $0.value)
Removed(
identifier: $0.identifier.base,
oldIndex: $0.index,
old: $0.value
)
}

//
Expand Down Expand Up @@ -145,18 +163,30 @@ struct ArrayDiff<Element, Identifier:Hashable>
)

self.moved.append(Moved(
old: Removed(oldIndex: pair.old.index, old: pair.old.value),
new: Added(newIndex: pair.new.index, new: pair.new.value)
identifier: pair.new.identifier.base,

old: Removed(
identifier: pair.old.identifier.base,
oldIndex: pair.old.index,
old: pair.old.value
),
new: Added(
identifier: pair.new.identifier.base,
newIndex: pair.new.index,
new: pair.new.value
)
))
} else if pair.updated {
self.updated.append(Updated(
identifier: pair.new.identifier.base,
oldIndex: pair.old.index,
newIndex: pair.new.index,
old: pair.old.value,
new: pair.new.value
))
} else {
self.noChange.append(NoChange(
identifier: pair.new.identifier.base,
oldIndex: pair.old.index,
newIndex: pair.new.index,
old: pair.old.value,
Expand Down Expand Up @@ -199,14 +229,32 @@ struct ArrayDiff<Element, Identifier:Hashable>
let old = old[index]
let new = new[index]

guard identifierProvider(old) == identifierProvider(new) else {
let newID = identifierProvider(new)

guard identifierProvider(old) == newID else {
return nil
}

if updated(old, new) {
updates.append(Updated(oldIndex: index, newIndex: index, old: old, new: new))
updates.append(
Updated(
identifier: newID,
oldIndex: index,
newIndex: index,
old: old,
new: new
)
)
} else {
notChanged.append(NoChange(oldIndex: index, newIndex: index, old: old, new: new))
notChanged.append(
NoChange(
identifier: newID,
oldIndex: index,
newIndex: index,
old: old,
new: new
)
)
}
}

Expand Down Expand Up @@ -423,7 +471,8 @@ private class DiffContainer<Value, Identifier:Hashable>

private struct UniqueIdentifier<Type, Identifier:Hashable> : Hashable
{
private let base : Identifier
let base : Identifier

private let modifier : Int

private let hash : Int
Expand Down
Loading

0 comments on commit ec4c7f9

Please sign in to comment.