-
Notifications
You must be signed in to change notification settings - Fork 0
/
checklistViewController.swift
138 lines (114 loc) · 5.1 KB
/
checklistViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// ViewController.swift
// Table List
//
// Created by Gabriel Wilk Mizrahi on 4/26/19.
// Copyright © 2019 Gabriel Wilk Mizrahi. All rights reserved.
//
import UIKit
class checkListViewController: UITableViewController {
var todoList: TodoList
@IBAction func addItem(_ sender: Any) {
let newRowIndex = todoList.todos.count
_ = todoList.newTodo()
let indexPath = IndexPath(row: newRowIndex, section: 0)
let indexPaths = [indexPath]
tableView.insertRows(at: indexPaths, with: .automatic)
print("added item")
}
required init?(coder aDecoder: NSCoder) {
todoList = TodoList()
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.leftBarButtonItem = editButtonItem
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: true)
tableView.setEditing(tableView.isEditing, animated: true)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return todoList.todos.count
}
func configureText(for cell: UITableViewCell, with item: checklistItem) {
if let checkmarkCell = cell as? ChecklistTableViewCell {
checkmarkCell.todoTextLabel.text = item.text
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ChecklistItem", for: indexPath)
let item = todoList.todos[indexPath.row]
configureText(for: cell, with: item)
configureCheckmark(for: cell, with:item
)
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
let item = todoList.todos[indexPath.row]
item.toggleChecked()
configureCheckmark(for: cell, with: item)
tableView.deselectRow(at: indexPath, animated: true)
}
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
todoList.todos.remove(at: indexPath.row)
//let indexPaths = [indexPath]
//tableView.deleteRows(at: indexPaths, with: .automatic)
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
todoList.move(item: todoList.todos[sourceIndexPath.row], to: destinationIndexPath.row)
tableView.reloadData()
}
func configureCheckmark(for cell: UITableViewCell, with item: checklistItem){
guard let checkmarkCell = cell as? ChecklistTableViewCell else {
return
}
if item.checked {
checkmarkCell.checkmarkLabel.text = "√"
} else {
checkmarkCell.checkmarkLabel.text = ""
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "AddItemSegue" {
if let itemDetailViewController = segue.destination as? itemDetailViewController {
itemDetailViewController.delegate = self
itemDetailViewController.todoList = todoList
}
} else if segue.identifier == "EditItemSegue" {
if let itemDetailViewController = segue.destination as? itemDetailViewController {
if let cell = sender as? UITableViewCell,
let indexPath = tableView.indexPath(for: cell) {
let item = todoList.todos[indexPath.row]
itemDetailViewController.itemToEdit = item
itemDetailViewController.delegate = self
}
}
}
}
}
extension checkListViewController: ItemDetailViewControllerDelegate {
func itemDetailViewControllerDidCancel(_ controller: itemDetailViewController) {
navigationController?.popViewController(animated: true)
}
func itemDetailViewController(_ controller: itemDetailViewController, didFinishAdding item: checklistItem) {
navigationController?.popViewController(animated: true)
let rowIndex = todoList.todos.count - 1
let indexPath = IndexPath(row: rowIndex, section: 0)
let indexPaths = [indexPath]
tableView.insertRows(at: indexPaths, with: .automatic)
}
func itemDetailViewController(_ controller: itemDetailViewController, disFinishEditing item: checklistItem) {
if let index = todoList.todos.index(of: item) {
let indexPath = IndexPath(row: index, section: 0)
if let cell = tableView.cellForRow(at: indexPath) {
configureText(for: cell, with: item)
}
}
navigationController?.popViewController(animated: true)
}
}