-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllDiaryTableViewController.swift
168 lines (123 loc) · 5.33 KB
/
AllDiaryTableViewController.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
165
166
167
168
//
// AllDiaryTableViewController.swift
// ass2-5140
//
// Created by haofang Liu on 1/10/19.
// Copyright © 2019 haofang Liu. All rights reserved.
//
import UIKit
class AllDiaryTableViewController: UITableViewController, UISearchResultsUpdating, DatabaseListener {
let SECTION_DIARY = 0;
let SECTION_COUNT = 1;
let CELL_DIARY = "diaryCell"
let CELL_COUNT = "totalDiaryCount"
var selectedDiary:Diary?
var allDiaries: [Diary] = []
var filteredDiary: [Diary] = []
weak var dairyDelegate: AddDiaryDelegate?
weak var databaseController: DatabaseProtocol?
override func viewDidLoad() {
super.viewDidLoad()
// Get the database controller once from the App Delegate
let appDelegate = UIApplication.shared.delegate as! AppDelegate
databaseController = appDelegate.databaseController
let searchController = UISearchController(searchResultsController: nil);
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Heroes"
navigationItem.searchController = searchController
// This view controller decides how the search controller is presented.
definesPresentationContext = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
databaseController?.addListener(listener: self)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
databaseController?.removeListener(listener: self)
}
func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text?.lowercased(), searchText.count > 0 {
filteredDiary = allDiaries.filter({(diary: Diary) -> Bool in
return diary.title.lowercased().contains(searchText)
})
}
else {
filteredDiary = allDiaries;
}
tableView.reloadData();
}
// MARK: - Database Listener
var listenerType = ListenerType.diary
func onDiaryListChange(change: DatabaseChange, diaries: [Diary]) {
allDiaries = diaries
updateSearchResults(for: navigationItem.searchController!)
}
func onRgbChange(change: DatabaseChange, rgb: Rgb) {
}
func onWeatherChange(change: DatabaseChange, weather: Weather) {
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == SECTION_DIARY {
return filteredDiary.count
} else {
return 1
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == SECTION_DIARY {
let diaryCell = tableView.dequeueReusableCell(withIdentifier: CELL_DIARY, for: indexPath) as! DiaryTableViewCell
let diaryOne = filteredDiary[indexPath.row]
diaryCell.diaryTitle.text = diaryOne.title
diaryCell.diaryDate.text = diaryOne.date
return diaryCell
}
let countCell = tableView.dequeueReusableCell(withIdentifier: CELL_COUNT, for: indexPath)
countCell.textLabel?.text = "\(allDiaries.count) daires in the database"
countCell.selectionStyle = .none
return countCell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedDiary = filteredDiary[indexPath.row]
performSegue(withIdentifier: "viewDetail", sender: nil)
}
/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
let diaryDelete = filteredDiary[indexPath.row]
databaseController?.deleteDiary(diary: diaryDelete)
}
}
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "viewDetail" {
let controller = segue.destination as! DiaryDetailViewController
controller.diarySelected = self.selectedDiary!
}
}
}