-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNoteListViewController.swift
164 lines (119 loc) · 5.23 KB
/
NoteListViewController.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
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
//
// NoteListViewController.swift
// Notes
//
// Created by Julian Moorhouse on 15/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
class NoteListViewController: UITableViewController {
var folder: Folder?
var notes = [Note]()
var notesInFolder = [Note]()
var toolbarLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.backBarButtonItem = UIBarButtonItem(title: "Notes", style: .plain, target: nil, action: nil)
title = folder?.name
let newFolder = UIBarButtonItem(barButtonSystemItem: .compose, target: self, action: #selector(composeNewNote))
let flex = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
let fixed = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: self, action: nil)
toolbarLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
toolbarLabel.text = "0 Notes"
toolbarLabel.center = CGPoint(x: view.frame.midX, y: view.frame.height)
toolbarLabel.textAlignment = NSTextAlignment.center
let toolbarTitle = UIBarButtonItem(customView: toolbarLabel)
setToolbarItems([fixed, flex, toolbarTitle, flex, newFolder], animated: true)
loadNotes()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
navigationController?.navigationBar.prefersLargeTitles = true
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return notesInFolder.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Note", for: indexPath)
let item = notesInFolder[indexPath.row]
let array = item.content?.components(separatedBy: .newlines)
let firstLine = array?[0]
let secondLine = array?[1]
cell.textLabel?.text = firstLine
cell.detailTextLabel?.text = secondLine
cell.accessoryType = .disclosureIndicator
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "sb_Note") as? NoteViewController {
let item = notesInFolder[indexPath.row]
vc.note = item
vc.didUpdateItem = {
[weak self] (item) in
// update
self?.notes.filter {$0.uuid == item.uuid }.first?.content = item.content
self?.saveNotes()
self?.tableView.reloadData()
}
vc.didDeleteItem = {
[weak self] (item) in
// update
self?.notes.removeAll() {$0.uuid == item.uuid }
self?.saveNotes()
self?.tableView.reloadData()
}
navigationController?.pushViewController(vc, animated: true)
}
}
@objc func composeNewNote() {
if let vc = storyboard?.instantiateViewController(withIdentifier: "sb_Note") as? NoteViewController {
guard let folderUuid = folder?.uuid else { return }
let item = Note(content: "", folderUuid: folderUuid)
vc.note = item
vc.didUpdateItem = {
[weak self] (item) in
// update
self?.notes.append(item)
self?.saveNotes()
self?.tableView.reloadData()
}
vc.didDeleteItem = {
[weak self] (item) in
// update
self?.notes.removeAll() {$0.uuid == item.uuid }
self?.saveNotes()
self?.tableView.reloadData()
}
navigationController?.pushViewController(vc, animated: true)
}
}
func saveNotes() {
let jsonEncoder = JSONEncoder()
if let savedData = try? jsonEncoder.encode(notes) {
let defaults = UserDefaults.standard
defaults.set(savedData, forKey: "notes")
} else {
print("Failed to save notes")
}
let uuid = folder?.uuid!
notesInFolder = notes.filter{ $0.folderUuid == uuid }
}
func loadNotes() {
let defaults = UserDefaults.standard
if let savedNotes = defaults.object(forKey: "notes") as? Data {
let jsonDecoder = JSONDecoder()
do {
notes = try jsonDecoder.decode([Note].self, from: savedNotes)
} catch {
print("Failed to load notes.")
}
let uuid = folder?.uuid!
notesInFolder = notes.filter{ $0.folderUuid == uuid }
toolbarLabel.text = "\(notesInFolder.count) Notes"
}
}
}