Created
December 26, 2018 19:47
-
-
Save darrenclark/7f8ef59097aa7ee7e6f79d301fac8ec0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import PlaygroundSupport | |
extension NSNotification.Name { | |
static let todoListItemUpdated = NSNotification.Name("TodoListItemUpdated") | |
} | |
class TodoList { | |
static let shared = TodoList() | |
var items: [TodoListItem] = [ | |
TodoListItem(title: "Wash dishes"), | |
TodoListItem(title: "Take out garbage"), | |
TodoListItem(title: "Go grocery shopping"), | |
TodoListItem(title: "Do laundry"), | |
TodoListItem(title: "Meal prep for next week") | |
] | |
func markAllAsCompleted() { | |
for item in items { | |
item.completed = true | |
} | |
} | |
} | |
class TodoListItem { | |
let title: String | |
var completed: Bool = false { | |
didSet { | |
NotificationCenter.default.post(name: .todoListItemUpdated, object: self) | |
} | |
} | |
init(title: String) { | |
self.title = title | |
} | |
} | |
class ViewController: UITableViewController { | |
let dispatchSource = DispatchSource.makeUserDataAddSource(queue: .main) | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
navigationItem.title = "To Do..." | |
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Complete All", style: .plain, target: self, action: #selector(completeAll)) | |
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") | |
dispatchSource.setEventHandler(handler: { [weak self] in | |
print("tableView.reloadData()") | |
self?.tableView.reloadData() | |
}) | |
dispatchSource.resume() | |
NotificationCenter.default.addObserver(self, selector: #selector(todoListItemUpdated), name: .todoListItemUpdated, object: nil) | |
} | |
@objc func completeAll() { | |
TodoList.shared.markAllAsCompleted() | |
} | |
@objc func todoListItemUpdated() { | |
dispatchSource.add(data: 1) | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return TodoList.shared.items.count | |
} | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) | |
let item = TodoList.shared.items[indexPath.row] | |
if item.completed { | |
cell.textLabel?.text = "✅ " + item.title | |
} | |
else { | |
cell.textLabel?.text = item.title | |
} | |
return cell | |
} | |
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
tableView.deselectRow(at: indexPath, animated: false) | |
TodoList.shared.items[indexPath.row].completed.toggle() | |
} | |
} | |
PlaygroundPage.current.liveView = UINavigationController(rootViewController: ViewController()) | |
PlaygroundPage.current.needsIndefiniteExecution = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment