HistoryViewController.swift
6.46 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
//
// HistoryViewController.swift
// browser
//
// Created by Artem Talko on 29.09.2023.
//
import UIKit
import RealmSwift
final class HistoryViewController: UIViewController {
private let mainView = HistoryView()
private var objectNotificationToken: NotificationToken?
private var historyData: [HistoryElement] = [] {
didSet {
filteredData = historyData
}
}
private var filteredData: [HistoryElement] = [] {
didSet {
mainView.historyTabsTableView.reloadData()
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.isNavigationBarHidden = false
navigationItem.title = "History"
}
override func viewDidLoad() {
super.viewDidLoad()
historyData = HistoryDBManager.shared.getHistoryDataForTableView()
initViewController()
}
private func initViewController() {
setupTableView()
addTargets()
setupSearchBar()
historyChangesObserve()
}
override func loadView() {
view = mainView
view.backgroundColor = .white
navigationController?.isNavigationBarHidden = false
navigationItem.title = "History"
}
init() {
super.init(nibName: nil, bundle: nil)
historyData = HistoryDBManager.shared.getHistoryDataForTableView()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//MARK: - TableView Extention
extension HistoryViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return filteredData.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return filteredData[section].name
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredData[section].stories.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: HistoryTableViewCell.tabID, for: indexPath) as? HistoryTableViewCell else {
return UITableViewCell()
}
let row = indexPath.row
let section = indexPath.section
cell.model = filteredData[section].stories[row]
return cell
}
private func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == .delete) {
let row = indexPath.row
let section = indexPath.section
let historyCellData = filteredData[section].stories[row]
let tabId = historyCellData.historyCellId
HistoryDBManager.shared.deleteHistoryCell(tabId: tabId)
filteredData[section].stories.remove(at: row)
mainView.historyTabsTableView.reloadData()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let section = indexPath.section
let index = indexPath.row
let urlForFunc = historyData[section].stories[index].siteUrl
if let navigationController = presentingViewController as? NavigationViewController {
for viewController in navigationController.viewControllers {
if let browserHomeViewController = viewController as? BrowserHomeViewController {
browserHomeViewController.urlManagment(urlForFunc)
}
}
dismiss(animated: true)
}
}
}
//MARK: Helper
extension HistoryViewController {
private func addTargets() {
mainView.historySearchBar.searchTextFieldView.addTarget(self, action: #selector(searchBarTextChanged), for: .editingChanged)
addToolbarTargets()
}
private func addToolbarTargets() {
mainView.historyToolbarView.action = didTabButtonTapped
}
private func setupTableView() {
mainView.historyTabsTableView.dataSource = self
mainView.historyTabsTableView.delegate = self
mainView.historyTabsTableView.register(HistoryTableViewCell.self, forCellReuseIdentifier: HistoryTableViewCell.tabID)
}
}
//MARK: Targets
extension HistoryViewController {
private func didTabButtonTapped(type: historyToolbarElementType) {
if type == .clean {
HistoryDBManager.shared.cleanHistory()
historyData.removeAll()
mainView.historyTabsTableView.reloadData()
}
}
}
//MARK: Action
extension HistoryViewController {
@objc
private func searchResultAction() {
mainView.historySearchBar.searchTextFieldView.text = nil
filteredData = historyData
}
@objc
private func searchBarTextChanged() {
if let text = mainView.historySearchBar.searchTextFieldView.text {
if text.isEmpty {
filteredData = historyData
} else {
filteredData = HistoryDBManager.shared.getFilteredHistory(filter: text)
}
}
}
}
//MARK: SearchBar
extension HistoryViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
mainView.historySearchBar.searchTextFieldView.text = nil
mainView.historySearchBar.searchTextFieldView.textColor = .black
}
private func setupSearchBar() {
mainView.historySearchBar.searchTextFieldView.delegate = self
}
}
//MARK: History DB observe
extension HistoryViewController {
private func historyChangesObserve() {
let historyObjects = HistoryDBManager.shared.getHistory()
for historyObject in historyObjects {
objectNotificationToken = historyObject.observe { [self] changes in
switch changes {
case .error(let error):
print("Error observing changes in Realm: \(error)")
case .change(_, _):
break
case .deleted:
self.historyData = HistoryDBManager.shared.getHistoryDataForTableView()
self.mainView.historyTabsTableView.reloadData()
}
}
}
}
}