added mechanism to track transaction sources

This commit is contained in:
John Estropia
2021-09-15 14:45:13 +09:00
parent 45215c7a18
commit 4ddfa95140
26 changed files with 1323 additions and 179 deletions

View File

@@ -102,6 +102,7 @@ extension Modern.ColorsDemo {
try transaction.deleteAll(From<Modern.ColorsDemo.Palette>())
},
sourceIdentifier: TransactionSource.clear,
completion: { _ in }
)
}
@@ -113,6 +114,7 @@ extension Modern.ColorsDemo {
_ = transaction.create(Into<Modern.ColorsDemo.Palette>())
},
sourceIdentifier: TransactionSource.add,
completion: { _ in }
)
}
@@ -127,6 +129,7 @@ extension Modern.ColorsDemo {
palette.setRandomHue()
}
},
sourceIdentifier: TransactionSource.shuffle,
completion: { _ in }
)
}

View File

@@ -69,5 +69,16 @@ extension Modern {
)
}
}
// MARK: - TransactionSource
enum TransactionSource {
case add
case delete
case shuffle
case clear
}
}
}

View File

@@ -91,7 +91,8 @@ extension Modern.ColorsDemo.UIKit {
func objectMonitor(
_ monitor: ObjectMonitor<Modern.ColorsDemo.Palette>,
didUpdateObject object: Modern.ColorsDemo.Palette,
changedPersistentKeys: Set<KeyPathString>
changedPersistentKeys: Set<KeyPathString>,
sourceIdentifier: Any?
) {
self.reloadPaletteInfo(object, changedKeys: changedPersistentKeys)

View File

@@ -32,17 +32,25 @@ extension Modern.ColorsDemo.UIKit {
)
/**
Sample 2: Once the views are created, we can start binding `ListPublisher` updates to the `DiffableDataSource`. We typically call this at the end of `viewDidLoad`. Note that the `addObserver`'s closure argument will only be called on the succeeding updates, so to immediately display the current values, we need to call `dataSource.apply()` once.
Sample 2: Once the views are created, we can start binding `ListPublisher` updates to the `DiffableDataSource`. We typically call this at the end of `viewDidLoad`. Note that the `addObserver`'s closure argument will only be called on the succeeding updates, so to immediately display the current values, we need to call `dataSource.apply()` once. This example inspects the optional `transactionSource` to determine the source of the update which is helpful for debugging or for fine-tuning animations.
*/
private func startObservingList() {
let dataSource = self.dataSource
self.listPublisher.addObserver(self) { (listPublisher) in
self.listPublisher.addObserver(self, notifyInitial: true) { (listPublisher, transactionSource) in
dataSource.apply(listPublisher.snapshot, animatingDifferences: true)
switch transactionSource as? Modern.ColorsDemo.TransactionSource {
case .add,
.delete,
.shuffle,
.clear:
dataSource.apply(listPublisher.snapshot, animatingDifferences: true)
case nil:
dataSource.apply(listPublisher.snapshot, animatingDifferences: false)
}
}
dataSource.apply(self.listPublisher.snapshot, animatingDifferences: false)
}
/**
@@ -74,6 +82,7 @@ extension Modern.ColorsDemo.UIKit {
transaction.delete(objectIDs: [itemID])
},
sourceIdentifier: Modern.ColorsDemo.TransactionSource.delete,
completion: { _ in }
)