-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathTodoViewController.swift
More file actions
297 lines (250 loc) · 10.9 KB
/
TodoViewController.swift
File metadata and controls
297 lines (250 loc) · 10.9 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//
// TodoViewController.swift
// To-Do
//
// Created by Aaryan Kothari on 29/09/20.
// Copyright © 2020 Aaryan Kothari. All rights reserved.
//
import UIKit
import CoreData
class TodoViewController: UITableViewController {
/// `Tableview` to display list of tasks
@IBOutlet weak var todoTableView: UITableView!
/// `SearchController` to include search bar
var searchController: UISearchController!
/// `ResultsController` to display results of specific search
var resultsTableController: ResultsTableController!
/// `DataSource` for todoTableview
var todoList : [Task] = []
var lastIndexTapped : Int = 0
/// Coredata managed object
var moc: NSManagedObjectContext!
/// Controller to fetch tasks from core-data
var fetchedResultsController: NSFetchedResultsController<Task>!
/// default fetch request for tasks
lazy var defaultFetchRequest: NSFetchRequest<Task> = {
let fetchRequest : NSFetchRequest<Task> = Task.fetchRequest()
return fetchRequest
}()
/// `Reuse Identifier` for TodoTableViewCell
let todoCellReuseIdentifier = "todocell"
/// `Segue Identifier` to go to TaskDetailsViewController
let taskDetailsIdentifier = "gototask"
var currentSelectedSortType: SortTypesAvailable = .sortByNameAsc
//MARK: View lifecycle methods
override func viewDidLoad() {
setupSearchController()
super.viewDidLoad()
/// Core data setup and population
loadData()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationItem.searchController = searchController
}
/// initialize ManagedObjectContext
func loadData() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let persistenceContainer = appDelegate.persistentContainer
moc = persistenceContainer.viewContext
defaultFetchRequest.sortDescriptors = currentSelectedSortType.getSortDescriptor()
setupFetchedResultsController(fetchRequest: defaultFetchRequest)
/// reloading the table view with the fetched objects
if let objects = fetchedResultsController.fetchedObjects {
self.todoList = objects
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
/// initialize FetchedResultsController
func setupFetchedResultsController(fetchRequest: NSFetchRequest<Task>) {
fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
do{
try fetchedResultsController.performFetch()
} catch {
fatalError(error.localizedDescription)
}
}
//MARK: IBActions
/// perform segue to `TaskDetailsViewController` if `+` tapped
@IBAction func addTasksTapped(_ sender: UIBarButtonItem) {
performSegue(withIdentifier: taskDetailsIdentifier, sender: Any?.self)
}
@IBAction func sortButtonTapped(_ sender: UIBarButtonItem) {
showSortAlertController()
}
///Star task
/// function called when `Star Task` tapped
func starTask(at index : Int){
//TODO: write star login
todoList[index].isFavourite = todoList[index].isFavourite ? false : true
updateTask()
}
///Delete task
/// function called when `Delete Task` tapped
func deleteTask(at index : Int){
let element = todoList.remove(at: index) /// removes task at index
/// deleting the object from core data
moc.delete(element)
do {
try moc.save()
} catch {
todoList.insert(element, at: index)
print(error.localizedDescription)
}
tableView.reloadData() /// Reload tableview with remaining data
}
/// Update task
/// function called whenever updating a task is required
func updateTask(){
do {
try moc.save()
} catch {
print(error.localizedDescription)
}
loadData()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let taskDetailVC = segue.destination as? TaskDetailsViewController{
taskDetailVC.delegate = self
taskDetailVC.task = sender as? Task
}
}
/// search controller setup
fileprivate func setupSearchController() {
resultsTableController =
self.storyboard?.instantiateViewController(withIdentifier: "ResultsTableController") as? ResultsTableController
resultsTableController.tableView.delegate = self
searchController = UISearchController(searchResultsController: resultsTableController)
searchController.delegate = self
searchController.searchResultsUpdater = self
searchController.searchBar.autocapitalizationType = .none
searchController.searchBar.delegate = self
}
//MARK: ------ Tableview Datasource methods ------
// Reference: https://developer.apple.com/documentation/uikit/uitableviewdatasource
/// function to determine `Number of rows` in tableview
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return todoList.count
}
/// function to determine `tableview cell` at a given row
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: todoCellReuseIdentifier, for: indexPath) as! TaskCell
let task = todoList[indexPath.row]
cell.title.text = task.title
cell.subtitle.text = task.dueDate
/// width adjustment of starImage
cell.starImageWidthConstraint.constant = todoList[indexPath.row].isFavourite ? 32 : 0
let priority: Priority = Priority(rawValue: task.priority ?? "") ?? Priority.low
cell.priorityImage.image = priority.getImageForPriority()
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
lastIndexTapped = indexPath.row
let task = todoList[indexPath.row]
performSegue(withIdentifier: taskDetailsIdentifier, sender: task)
}
//MARK: ----- Tableview Delagate methods ------
// Reference: https://developer.apple.com/documentation/uikit/uitableviewdelegate
/// `UISwipeActionsConfiguration` for delete and star buttons
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = UIContextualAction(style: .destructive, title: "Delete") { (_, _, _) in
self.deleteTask(at: indexPath.row)
}
let star = UIContextualAction(style: .normal, title: todoList[indexPath.row].isFavourite ? "Unstar" : "Star"){ (_, _, _) in
self.starTask(at: indexPath.row)
}
star.backgroundColor = .orange
let swipeActions = UISwipeActionsConfiguration(actions: [delete,star])
return swipeActions
}
/// function to determine `View for Footer` in tableview.
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView() /// Returns an empty view.
}
}
extension TodoViewController: NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.beginUpdates()
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.endUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert:
tableView.insertRows(at: [newIndexPath!], with: .fade)
case .delete:
tableView.deleteRows(at: [indexPath!], with: .fade)
case .update:
tableView.reloadRows(at: [indexPath!], with: .fade)
case .move:
break
@unknown default:
break
}
}
}
//MARK: - TaskDelegate
/// protocol for `saving` or `updating` `Tasks`
extension TodoViewController : TaskDelegate{
func didTapSave(task: Task) {
todoList.append(task)
do {
try moc.save()
} catch {
todoList.removeLast()
print(error.localizedDescription)
}
loadData()
}
func didTapUpdate(task: Task) {
/// Reload tableview with new data
updateTask()
}
}
// MARK: - Search Bar Delegate
extension TodoViewController: UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
func updateSearchResults(for searchController: UISearchController) {
/// perform search only when there is some text
if let text: String = searchController.searchBar.text?.lowercased(), text.count > 0, let resultsController = searchController.searchResultsController as? ResultsTableController {
resultsController.todoList = todoList.filter({ (task) -> Bool in
if task.title?.lowercased().contains(text) == true || task.subTasks?.lowercased().contains(text) == true {
return true
}
return false
})
let fetchRequest : NSFetchRequest<Task> = Task.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "title", ascending: true)]
fetchRequest.predicate = NSPredicate(format: "title contains[c] %@", text)
setupFetchedResultsController(fetchRequest: fetchRequest)
resultsController.tableView.reloadData()
} else {
/// default case when text not available or text length is zero
tableView.reloadData()
}
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
tableView.reloadData()
}
}
// MARK:- Sort functionality
extension TodoViewController {
func showSortAlertController() {
let alertController = UIAlertController(title: nil, message: "Choose sort type", preferredStyle: .actionSheet)
SortTypesAvailable.allCases.forEach { (sortType) in
let action = UIAlertAction(title: sortType.getTitleForSortType(), style: .default) { (_) in
self.currentSelectedSortType = sortType
self.loadData()
}
alertController.addAction(action)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
self.present(alertController, animated: true)
}
}