-
-
Notifications
You must be signed in to change notification settings - Fork 907
/
Copy pathNCMainNavigationController.swift
347 lines (273 loc) · 17 KB
/
NCMainNavigationController.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// SPDX-FileCopyrightText: Nextcloud GmbH
// SPDX-FileCopyrightText: 2025 Marino Faggiana
// SPDX-License-Identifier: GPL-3.0-or-later
import UIKit
import SwiftUI
import NextcloudKit
class NCMainNavigationController: UINavigationController, UINavigationControllerDelegate {
let database = NCManageDatabase.shared
let global = NCGlobal.shared
let utility = NCUtility()
let utilityFileSystem = NCUtilityFileSystem()
let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
private var timer: Timer?
var controller: NCMainTabBarController? {
self.tabBarController as? NCMainTabBarController
}
var collectionViewCommon: NCCollectionViewCommon? {
topViewController as? NCCollectionViewCommon
}
var session: NCSession.Session {
NCSession.shared.getSession(controller: controller)
}
let menuButtonTag = 100
let assistantButtonTag = 101
let notificationsButtonTag = 102
let transfersButtonTag = 103
let menuButton = UIButton(type: .system)
var menuBarButtonItem: UIBarButtonItem {
let item = UIBarButtonItem(customView: menuButton)
item.tag = menuButtonTag
return item
}
let assistantButton = UIButton(type: .system)
var assistantButtonItem: UIBarButtonItem {
let item = UIBarButtonItem(customView: assistantButton)
item.tag = assistantButtonTag
return item
}
let notificationsButton = UIButton(type: .system)
var notificationsButtonItem: UIBarButtonItem {
let item = UIBarButtonItem(customView: notificationsButton)
item.tag = notificationsButtonTag
return item
}
let transfersButton = UIButton(type: .system)
var transfersButtonItem: UIBarButtonItem {
let item = UIBarButtonItem(customView: transfersButton)
item.tag = transfersButtonTag
return item
}
// MARK: - View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
menuButton.setImage(UIImage(systemName: "ellipsis.circle"), for: .normal)
menuButton.tintColor = NCBrandColor.shared.iconImageColor
menuButton.menu = createRightMenu()
menuButton.showsMenuAsPrimaryAction = true
assistantButton.setImage(UIImage(systemName: "sparkles"), for: .normal)
assistantButton.tintColor = NCBrandColor.shared.iconImageColor
assistantButton.addAction(UIAction(handler: { _ in
let assistant = NCAssistant()
.environmentObject(NCAssistantModel(controller: self.controller))
let hostingController = UIHostingController(rootView: assistant)
self.present(hostingController, animated: true, completion: nil)
}), for: .touchUpInside)
notificationsButton.setImage(UIImage(systemName: "bell.fill"), for: .normal)
notificationsButton.tintColor = NCBrandColor.shared.iconImageColor
notificationsButton.addAction(UIAction(handler: { _ in
if let navigationController = UIStoryboard(name: "NCNotification", bundle: nil).instantiateInitialViewController() as? UINavigationController,
let viewController = navigationController.topViewController as? NCNotification {
viewController.modalPresentationStyle = .pageSheet
viewController.session = self.session
self.present(navigationController, animated: true, completion: nil)
}
}), for: .touchUpInside)
transfersButton.setImage(UIImage(systemName: "arrow.left.arrow.right.circle.fill"), for: .normal)
transfersButton.tintColor = NCBrandColor.shared.iconImageColor
transfersButton.addAction(UIAction(handler: { _ in
if let navigationController = UIStoryboard(name: "NCTransfers", bundle: nil).instantiateInitialViewController() as? UINavigationController,
let viewController = navigationController.topViewController as? NCTransfers {
viewController.modalPresentationStyle = .pageSheet
self.present(navigationController, animated: true, completion: nil)
}
}), for: .touchUpInside)
navigationBar.prefersLargeTitles = true
setNavigationBarHidden(false, animated: true)
NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: nil) { _ in
self.timer?.invalidate()
self.timer = nil
}
NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil) { _ in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
if UIApplication.shared.applicationState == .active {
self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { _ in
self.updateRightBarButtonItems()
})
}
}
}
}
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
setNavigationBarAppearance()
self.updateRightBarButtonItems()
}
// MARK: - Right
func setNavigationRightItems() {
guard let collectionViewCommon
else {
return
}
if collectionViewCommon.isEditMode {
collectionViewCommon.tabBarSelect?.update(fileSelect: collectionViewCommon.fileSelect, metadatas: collectionViewCommon.getSelectedMetadatas(), userId: session.userId)
collectionViewCommon.tabBarSelect?.show()
let select = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: .done) {
collectionViewCommon.setEditMode(false)
collectionViewCommon.collectionView.reloadData()
}
self.collectionViewCommon?.navigationItem.rightBarButtonItems = [select]
} else {
collectionViewCommon.tabBarSelect?.hide()
self.updateRightBarButtonItems()
}
// fix, if the tabbar was hidden before the update, set it in hidden
if self.tabBarController?.tabBar.isHidden ?? true,
collectionViewCommon.tabBarSelect?.isHidden() ?? true {
self.tabBarController?.tabBar.isHidden = true
}
}
func updateRightBarButtonItems() {
guard let collectionViewCommon,
!collectionViewCommon.isEditMode,
let controller
else {
return
}
let capabilities = NCCapabilities.shared.getCapabilities(account: session.account)
let resultsCount = self.database.getResultsMetadatas(predicate: NSPredicate(format: "status != %i", NCGlobal.shared.metadataStatusNormal))?.count ?? 0
var tempRightBarButtonItems = [self.menuBarButtonItem]
var tempTotalTags = self.menuBarButtonItem.tag
var totalTags = 0
if let rightBarButtonItems = self.collectionViewCommon?.navigationItem.rightBarButtonItems,
let menuBarButtonItem = rightBarButtonItems.first(where: { $0.tag == menuButtonTag }),
let menuButton = menuBarButtonItem.customView as? UIButton {
menuButton.menu = createRightMenu()
}
if let rightBarButtonItems = collectionViewCommon.navigationItem.rightBarButtonItems {
for item in rightBarButtonItems {
totalTags = totalTags + item.tag
}
}
if capabilities.capabilityAssistantEnabled {
tempRightBarButtonItems.append(self.assistantButtonItem)
tempTotalTags = tempTotalTags + self.assistantButtonItem.tag
}
if controller.availableNotifications {
tempRightBarButtonItems.append(self.notificationsButtonItem)
tempTotalTags = tempTotalTags + self.notificationsButtonItem.tag
}
if resultsCount > 0 {
tempRightBarButtonItems.append(self.transfersButtonItem)
tempTotalTags = tempTotalTags + self.transfersButtonItem.tag
}
if totalTags != tempTotalTags {
collectionViewCommon.navigationItem.rightBarButtonItems = tempRightBarButtonItems
}
}
func createRightMenu() -> UIMenu? { return nil }
func createRightMenuActions() -> (select: UIAction, viewStyleSubmenu: UIMenu, sortSubmenu: UIMenu, personalFilesOnlyAction: UIAction, showDescription: UIAction, showRecommendedFiles: UIAction)? {
guard let collectionViewCommon,
let layoutForView = database.getLayoutForView(account: session.account, key: collectionViewCommon.layoutKey, serverUrl: collectionViewCommon.serverUrl) else { return nil }
let select = UIAction(title: NSLocalizedString("_select_", comment: ""),
image: utility.loadImage(named: "checkmark.circle"),
attributes: (collectionViewCommon.dataSource.isEmpty() || NCNetworking.shared.isOffline) ? .disabled : []) { _ in
collectionViewCommon.setEditMode(true)
collectionViewCommon.collectionView.reloadData()
}
let list = UIAction(title: NSLocalizedString("_list_", comment: ""), image: utility.loadImage(named: "list.bullet"), state: layoutForView.layout == global.layoutList ? .on : .off) { _ in
layoutForView.layout = self.global.layoutList
NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
object: nil,
userInfo: ["account": self.session.account,
"serverUrl": collectionViewCommon.serverUrl,
"layoutForView": layoutForView])
}
let grid = UIAction(title: NSLocalizedString("_icons_", comment: ""), image: utility.loadImage(named: "square.grid.2x2"), state: layoutForView.layout == global.layoutGrid ? .on : .off) { _ in
layoutForView.layout = self.global.layoutGrid
NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
object: nil,
userInfo: ["account": self.session.account,
"serverUrl": collectionViewCommon.serverUrl,
"layoutForView": layoutForView])
}
let mediaSquare = UIAction(title: NSLocalizedString("_media_square_", comment: ""), image: utility.loadImage(named: "square.grid.3x3"), state: layoutForView.layout == global.layoutPhotoSquare ? .on : .off) { _ in
layoutForView.layout = self.global.layoutPhotoSquare
NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
object: nil,
userInfo: ["account": self.session.account,
"serverUrl": collectionViewCommon.serverUrl,
"layoutForView": layoutForView])
}
let mediaRatio = UIAction(title: NSLocalizedString("_media_ratio_", comment: ""), image: utility.loadImage(named: "rectangle.grid.3x2"), state: layoutForView.layout == self.global.layoutPhotoRatio ? .on : .off) { _ in
layoutForView.layout = self.global.layoutPhotoRatio
NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
object: nil,
userInfo: ["account": self.session.account,
"serverUrl": collectionViewCommon.serverUrl,
"layoutForView": layoutForView])
}
let viewStyleSubmenu = UIMenu(title: "", options: .displayInline, children: [list, grid, mediaSquare, mediaRatio])
let ascending = layoutForView.ascending
let ascendingChevronImage = utility.loadImage(named: ascending ? "chevron.up" : "chevron.down")
let isName = layoutForView.sort == "fileName"
let isDate = layoutForView.sort == "date"
let isSize = layoutForView.sort == "size"
let byName = UIAction(title: NSLocalizedString("_name_", comment: ""), image: isName ? ascendingChevronImage : nil, state: isName ? .on : .off) { _ in
if isName { // repeated press
layoutForView.ascending = !layoutForView.ascending
}
layoutForView.sort = "fileName"
NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
object: nil,
userInfo: ["account": self.session.account,
"serverUrl": collectionViewCommon.serverUrl,
"layoutForView": layoutForView])
}
let byNewest = UIAction(title: NSLocalizedString("_date_", comment: ""), image: isDate ? ascendingChevronImage : nil, state: isDate ? .on : .off) { _ in
if isDate { // repeated press
layoutForView.ascending = !layoutForView.ascending
}
layoutForView.sort = "date"
NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
object: nil,
userInfo: ["account": self.session.account,
"serverUrl": collectionViewCommon.serverUrl,
"layoutForView": layoutForView])
}
let byLargest = UIAction(title: NSLocalizedString("_size_", comment: ""), image: isSize ? ascendingChevronImage : nil, state: isSize ? .on : .off) { _ in
if isSize { // repeated press
layoutForView.ascending = !layoutForView.ascending
}
layoutForView.sort = "size"
NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
object: nil,
userInfo: ["account": self.session.account,
"serverUrl": collectionViewCommon.serverUrl,
"layoutForView": layoutForView])
}
let sortSubmenu = UIMenu(title: NSLocalizedString("_order_by_", comment: ""), options: .displayInline, children: [byName, byNewest, byLargest])
let personalFilesOnly = NCKeychain().getPersonalFilesOnly(account: self.session.account)
let personalFilesOnlyAction = UIAction(title: NSLocalizedString("_personal_files_only_", comment: ""), image: utility.loadImage(named: "folder.badge.person.crop", colors: NCBrandColor.shared.iconImageMultiColors), state: personalFilesOnly ? .on : .off) { _ in
NCKeychain().setPersonalFilesOnly(account: self.session.account, value: !personalFilesOnly)
NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl": collectionViewCommon.serverUrl, "clearDataSource": true])
self.setNavigationRightItems()
}
let showDescriptionKeychain = NCKeychain().showDescription
let showDescription = UIAction(title: NSLocalizedString("_show_description_", comment: ""), state: showDescriptionKeychain ? .on : .off) { _ in
NCKeychain().showDescription = !showDescriptionKeychain
NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl": collectionViewCommon.serverUrl, "clearDataSource": true])
self.setNavigationRightItems()
}
let showRecommendedFilesKeychain = NCKeychain().showRecommendedFiles
let capabilityRecommendations = NCCapabilities.shared.getCapabilities(account: session.account).capabilityRecommendations
let showRecommendedFiles = UIAction(title: NSLocalizedString("_show_recommended_files_", comment: ""), attributes: !capabilityRecommendations ? .disabled : [], state: showRecommendedFilesKeychain ? .on : .off) { _ in
NCKeychain().showRecommendedFiles = !showRecommendedFilesKeychain
collectionViewCommon.collectionView.reloadData()
self.setNavigationRightItems()
}
return (select, viewStyleSubmenu, sortSubmenu, personalFilesOnlyAction, showDescription, showRecommendedFiles)
}
// MARK: - Left
func setNavigationLeftItems() { }
}