@@ -17,6 +17,11 @@ import RealmSwift
1717@UIApplicationMain
1818class AppDelegate : UIResponder , UIApplicationDelegate , UNUserNotificationCenterDelegate {
1919 var backgroundSessionCompletionHandler : ( ( ) -> Void ) ?
20+ var activeLogin : NCLogin ?
21+ var activeLoginWeb : NCLoginWeb ?
22+ var taskAutoUploadDate : Date = Date ( )
23+ var orientationLock = UIInterfaceOrientationMask . all
24+ @objc let adjust = AdjustHelper ( )
2025 var isUiTestingEnabled : Bool {
2126 return ProcessInfo . processInfo. arguments. contains ( " UI_TESTING " )
2227 }
@@ -31,23 +36,32 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
3136
3237 var bgTask : UIBackgroundTaskIdentifier = . invalid
3338 var pushSubscriptionTask : Task < Void , Never > ?
34-
39+ var window : UIWindow ?
40+ var sceneIdentifier : String = " "
41+ var activeViewController : UIViewController ?
42+ var account : String = " "
43+ var urlBase : String = " "
44+ var user : String = " "
45+ var userId : String = " "
46+ var password : String = " "
47+ var timerErrorNetworking : Timer ?
48+
3549 func application( _ application: UIApplication , didFinishLaunchingWithOptions launchOptions: [ UIApplication . LaunchOptionsKey : Any ] ? ) -> Bool {
3650 if isUiTestingEnabled {
3751 Task {
3852 await NCAccount ( ) . deleteAllAccounts ( )
3953 }
4054 }
41- UINavigationBar . appearance ( ) . tintColor = NCBrandColor . shared. customer
42- UIToolbar . appearance ( ) . tintColor = NCBrandColor . shared. customer
43-
4455 let utilityFileSystem = NCUtilityFileSystem ( )
4556 let utility = NCUtility ( )
4657
4758 utilityFileSystem. createDirectoryStandard ( )
4859 utilityFileSystem. emptyTemporaryDirectory ( )
4960 utilityFileSystem. clearCacheDirectory ( " com.limit-point.LivePhoto " )
5061
62+ UINavigationBar . appearance ( ) . tintColor = NCBrandColor . shared. brand
63+ UIView . appearance ( whenContainedInInstancesOf: [ UIAlertController . self] ) . tintColor = NCBrandColor . shared. brand
64+
5165 let versionNextcloudiOS = String ( format: NCBrandOptions . shared. textCopyrightNextcloudiOS, utility. getVersionBuild ( ) )
5266
5367 NCAppVersionManager . shared. checkAndUpdateInstallState ( )
@@ -122,6 +136,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
122136 if NCBrandOptions . shared. enforce_passcode_lock {
123137 NCPreferences ( ) . requestPasscodeAtStart = true
124138 }
139+
140+ // if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
141+ // for window in windowScene.windows {
142+ // let imageViews = window.allImageViews()
143+ // // Do something with the imageViews
144+ // for imageView in imageViews {
145+ // print("Found image view: \(imageView)")
146+ // imageView.tintColor = UITraitCollection.current.userInterfaceStyle == .dark ? .white : .black
147+ // }
148+ // }
149+ // }
125150
126151 return true
127152 }
@@ -203,7 +228,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
203228 task. setTaskCompleted ( success: true )
204229 }
205230
206- await backgroundSync ( task: task)
231+ guard let tblAccount = await NCManageDatabase . shared. getActiveTableAccountAsync ( ) else {
232+ nkLog ( tag: self . global. logTagTask, emoji: . info, message: " No active account or background task already running " )
233+ return
234+ }
235+
236+ await backgroundSync ( tblAccount: tblAccount, task: task)
207237 }
208238 }
209239
@@ -284,12 +314,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
284314 guard !expired else { return }
285315
286316 let err = await NCNetworking . shared. createFolderForAutoUpload (
287- serverUrlFileName: metadata . serverUrlFileName,
288- account: metadata . account
317+ serverUrlFileName: meta . serverUrlFileName,
318+ account: meta . account
289319 )
290320 // Fail-fast: abort the whole sync on first failure
291321 if err != . success {
292- nkLog ( tag: self . global. logTagBgSync, emoji: . error, message: " Create folder ' \( metadata . serverUrlFileName) ' failed: \( err. errorCode) – aborting sync " )
322+ nkLog ( tag: self . global. logTagBgSync, emoji: . error, message: " Create folder ' \( meta . serverUrlFileName) ' failed: \( err. errorCode) – aborting sync " )
293323 return
294324 }
295325 }
@@ -440,7 +470,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
440470 }
441471 }
442472
443- // MARK: -
473+ // MARK: - Trust Certificate Error
444474
445475 func trustCertificateError( host: String ) {
446476 guard let activeTblAccount = NCManageDatabase . shared. getActiveTableAccount ( ) ,
@@ -492,6 +522,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
492522
493523 NCPreferences ( ) . removeAll ( )
494524
525+ // Reset App Icon badge / File Icon badge
526+ if #available( iOS 17 . 0 , * ) {
527+ UNUserNotificationCenter . current ( ) . setBadgeCount ( 0 )
528+ }
495529 exit ( 0 )
496530 }
497531
@@ -501,6 +535,151 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
501535 let applicationHandle = NCApplicationHandle ( )
502536 return applicationHandle. applicationOpenUserActivity ( userActivity)
503537 }
538+
539+ // MARK: - Login
540+
541+ func openLogin( selector: Int , window: UIWindow ? = nil ) {
542+ UIApplication . shared. allSceneSessionDestructionExceptFirst ( )
543+
544+ // Nextcloud standard login
545+ if selector == NCGlobal . shared. introSignup {
546+ if activeLogin? . view. window == nil {
547+ if selector == NCGlobal . shared. introSignup {
548+ let web = UIStoryboard ( name: " NCLogin " , bundle: nil ) . instantiateViewController ( withIdentifier: " NCLoginProvider " ) as? NCLoginProvider
549+ web? . initialURLString = NCBrandOptions . shared. linkloginPreferredProviders
550+ showLoginViewController ( web)
551+ } else {
552+ activeLogin = UIStoryboard ( name: " NCLogin " , bundle: nil ) . instantiateViewController ( withIdentifier: " NCLogin " ) as? NCLogin
553+ if let controller = UIApplication . shared. firstWindow? . rootViewController as? NCMainTabBarController , !controller. account. isEmpty {
554+ let session = NCSession . shared. getSession ( account: controller. account)
555+ activeLogin? . urlBase = session. urlBase
556+ }
557+ showLoginViewController ( activeLogin)
558+ }
559+ }
560+ } else {
561+ if activeLogin? . view. window == nil {
562+ activeLogin = UIStoryboard ( name: " NCLogin " , bundle: nil ) . instantiateViewController ( withIdentifier: " NCLogin " ) as? NCLogin
563+ activeLogin? . urlBase = NCBrandOptions . shared. disable_request_login_url ? NCBrandOptions . shared. loginBaseUrl : " "
564+ showLoginViewController ( activeLogin)
565+ }
566+ }
567+ }
568+
569+ func openLogin( viewController: UIViewController ? , selector: Int , openLoginWeb: Bool ) {
570+ // openLogin(selector: NCGlobal.shared.introLogin)
571+ // [WEBPersonalized] [AppConfig]
572+ if NCBrandOptions . shared. use_login_web_personalized || NCBrandOptions . shared. use_AppConfig {
573+
574+ if activeLoginWeb? . view. window == nil {
575+ activeLoginWeb = UIStoryboard ( name: " NCLogin " , bundle: nil ) . instantiateViewController ( withIdentifier: " NCLoginWeb " ) as? NCLoginWeb
576+ activeLoginWeb? . urlBase = NCBrandOptions . shared. loginBaseUrl
577+ showLoginViewController ( activeLoginWeb, contextViewController: viewController)
578+ }
579+ return
580+ }
581+
582+ // Nextcloud standard login
583+ if selector == NCGlobal . shared. introSignup {
584+
585+ if activeLoginWeb? . view. window == nil {
586+ activeLoginWeb = UIStoryboard ( name: " NCLogin " , bundle: nil ) . instantiateViewController ( withIdentifier: " NCLoginWeb " ) as? NCLoginWeb
587+ if selector == NCGlobal . shared. introSignup {
588+ activeLoginWeb? . urlBase = NCBrandOptions . shared. linkloginPreferredProviders
589+ } else {
590+ activeLoginWeb? . urlBase = self . urlBase
591+ }
592+ showLoginViewController ( activeLoginWeb, contextViewController: viewController)
593+ }
594+
595+ } else if NCBrandOptions . shared. disable_intro && NCBrandOptions . shared. disable_request_login_url {
596+
597+ if activeLoginWeb? . view. window == nil {
598+ activeLoginWeb = UIStoryboard ( name: " NCLogin " , bundle: nil ) . instantiateViewController ( withIdentifier: " NCLoginWeb " ) as? NCLoginWeb
599+ activeLoginWeb? . urlBase = NCBrandOptions . shared. loginBaseUrl
600+ showLoginViewController ( activeLoginWeb, contextViewController: viewController)
601+ }
602+
603+ } else if openLoginWeb {
604+
605+ // Used also for reinsert the account (change passwd)
606+ if activeLoginWeb? . view. window == nil {
607+ activeLoginWeb = UIStoryboard ( name: " NCLogin " , bundle: nil ) . instantiateViewController ( withIdentifier: " NCLoginWeb " ) as? NCLoginWeb
608+ activeLoginWeb? . urlBase = urlBase
609+ activeLoginWeb? . user = user
610+ showLoginViewController ( activeLoginWeb, contextViewController: viewController)
611+ }
612+
613+ } else {
614+
615+ if activeLogin? . view. window == nil {
616+ activeLogin = UIStoryboard ( name: " NCLogin " , bundle: nil ) . instantiateViewController ( withIdentifier: " NCLogin " ) as? NCLogin
617+ showLoginViewController ( activeLogin, contextViewController: viewController)
618+ }
619+ }
620+ }
621+
622+ func showLoginViewController( _ viewController: UIViewController ? ) {
623+ guard let viewController else { return }
624+ let navigationController = UINavigationController ( rootViewController: viewController)
625+
626+ navigationController. modalPresentationStyle = . fullScreen
627+ navigationController. navigationBar. barStyle = . black
628+ navigationController. navigationBar. tintColor = NCBrandColor . shared. customerText
629+ navigationController. navigationBar. barTintColor = NCBrandColor . shared. customer
630+ navigationController. navigationBar. isTranslucent = false
631+
632+ if let controller = UIApplication . shared. firstWindow? . rootViewController {
633+ if let presentedVC = controller. presentedViewController, !( presentedVC is UINavigationController ) {
634+ presentedVC. dismiss ( animated: false ) {
635+ controller. present ( navigationController, animated: true )
636+ }
637+ } else {
638+ controller. present ( navigationController, animated: true )
639+ }
640+ } else {
641+ window? . rootViewController = navigationController
642+ window? . makeKeyAndVisible ( )
643+ }
644+ }
645+
646+ func showLoginViewController( _ viewController: UIViewController ? , contextViewController: UIViewController ? ) {
647+
648+ if contextViewController == nil {
649+ if let viewController = viewController {
650+ let navigationController = UINavigationController ( rootViewController: viewController)
651+ navigationController. navigationBar. barStyle = . black
652+ navigationController. navigationBar. tintColor = NCBrandColor . shared. customerText
653+ navigationController. navigationBar. barTintColor = NCBrandColor . shared. customer
654+ navigationController. navigationBar. isTranslucent = false
655+ window? . rootViewController = navigationController
656+ window? . makeKeyAndVisible ( )
657+ }
658+ } else if contextViewController is UINavigationController {
659+ if let contextViewController = contextViewController, let viewController = viewController {
660+ ( contextViewController as? UINavigationController ) ? . pushViewController ( viewController, animated: true )
661+ }
662+ } else {
663+ if let viewController = viewController, let contextViewController = contextViewController {
664+ let navigationController = UINavigationController ( rootViewController: viewController)
665+ navigationController. modalPresentationStyle = . fullScreen
666+ navigationController. navigationBar. barStyle = . black
667+ navigationController. navigationBar. tintColor = NCBrandColor . shared. customerText
668+ navigationController. navigationBar. barTintColor = NCBrandColor . shared. customer
669+ navigationController. navigationBar. isTranslucent = false
670+ contextViewController. present ( navigationController, animated: true ) { }
671+ }
672+ }
673+ }
674+
675+ @objc func startTimerErrorNetworking( ) {
676+ timerErrorNetworking = Timer . scheduledTimer ( timeInterval: 3 , target: self , selector: #selector( checkErrorNetworking) , userInfo: nil , repeats: true )
677+ }
678+
679+ @objc private func checkErrorNetworking( ) {
680+ guard !account. isEmpty, NCKeychain ( ) . getPassword ( account: account) . isEmpty else { return }
681+ openLogin ( viewController: window? . rootViewController, selector: NCGlobal . shared. introLogin, openLoginWeb: true )
682+ }
504683}
505684
506685// MARK: - Extension
0 commit comments