2019-12-19 17:32:49 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
|
*
|
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-12-19 17:13:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import Cocoa
|
2019-11-13 13:27:36 +00:00
|
|
|
import IGListDiffKit
|
2016-12-19 17:13:21 +00:00
|
|
|
|
|
|
|
|
final class UsersViewController: NSViewController {
|
|
|
|
|
|
2017-09-01 20:49:12 +00:00
|
|
|
@IBOutlet weak var collectionView: NSCollectionView!
|
2017-05-16 14:30:08 +00:00
|
|
|
|
|
|
|
|
// MARK: Data
|
|
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
var users = [User]() {
|
|
|
|
|
didSet {
|
|
|
|
|
computeFilteredUsers()
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
var searchTerm = "" {
|
|
|
|
|
didSet {
|
|
|
|
|
computeFilteredUsers()
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
private func computeFilteredUsers() {
|
2017-12-19 17:23:37 +00:00
|
|
|
guard !searchTerm.isEmpty else {
|
2016-12-19 17:13:21 +00:00
|
|
|
filteredUsers = users
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
filteredUsers = users.filter({ $0.name.localizedCaseInsensitiveContains(self.searchTerm) })
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
fileprivate func delete(user: User) {
|
2019-12-19 05:50:02 +00:00
|
|
|
guard let index = self.users.firstIndex(where: { $0.pk == user.pk }) else { return }
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
self.users.remove(at: index)
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
|
|
|
|
// MARK: -
|
2019-12-19 17:32:49 +00:00
|
|
|
// MARK: Diffing
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2017-09-01 20:49:12 +00:00
|
|
|
var isFirstRun = true
|
2016-12-19 17:13:21 +00:00
|
|
|
var filteredUsers = [User]() {
|
|
|
|
|
didSet {
|
2017-09-01 20:49:12 +00:00
|
|
|
// A crash occurs when you try to use performBatchUpdates the first time
|
|
|
|
|
guard !isFirstRun else {
|
|
|
|
|
collectionView.reloadData()
|
|
|
|
|
isFirstRun = false
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-12-19 17:23:37 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
// get the difference between the old array of Users and the new array of Users
|
2017-09-01 20:49:12 +00:00
|
|
|
let diff = ListDiffPaths(fromSection: 0, toSection: 0, oldArray: oldValue, newArray: filteredUsers, option: .equality)
|
|
|
|
|
let batchUpdates = diff.forBatchUpdates()
|
|
|
|
|
let inserts = Set(batchUpdates.inserts)
|
|
|
|
|
let deletes = Set(batchUpdates.deletes)
|
|
|
|
|
let updates = Set(batchUpdates.updates)
|
|
|
|
|
let moves = Set(batchUpdates.moves)
|
2017-12-19 17:23:37 +00:00
|
|
|
|
2017-09-01 20:49:12 +00:00
|
|
|
// this difference is used here to update the collection view, but it can be used
|
2016-12-19 17:13:21 +00:00
|
|
|
// to update collection views and other similar interface elements
|
2017-09-01 20:49:12 +00:00
|
|
|
// this code can also be added to an extension of NSCollectionView ;)
|
2017-12-19 17:23:37 +00:00
|
|
|
|
2017-09-01 20:49:12 +00:00
|
|
|
// Set the animation duration when updating the collection view
|
2017-09-22 13:34:52 +00:00
|
|
|
NSAnimationContext.current.duration = 0.25
|
2017-12-19 17:23:37 +00:00
|
|
|
|
2017-09-01 20:49:12 +00:00
|
|
|
// Perform the updates to the collection view
|
|
|
|
|
collectionView.animator().performBatchUpdates({
|
|
|
|
|
collectionView.deleteItems(at: deletes)
|
|
|
|
|
collectionView.insertItems(at: inserts)
|
|
|
|
|
collectionView.reloadItems(at: updates)
|
|
|
|
|
moves.forEach { move in
|
|
|
|
|
collectionView.moveItem(at: move.from, to: move.to)
|
|
|
|
|
}
|
|
|
|
|
}, completionHandler: nil)
|
2016-12-19 17:13:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
private func loadSampleUsers() {
|
|
|
|
|
guard let file = Bundle.main.url(forResource: "users", withExtension: "json") else { return }
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
do {
|
|
|
|
|
self.users = try UsersProvider(with: file).users
|
|
|
|
|
} catch {
|
|
|
|
|
NSAlert(error: error).runModal()
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
// MARK: Interface
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2019-12-19 17:32:49 +00:00
|
|
|
// The view needs to be backed by a CALayer to be able to enable the collections view animations you can
|
2017-09-01 20:49:12 +00:00
|
|
|
// enable this by selecting the view controller's view in the Interface Builder in the Core Animation section
|
|
|
|
|
// of the View Effects inspector tab, through code you can do by view.wantsLayer = true
|
2016-12-19 17:13:21 +00:00
|
|
|
loadSampleUsers()
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
override func viewDidAppear() {
|
|
|
|
|
super.viewDidAppear()
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
view.window?.titleVisibility = .hidden
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
@IBAction func shuffle(_ sender: Any?) {
|
|
|
|
|
users = users.shuffled
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
@IBAction func search(_ sender: NSSearchField) {
|
|
|
|
|
searchTerm = sender.stringValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 20:49:12 +00:00
|
|
|
extension UsersViewController: UserCollectionViewCellDelegate {
|
2017-12-19 17:23:37 +00:00
|
|
|
|
2017-09-01 20:49:12 +00:00
|
|
|
func itemDeleted(_ user: User) {
|
|
|
|
|
self.delete(user: user)
|
2016-12-19 17:13:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 20:49:12 +00:00
|
|
|
extension UsersViewController: NSCollectionViewDelegate {
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2017-09-01 20:49:12 +00:00
|
|
|
extension UsersViewController: NSCollectionViewDataSource {
|
2017-12-19 17:23:37 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
private struct Storyboard {
|
2017-09-01 20:49:12 +00:00
|
|
|
static let cellIdentifier = "UserCollectionViewCell"
|
2016-12-19 17:13:21 +00:00
|
|
|
}
|
2017-12-19 17:23:37 +00:00
|
|
|
|
2017-09-01 20:49:12 +00:00
|
|
|
func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
|
|
|
return self.filteredUsers.count
|
2016-12-19 17:13:21 +00:00
|
|
|
}
|
2017-12-19 17:23:37 +00:00
|
|
|
|
2016-12-29 18:56:56 +00:00
|
|
|
@available(OSX 10.11, *)
|
2017-09-01 20:49:12 +00:00
|
|
|
func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
|
2017-09-22 13:34:52 +00:00
|
|
|
let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: Storyboard.cellIdentifier), for: indexPath)
|
2017-09-01 20:49:12 +00:00
|
|
|
guard let cell = item as? UserCollectionViewCell else { return item }
|
2017-12-19 17:23:37 +00:00
|
|
|
|
2017-09-01 20:49:12 +00:00
|
|
|
cell.delegate = self
|
|
|
|
|
cell.bindViewModel(filteredUsers[indexPath.item])
|
|
|
|
|
return cell
|
2016-12-19 17:13:21 +00:00
|
|
|
}
|
2017-09-01 20:49:12 +00:00
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2017-09-01 20:49:12 +00:00
|
|
|
extension UsersViewController: NSCollectionViewDelegateFlowLayout {
|
2017-12-19 17:23:37 +00:00
|
|
|
|
2017-09-01 20:49:12 +00:00
|
|
|
func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
|
2017-12-19 17:23:37 +00:00
|
|
|
|
2017-09-01 20:49:12 +00:00
|
|
|
let availableWidth = collectionView.bounds.width
|
|
|
|
|
return CGSize(width: availableWidth, height: 44)
|
|
|
|
|
}
|
2016-12-19 17:13:21 +00:00
|
|
|
}
|