-
-
Notifications
You must be signed in to change notification settings - Fork 907
/
Copy pathNCMainTabBarController.swift
142 lines (124 loc) · 5.71 KB
/
NCMainTabBarController.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
//
// NCMainTabBarController.swift
// Nextcloud
//
// Created by Marino Faggiana on 02/04/24.
// Copyright © 2024 Marino Faggiana. All rights reserved.
//
// Author Marino Faggiana <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
import UIKit
import SwiftUI
import NextcloudKit
struct NavigationCollectionViewCommon {
var serverUrl: String
var navigationController: UINavigationController?
var viewController: NCCollectionViewCommon
}
class NCMainTabBarController: UITabBarController {
var sceneIdentifier: String = UUID().uuidString
var account = ""
var availableNotifications: Bool = false
var documentPickerViewController: NCDocumentPickerViewController?
let navigationCollectionViewCommon = ThreadSafeArray<NavigationCollectionViewCommon>()
private var previousIndex: Int?
private let groupDefaults = UserDefaults(suiteName: NCBrandOptions.shared.capabilitiesGroup)
private var checkUserDelaultErrorInProgress: Bool = false
private var timer: Timer?
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
if #available(iOS 17.0, *) {
traitOverrides.horizontalSizeClass = .compact
}
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil, queue: .main) { [weak self] notification in
if let userInfo = notification.userInfo as? NSDictionary,
let account = userInfo["account"] as? String,
let tabBar = self?.tabBar as? NCMainTabBar,
self?.account == account {
let color = NCBrandColor.shared.getElement(account: account)
tabBar.color = color
tabBar.tintColor = color
tabBar.setNeedsDisplay()
}
}
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterCheckUserDelaultErrorDone), object: nil, queue: nil) { notification in
if let userInfo = notification.userInfo,
let account = userInfo["account"] as? String,
let controller = userInfo["controller"] as? NCMainTabBarController,
account == self.account,
controller == self {
self.checkUserDelaultErrorInProgress = false
}
}
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.timerCheckServerError()
}
}
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
previousIndex = selectedIndex
if NCBrandOptions.shared.enforce_passcode_lock && NCKeychain().passcode.isEmptyOrNil {
let vc = UIHostingController(rootView: SetupPasscodeView(isLockActive: .constant(false)))
vc.isModalInPresentation = true
present(vc, animated: true)
}
}
private func timerCheckServerError() {
self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: false, block: { _ in
NCNetworking.shared.checkServerError(account: self.account, controller: self) {
self.timerCheckServerError()
}
})
}
func currentViewController() -> UIViewController? {
return (selectedViewController as? UINavigationController)?.topViewController
}
func currentServerUrl() -> String {
let session = NCSession.shared.getSession(account: account)
var serverUrl = NCUtilityFileSystem().getHomeServer(session: session)
let viewController = currentViewController()
if let collectionViewCommon = viewController as? NCCollectionViewCommon {
if !collectionViewCommon.serverUrl.isEmpty {
serverUrl = collectionViewCommon.serverUrl
}
}
return serverUrl
}
}
extension NCMainTabBarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if previousIndex == tabBarController.selectedIndex {
scrollToTop(viewController: viewController)
}
previousIndex = tabBarController.selectedIndex
}
private func scrollToTop(viewController: UIViewController) {
guard let navigationController = viewController as? UINavigationController,
let topViewController = navigationController.topViewController else { return }
if let scrollView = topViewController.view.subviews.compactMap({ $0 as? UIScrollView }).first {
scrollView.setContentOffset(CGPoint(x: 0, y: -scrollView.adjustedContentInset.top), animated: true)
}
}
}