2016-12-19 17:13:21 +00:00
|
|
|
/**
|
|
|
|
|
Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
|
|
|
|
|
|
|
|
|
|
The examples provided by Facebook are for non-commercial testing and evaluation
|
|
|
|
|
purposes only. Facebook reserves all rights not expressly granted.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
|
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
import IGListKit
|
|
|
|
|
|
|
|
|
|
final class UsersViewController: NSViewController {
|
|
|
|
|
|
|
|
|
|
@IBOutlet weak var tableView: NSTableView!
|
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() {
|
2016-12-29 22:33:29 +00:00
|
|
|
guard !searchTerm.characters.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) {
|
|
|
|
|
guard let index = self.users.index(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: -
|
|
|
|
|
// MARK: Diffing
|
|
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
var filteredUsers = [User]() {
|
|
|
|
|
didSet {
|
|
|
|
|
// get the difference between the old array of Users and the new array of Users
|
Swift name annotations
Summary:
This adds `NS_SWIFT_NAME` annotations to all public API's to provide cleaner integration into Swift:
- Removes the need to prefix classes in Swift code, instead rely on Swift module name spacing
- Adds more argument labels to C function API's like `IGListDiff([], [], .equality)` => `ListDiff(oldArray: [], newArray: [], option: .equality)`
While this is a large API change it should be as easy as:
- Find and replace `(IGList)([^K])` to `List$2` in Xcode with a scope set to Swift
- Build and follow compiler's auto fix corrections for C API's or any missed renames
I have not updated the documentation to reflect this yet, I am totally willing to do so but before I sink that amount of time into it I wanted to see if the Instagram team is even open to this change!
- [x] All tests pass. Demo project builds and runs.
- [x] I added tests, an experiment, or detailed why my change isn't tested.
- [x] I added an entry to the `CHANGELOG.md` for any breaking changes, enhancements, or bug fixes.
- [x] I have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/master/.github/CONTRIBUTING.md)
- [ ] I have updated the documentation
Closes https://github.com/Instagram/IGListKit/pull/593
Reviewed By: jessesquires
Differential Revision: D5028039
Pulled By: rnystrom
fbshipit-source-id: b473d874a1f9574e56b2ebaabd5b73d1b57d4bab
2017-05-09 21:29:52 +00:00
|
|
|
let diff = ListDiff(oldArray: oldValue, newArray: filteredUsers, option: .equality)
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
// this difference is used here to update the table view, but it can be used
|
|
|
|
|
// to update collection views and other similar interface elements
|
|
|
|
|
// this code can also be added to an extension of NSTableView ;)
|
|
|
|
|
tableView.beginUpdates()
|
|
|
|
|
tableView.insertRows(at: diff.inserts, withAnimation: .slideDown)
|
|
|
|
|
tableView.removeRows(at: diff.deletes, withAnimation: .slideUp)
|
|
|
|
|
tableView.reloadData(forRowIndexes: diff.updates, columnIndexes: .zero)
|
|
|
|
|
diff.moves.forEach { move in
|
|
|
|
|
self.tableView.moveRow(at: move.from, to: move.to)
|
|
|
|
|
}
|
|
|
|
|
tableView.endUpdates()
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
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-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
@IBAction func delete(_ sender: Any?) {
|
2017-05-16 14:30:08 +00:00
|
|
|
guard !tableView.selectedRowIndexes.isEmpty else { return }
|
|
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
tableView.selectedRowIndexes.forEach({ self.delete(user: self.filteredUsers[$0]) })
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension UsersViewController: NSTableViewDataSource {
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
func numberOfRows(in tableView: NSTableView) -> Int {
|
|
|
|
|
return filteredUsers.count
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension UsersViewController: NSTableViewDelegate {
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
private struct Storyboard {
|
|
|
|
|
static let cellIdentifier = "cell"
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
|
|
|
|
|
guard let cell = tableView.make(withIdentifier: Storyboard.cellIdentifier, owner: tableView) as? NSTableCellView else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
cell.textField?.stringValue = filteredUsers[row].name
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
return cell
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-29 18:56:56 +00:00
|
|
|
@available(OSX 10.11, *)
|
2016-12-19 17:13:21 +00:00
|
|
|
func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableRowActionEdge) -> [NSTableViewRowAction] {
|
2017-05-16 14:30:08 +00:00
|
|
|
let delete = NSTableViewRowAction(style: .destructive, title: "Delete") { _, row in
|
2016-12-19 17:13:21 +00:00
|
|
|
guard row < self.filteredUsers.count else { return }
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
self.delete(user: self.filteredUsers[row])
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
return [delete]
|
|
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
}
|