diff --git a/Dwifft/AbstractDiffCalculator.swift b/Dwifft/AbstractDiffCalculator.swift index 9db51ca..365a860 100644 --- a/Dwifft/AbstractDiffCalculator.swift +++ b/Dwifft/AbstractDiffCalculator.swift @@ -10,11 +10,11 @@ import Foundation /// A parent class for all diff calculators. Don't use it directly. public class AbstractDiffCalculator { - + internal init(initialSectionedValues: SectionedValues) { self._sectionedValues = initialSectionedValues } - + /// The number of sections in the diff calculator. Return this inside /// `numberOfSections(in: tableView)` or `numberOfSections(in: collectionView)`. /// Don't implement that method any other way (see the docs for `numberOfObjects(inSection:)` @@ -22,7 +22,7 @@ public class AbstractDiffCalculator { public final func numberOfSections() -> Int { return self.sectionedValues.sections.count } - + /// The section at a given index. If you implement `tableView:titleForHeaderInSection` or /// `collectionView:viewForSupplementaryElementOfKind:atIndexPath`, you can use this /// method to get information about that section out of Dwifft. @@ -32,8 +32,7 @@ public class AbstractDiffCalculator { public final func value(forSection: Int) -> Section { return self.sectionedValues[forSection].0 } - - + /// The, uh, number of objects in a given section. Use this to implement /// `UITableViewDataSource.numberOfRowsInSection:` or `UICollectionViewDataSource.numberOfItemsInSection:`. /// Seriously, don't implement that method any other way - there is some subtle timing stuff @@ -46,8 +45,7 @@ public class AbstractDiffCalculator { public final func numberOfObjects(inSection section: Int) -> Int { return self.sectionedValues[section].1.count } - - + /// The value at a given index path. Use this to implement /// `UITableViewDataSource.cellForRowAtIndexPath` or `UICollectionViewDataSource.cellForItemAtIndexPath`. /// @@ -62,8 +60,7 @@ public class AbstractDiffCalculator { #endif return self.sectionedValues[indexPath.section].1[row] } - - + /// Set this variable to automatically trigger the correct section/row/item insertion/deletions /// on your table/collection view. public final var sectionedValues: SectionedValues { @@ -79,12 +76,12 @@ public class AbstractDiffCalculator { } } } - + internal static func buildSectionedValues(values: [Value], sectionIndex: Int) -> SectionedValues { let firstRows = (0.. internal func processChanges(newState: SectionedValues, diff: [SectionedDiffStep]){ diff --git a/Dwifft/Dwifft+AppKit.swift b/Dwifft/Dwifft+AppKit.swift index a3831a1..5f7e2e0 100644 --- a/Dwifft/Dwifft+AppKit.swift +++ b/Dwifft/Dwifft+AppKit.swift @@ -9,7 +9,7 @@ #if os(OSX) import Cocoa - + /// This class manages a `NSTableView`'s rows. It will make the necessary /// calls to the table view to ensure that its UI is kept in sync with the contents of the `rows` property. public final class TableViewDiffCalculator: AbstractDiffCalculator { @@ -23,7 +23,7 @@ public final class TableViewDiffCalculator: AbstractDiffCalcul /// You can change insertion/deletion animations like this! Fade works well. /// So does Top/Bottom. Left/Right/Middle are a little weird, but hey, do your thing. public var insertionAnimation = NSTableView.AnimationOptions.slideUp - + public var deletionAnimation = NSTableView.AnimationOptions.slideUp /// Set this variable to automatically trigger the correct row insertion/deletions @@ -48,7 +48,7 @@ public final class TableViewDiffCalculator: AbstractDiffCalcul self.sectionIndex = sectionIndex super.init(initialSectionedValues: AbstractDiffCalculator.buildSectionedValues(values: initialRows, sectionIndex: sectionIndex)) } - + override internal func processChanges(newState: SectionedValues, diff: [SectionedDiffStep]) { guard let tableView = self.tableView else { return } tableView.beginUpdates() @@ -102,18 +102,17 @@ public final class SingleSectionCollectionViewDiffCalculator { } private let internalDiffCalculator: CollectionViewDiffCalculator - + } - /// This class manages a `NSCollectionView`'s items and sections. It will make the necessary /// calls to the collection view to ensure that its UI is kept in sync with the contents /// of the `sectionedValues` property. public final class CollectionViewDiffCalculator : AbstractDiffCalculator { - + /// The collection view to be managed. public weak var collectionView: NSCollectionView? - + /// Initializes a new diff calculator. /// /// - Parameters: @@ -123,7 +122,7 @@ public final class CollectionViewDiffCalculator, diff: [SectionedDiffStep]) { guard let collectionView = self.collectionView else { return } collectionView.animator().performBatchUpdates({ diff --git a/Dwifft/Dwifft+UIKit.swift b/Dwifft/Dwifft+UIKit.swift index 752c528..10185db 100644 --- a/Dwifft/Dwifft+UIKit.swift +++ b/Dwifft/Dwifft+UIKit.swift @@ -111,7 +111,7 @@ public final class SingleSectionTableViewDiffCalculator { self.internalDiffCalculator.insertionAnimation = self.insertionAnimation } } - + public var deletionAnimation = UITableViewRowAnimation.automatic { didSet { self.internalDiffCalculator.deletionAnimation = self.deletionAnimation @@ -182,7 +182,7 @@ public final class SingleSectionCollectionViewDiffCalculator { } private let internalDiffCalculator: CollectionViewDiffCalculator - + } #endif diff --git a/Dwifft/Dwifft.swift b/Dwifft/Dwifft.swift index 188d037..f4d2fe1 100644 --- a/Dwifft/Dwifft.swift +++ b/Dwifft/Dwifft.swift @@ -15,7 +15,7 @@ public enum DiffStep : CustomDebugStringConvertible { case delete(Int, Value) public var debugDescription: String { - switch(self) { + switch self { case let .insert(i, j): return "+\(j)@\(i)" case let .delete(i, j): @@ -25,7 +25,7 @@ public enum DiffStep : CustomDebugStringConvertible { /// The index to be inserted or deleted. public var idx: Int { - switch(self) { + switch self { case let .insert(i, _): return i case let .delete(i, _): @@ -35,7 +35,7 @@ public enum DiffStep : CustomDebugStringConvertible { /// The value to be inserted or deleted. public var value: Value { - switch(self) { + switch self { case let .insert(j): return j.1 case let .delete(j): diff --git a/Dwifft/SectionedValues.swift b/Dwifft/SectionedValues.swift index d87eac0..a5b0c53 100644 --- a/Dwifft/SectionedValues.swift +++ b/Dwifft/SectionedValues.swift @@ -91,5 +91,4 @@ public extension SectionedValues where Section: Hashable { return (section, sortedValues) }) } - } diff --git a/DwifftExample/DwifftExample-iOS/StuffCollectionViewController.swift b/DwifftExample/DwifftExample-iOS/StuffCollectionViewController.swift index 14ef669..06848ed 100644 --- a/DwifftExample/DwifftExample-iOS/StuffCollectionViewController.swift +++ b/DwifftExample/DwifftExample-iOS/StuffCollectionViewController.swift @@ -20,7 +20,7 @@ final class StuffCollectionViewCell: UICollectionViewCell { label.textAlignment = .center self.addSubview(label) } - + required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } @@ -40,7 +40,7 @@ final class StuffSectionHeaderView: UICollectionReusableView { label.font = UIFont.italicSystemFont(ofSize: 14) self.addSubview(label) } - + required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } @@ -55,7 +55,7 @@ final class StuffCollectionViewController: UICollectionViewController { required init!(coder aDecoder: NSCoder) { super.init(coder: aDecoder) - self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Shuffle", style: .plain, target: self, action: #selector(StuffCollectionViewController.shuffle)) + self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Shuffle", style: .plain, target: self, action: #selector(shuffle)) } @objc func shuffle() { diff --git a/DwifftExample/DwifftExample-iOS/StuffTableViewController.swift b/DwifftExample/DwifftExample-iOS/StuffTableViewController.swift index d3b4aab..3aad70d 100644 --- a/DwifftExample/DwifftExample-iOS/StuffTableViewController.swift +++ b/DwifftExample/DwifftExample-iOS/StuffTableViewController.swift @@ -13,27 +13,27 @@ final class StuffTableViewController: UITableViewController { required init!(coder aDecoder: NSCoder) { super.init(coder: aDecoder) - self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Shuffle", style: .plain, target: self, action: #selector(StuffTableViewController.shuffle)) + self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Shuffle", style: .plain, target: self, action: #selector(shuffle)) } - + @objc func shuffle() { self.stuff = Stuff.wordStuff() } - + var diffCalculator: TableViewDiffCalculator? - + var stuff: SectionedValues = Stuff.wordStuff() { // So, whenever your datasource's array of things changes, just let the diffCalculator know and it'll do the rest. didSet { self.diffCalculator?.sectionedValues = stuff } } - + override func viewDidLoad() { super.viewDidLoad() self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier") self.diffCalculator = TableViewDiffCalculator(tableView: self.tableView, initialSectionedValues: self.stuff) - + // You can change insertion/deletion animations like this! Automatic works for most situations. Fade works well too. So does Top/Bottom. Left/Right/Middle are a little weird, but hey, do your thing. self.diffCalculator?.insertionAnimation = .fade self.diffCalculator?.deletionAnimation = .fade @@ -46,7 +46,7 @@ final class StuffTableViewController: UITableViewController { override func numberOfSections(in tableView: UITableView) -> Int { return self.diffCalculator?.numberOfSections() ?? 0 } - + /// IMPORTANT: you *must* implement `numberOfSections` this way (meaning, using this function on your diff calculator) in your app, to avoid a lot of gotchas around UITableView's internal assertions. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.diffCalculator?.numberOfObjects(inSection: section) ?? 0 diff --git a/DwifftTests/DwifftTests-macOS.swift b/DwifftTests/DwifftTests-macOS.swift index 1709c55..517bb65 100644 --- a/DwifftTests/DwifftTests-macOS.swift +++ b/DwifftTests/DwifftTests-macOS.swift @@ -332,7 +332,7 @@ class DwifftTests: XCTestCase { self.view = NSView() } } - + class TestCollectionView: NSCollectionView { let insertionExpectations: [Int: XCTestExpectation] diff --git a/DwifftTests/DwifftTests.swift b/DwifftTests/DwifftTests.swift index c758332..1ab641c 100644 --- a/DwifftTests/DwifftTests.swift +++ b/DwifftTests/DwifftTests.swift @@ -432,11 +432,11 @@ class DwifftTests: XCTestCase { let x: XCTestExpectation = expectation(description: "+\(i)") deletionExpectations[i] = x } - + let collectionView = TestCollectionView(insertionExpectations: insertionExpectations, deletionExpectations: deletionExpectations) let viewController = TestViewController(collectionView: collectionView, rows: [0, 1, 2, 5, 8, 9, 0]) viewController.rows = [4, 5, 9, 8, 3, 1, 0] waitForExpectations(timeout: 1.0, handler: nil) } - + }