TabsViewController.swift
6.22 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
//
// TabsViewController.swift
// browser
//
// Created by Artem Talko on 28.09.2023.
//
import UIKit
import RealmSwift
import Realm
final class TabsViewController: UIViewController {
private var tabsData: [BrowserTabDataBase] = []
var cellIndexPathForTransition: IndexPath?
let mainView = TabsView()
override func viewDidLoad() {
super.viewDidLoad()
initViewController()
}
private func initViewController() {
view.backgroundColor = UIColor(red: 0.984, green: 0.984, blue: 0.984, alpha: 1)
setupCollectionView()
addTargets()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewIsAppearing(_ animated: Bool) {
navigationController?.isNavigationBarHidden = true
refreshData()
setCountOfTabs()
}
override func loadView() {
view = mainView
}
}
//MARK: CollectionView
extension TabsViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return tabsData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let tab = tabsData[indexPath.row]
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: OpenedTabsCollectionViewCell.cellID, for: indexPath) as? OpenedTabsCollectionViewCell else {
return UICollectionViewCell()
}
cell.model = tab
cell.delegate = self
cell.closeTabButton.addTarget(self, action: #selector(closeTab(_:)), for: .touchUpInside)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 163.sizeW, height: 184.sizeH)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
cellIndexPathForTransition = indexPath
pushSelectedTab(indexPath)
}
}
//MARK: - Action
extension TabsViewController {
private func didTabButtonTapped(type: HistoryToolbarElementType) {
switch type {
case .add:
addNewDefaultTab()
refreshData()
let lastIndex = tabsData.endIndex - 1
let lastIndexPath = IndexPath(row: lastIndex, section: 0)
cellIndexPathForTransition = lastIndexPath
pushSelectedTab(lastIndexPath)
break
case .done:
refreshData()
let lastIndex = tabsData.endIndex - 1
let lastIndexPath = IndexPath(row: lastIndex, section: 0)
cellIndexPathForTransition = lastIndexPath
pushSelectedTab(lastIndexPath)
break
default:
break
}
}
@objc
private func closeTab(_ sender: TabCollectionViewCell) {}
}
//MARK: - Helpers
extension TabsViewController {
private func setupCollectionView() {
mainView.openedTabsCollectionView.dataSource = self
mainView.openedTabsCollectionView.delegate = self
mainView.openedTabsCollectionView.register(OpenedTabsCollectionViewCell.self, forCellWithReuseIdentifier: OpenedTabsCollectionViewCell.cellID)
}
func removeSelf() {
navigationController?.popViewController(animated: false)
}
private func addTargets() {
addToolbarTargets()
}
private func addToolbarTargets() {
mainView.tabsToolbarView.action = didTabButtonTapped
}
func getCellIndex() -> IndexPath {
return cellIndexPathForTransition ?? IndexPath(row: tabsData.count - 1, section: 0)
}
private func addNewDefaultTab() {
let defaultImage = UIImage(named: "defaultImage")
TabManager.shared.saveTabWithComplition(tabTitle: "Start Page", snapshotImage: defaultImage, tabUrl: "homeUrl") { [weak self] result in
switch result {
default:
self?.refreshData()
}
}
}
private func pushSelectedTab(_ indexPath: IndexPath) {
let index = indexPath.row
if tabsData[index].TabUrl != "homeUrl" {
let browserHomeViewController = BrowserHomeViewController(url: tabsData[index].TabUrl, currentTabId: index)
browserHomeViewController.urlManagment(tabsData[index].TabUrl)
navigationController?.pushViewController(browserHomeViewController, animated: true)
} else {
let browserHomeViewController = BrowserHomeViewController(url: tabsData[index].TabUrl, currentTabId: index)
navigationController?.pushViewController(browserHomeViewController, animated: true)
}
}
func refreshData() {
tabsData = TabManager.shared.getAllTabs()
mainView.openedTabsCollectionView.reloadData()
}
}
//MARK: - Delete tab
extension TabsViewController: OpenedTabsCellDelegate {
func deleteTabDelegateFunc(cell: OpenedTabsCollectionViewCell) {
guard let indexPath = mainView.openedTabsCollectionView.indexPath(for: cell) else {
return
}
tabsData.remove(at: indexPath.row)
mainView.openedTabsCollectionView.performBatchUpdates({
mainView.openedTabsCollectionView.deleteItems(at: [indexPath])
}, completion: { _ in
if self.tabsData.isEmpty {
self.addNewDefaultTab()
let browserHomeViewController = BrowserHomeViewController(url: "homeUrl", currentTabId: self.tabsData.count - 1)
self.navigationController?.pushViewController(browserHomeViewController, animated: true)
self.cellIndexPathForTransition = IndexPath(row: self.tabsData.count - 1, section: 0)
}
self.setCountOfTabs()
})
}
private func setCountOfTabs() {
mainView.tabsToolbarView.showedCountOfTabs.title = "\(tabsData.count) Tabs"
}
}