Browse Source

Test autoupload in widget

Signed-off-by: marinofaggiana <marino.faggiana@nextcloud.com>
marinofaggiana 2 years ago
parent
commit
4f370aef49

+ 16 - 0
Nextcloud.xcodeproj/project.pbxproj

@@ -358,6 +358,14 @@
 		F78F74342163757000C2ADAD /* NCTrash.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F78F74332163757000C2ADAD /* NCTrash.storyboard */; };
 		F78F74362163781100C2ADAD /* NCTrash.swift in Sources */ = {isa = PBXBuildFile; fileRef = F78F74352163781100C2ADAD /* NCTrash.swift */; };
 		F790110E21415BF600D7B136 /* NCViewerRichdocument.swift in Sources */ = {isa = PBXBuildFile; fileRef = F790110D21415BF600D7B136 /* NCViewerRichdocument.swift */; };
+		F793E59B28B75FB0005E4B02 /* NCAutoUpload.swift in Sources */ = {isa = PBXBuildFile; fileRef = F72CD63925C19EBF00F46F9A /* NCAutoUpload.swift */; };
+		F793E59D28B761E7005E4B02 /* NCNetworking.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75A9EE523796C6F0044CFCE /* NCNetworking.swift */; };
+		F793E59E28B763C2005E4B02 /* NCAskAuthorization.swift in Sources */ = {isa = PBXBuildFile; fileRef = F733598025C1C188002ABA72 /* NCAskAuthorization.swift */; };
+		F793E59F28B764F6005E4B02 /* NCContentPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = F765608E23BF813500765969 /* NCContentPresenter.swift */; };
+		F793E5A028B7651B005E4B02 /* NCViewCertificateDetails.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7BC287F26663F85004D46C5 /* NCViewCertificateDetails.swift */; };
+		F793E5A128B76541005E4B02 /* NotificationCenter+MainThread.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70460512499061800BB98A7 /* NotificationCenter+MainThread.swift */; };
+		F793E5A228B76580005E4B02 /* NCNetworkingChunkedUpload.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7B8CD90261AF3F7007C1359 /* NCNetworkingChunkedUpload.swift */; };
+		F793E5A528B7698D005E4B02 /* NCNetworkingProcessUpload.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70D8D8024A4A9BF000A5756 /* NCNetworkingProcessUpload.swift */; };
 		F798F0E225880608000DAFFD /* UIColor+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70CEF5523E9C7E50007035B /* UIColor+Extensions.swift */; };
 		F798F0E725880609000DAFFD /* UIColor+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70CEF5523E9C7E50007035B /* UIColor+Extensions.swift */; };
 		F798F0EC2588060A000DAFFD /* UIColor+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70CEF5523E9C7E50007035B /* UIColor+Extensions.swift */; };
@@ -2638,6 +2646,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				F78302FD28B4C42B00B84583 /* NCUserBaseUrl.swift in Sources */,
+				F793E5A128B76541005E4B02 /* NotificationCenter+MainThread.swift in Sources */,
 				F78302F928B4C3E600B84583 /* NCManageDatabase+Account.swift in Sources */,
 				F7E0710128B13BB00001B882 /* DashboardData.swift in Sources */,
 				F741F3B628B3D5720045394D /* DashboardNetworking.swift in Sources */,
@@ -2647,14 +2656,21 @@
 				F78302F828B4C3E100B84583 /* NCManageDatabase+Activity.swift in Sources */,
 				F783030228B4C4B800B84583 /* NCUtility.swift in Sources */,
 				F78302FE28B4C44700B84583 /* NCBrand.swift in Sources */,
+				F793E5A028B7651B005E4B02 /* NCViewCertificateDetails.swift in Sources */,
+				F793E59F28B764F6005E4B02 /* NCContentPresenter.swift in Sources */,
 				F78302FA28B4C3EA00B84583 /* NCManageDatabase+Metadata.swift in Sources */,
 				F783030728B4C52800B84583 /* UIColor+Extensions.swift in Sources */,
 				F783030028B4C45800B84583 /* NCGlobal.swift in Sources */,
+				F793E59D28B761E7005E4B02 /* NCNetworking.swift in Sources */,
+				F793E5A528B7698D005E4B02 /* NCNetworkingProcessUpload.swift in Sources */,
 				F78302FC28B4C3F300B84583 /* NCElementsJSON.swift in Sources */,
 				F783030628B4C51E00B84583 /* String+Extensions.swift in Sources */,
 				F72A17D828B221E300F3F159 /* DashboardWidgetView.swift in Sources */,
+				F793E59B28B75FB0005E4B02 /* NCAutoUpload.swift in Sources */,
 				F78302FB28B4C3EE00B84583 /* NCManageDatabase+Video.swift in Sources */,
+				F793E5A228B76580005E4B02 /* NCNetworkingChunkedUpload.swift in Sources */,
 				F783031228B4C8EC00B84583 /* CCUtility.m in Sources */,
+				F793E59E28B763C2005E4B02 /* NCAskAuthorization.swift in Sources */,
 				F78302FF28B4C45000B84583 /* NCUtilityFileSystem.swift in Sources */,
 				F78302F628B4C3C500B84583 /* NCDatabase.swift in Sources */,
 				F783030128B4C49700B84583 /* UIImage+Extensions.swift in Sources */,

+ 20 - 1
Widget/DashboardNetworking.swift

@@ -38,5 +38,24 @@ func readDashboard(completion: @escaping (_ dashboardData: [DashboardData], _ is
         return completion(dashboardDatasTest, true, getTitle(account: nil))
     }
 
-    completion(dashboardDatasTest, false, getTitle(account: account))
+    // LOG
+    let levelLog = CCUtility.getLogLevel()
+    let isSimulatorOrTestFlight = NCUtility.shared.isSimulatorOrTestFlight()
+    let versionNextcloudiOS = String(format: NCBrandOptions.shared.textCopyrightNextcloudiOS, NCUtility.shared.getVersionApp())
+
+    NKCommon.shared.levelLog = levelLog
+    if let pathDirectoryGroup = CCUtility.getDirectoryGroup()?.path {
+        NKCommon.shared.pathLog = pathDirectoryGroup
+    }
+    if isSimulatorOrTestFlight {
+        NKCommon.shared.writeLog("Start Dashboard widget session with level \(levelLog) " + versionNextcloudiOS + " (Simulator / TestFlight)")
+    } else {
+        NKCommon.shared.writeLog("Start Dashboard widget session with level \(levelLog) " + versionNextcloudiOS)
+    }
+    NKCommon.shared.writeLog("Start Dashboard widget [Auto upload]")
+
+    NCAutoUpload.shared.initAutoUpload(viewController: nil) { _ in
+        completion(dashboardDatasTest, false, getTitle(account: account))
+        NKCommon.shared.writeLog("Completition Dashboard widget [Auto upload]")
+    }
 }

+ 1 - 1
iOSClient/Data/NCManageDatabase.swift

@@ -70,7 +70,7 @@ class NCManageDatabase: NSObject {
             let config = Realm.Configuration(
                 fileURL: dirGroup?.appendingPathComponent(NCGlobal.shared.appDatabaseNextcloud + "/" + NCGlobal.shared.databaseDefault),
                 schemaVersion: NCGlobal.shared.databaseSchemaVersion,
-                objectTypes: [tableMetadata.self, tableLocalFile.self, tableDirectory.self, tableTag.self, tableAccount.self, tableCapabilities.self, tableE2eEncryption.self, tableE2eEncryptionLock.self, tableShare.self, tableChunk.self, tableAvatar.self]
+                objectTypes: [tableMetadata.self, tableLocalFile.self, tableDirectory.self, tableTag.self, tableAccount.self, tableCapabilities.self, tablePhotoLibrary.self, tableE2eEncryption.self, tableE2eEncryptionLock.self, tableShare.self, tableChunk.self, tableAvatar.self]
             )
 
             Realm.Configuration.defaultConfiguration = config

+ 35 - 9
iOSClient/Networking/NCAutoUpload.swift

@@ -25,6 +25,7 @@ import UIKit
 import CoreLocation
 import NCCommunication
 import Photos
+import NextcloudKit
 
 class NCAutoUpload: NSObject {
     @objc static let shared: NCAutoUpload = {
@@ -32,13 +33,12 @@ class NCAutoUpload: NSObject {
         return instance
     }()
 
-    private let appDelegate = UIApplication.shared.delegate as! AppDelegate
     private var endForAssetToUpload: Bool = false
 
     // MARK: -
 
     @objc func initAutoUpload(viewController: UIViewController?, completion: @escaping (_ items: Int) -> Void) {
-        guard let activeAccount = NCManageDatabase.shared.getActiveAccount(), activeAccount.autoUpload else {
+        guard let account = NCManageDatabase.shared.getActiveAccount(), account.autoUpload else {
             completion(0)
             return
         }
@@ -58,23 +58,25 @@ class NCAutoUpload: NSObject {
 
     @objc func autoUploadFullPhotos(viewController: UIViewController?, log: String) {
 
-        NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: appDelegate.window?.rootViewController) { hasPermission in
+        NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: viewController) { hasPermission in
             guard hasPermission else { return }
-
+            #if !EXTENSION
             NCContentPresenter.shared.messageNotification("_attention_", description: "_create_full_upload_", delay: NCGlobal.shared.dismissAfterSecondLong, type: .info, errorCode: NCGlobal.shared.errorNoError, priority: .max)
             NCActivityIndicator.shared.start()
+            #endif
             self.uploadAssetsNewAndFull(viewController: viewController, selector: NCGlobal.shared.selectorUploadAutoUploadAll, log: log) { _ in
+                #if !EXTENSION
                 NCActivityIndicator.shared.stop()
+                #endif
             }
         }
     }
 
     private func uploadAssetsNewAndFull(viewController: UIViewController?, selector: String, log: String, completion: @escaping (_ items: Int) -> Void) {
-        guard !appDelegate.account.isEmpty else {
+        guard let account = NCManageDatabase.shared.getActiveAccount() else {
             completion(0)
             return
         }
-        guard let account = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", appDelegate.account)) else { return }
 
         DispatchQueue.global(qos: .background).async {
 
@@ -83,17 +85,26 @@ class NCAutoUpload: NSObject {
 
             self.getCameraRollAssets(viewController: viewController, account: account, selector: selector, alignPhotoLibrary: false) { assets in
                 guard let assets = assets, !assets.isEmpty else {
+                    #if EXTENSION_WIDGET
+                    NKCommon.shared.writeLog("Automatic upload Dashboard widget, no new assets found [" + log + "]")
+                    #else
                     NCCommunicationCommon.shared.writeLog("Automatic upload, no new assets found [" + log + "]")
+                    #endif
                     completion(0)
                     return
                 }
+                #if EXTENSION_WIDGET
+                NKCommon.shared.writeLog("Automatic upload Dashboard widget, new \(assets.count) assets found [" + log + "]")
+                #else
                 NCCommunicationCommon.shared.writeLog("Automatic upload, new \(assets.count) assets found [" + log + "]")
-
+                #endif
                 // Create the folder for auto upload & if request the subfolders
                 if !NCNetworking.shared.createFolder(assets: assets, selector: selector, useSubFolder: account.autoUploadCreateSubfolder, account: account.account, urlBase: account.urlBase) {
+                    #if !EXTENSION
                     if selector == NCGlobal.shared.selectorUploadAutoUploadAll {
                         NCContentPresenter.shared.messageNotification("_error_", description: "_error_createsubfolders_upload_", delay: NCGlobal.shared.dismissAfterSecond, type: .error, errorCode: NCGlobal.shared.errorInternalError, priority: .max)
                     }
+                    #endif
                     return completion(0)
                 }
 
@@ -159,7 +170,11 @@ class NCAutoUpload: NSObject {
                             metadata.classFile = NCCommunicationCommon.typeClassFile.image.rawValue
                         }
                         if selector == NCGlobal.shared.selectorUploadAutoUpload {
+                            #if EXTENSION_WIDGET
+                            NKCommon.shared.writeLog("Automatic upload Dashboard widget added \(metadata.fileNameView) with Identifier \(metadata.assetLocalIdentifier)")
+                            #else
                             NCCommunicationCommon.shared.writeLog("Automatic upload added \(metadata.fileNameView) with Identifier \(metadata.assetLocalIdentifier)")
+                            #endif
                             NCManageDatabase.shared.addPhotoLibrary([asset], account: account.account)
                         }
                         metadatas.append(metadata)
@@ -167,11 +182,18 @@ class NCAutoUpload: NSObject {
                 }
 
                 self.endForAssetToUpload = true
+                #if !EXTENSION
                 if selector == NCGlobal.shared.selectorUploadAutoUploadAll {
-                    self.appDelegate.networkingProcessUpload?.createProcessUploads(metadatas: metadatas)
+                    (UIApplication.shared.delegate as! AppDelegate).networkingProcessUpload?.createProcessUploads(metadatas: metadatas)
                 } else {
-                    self.appDelegate.networkingProcessUpload?.createProcessUploads(metadatas: metadatas, verifyAlreadyExists: true)
+                    (UIApplication.shared.delegate as! AppDelegate).networkingProcessUpload?.createProcessUploads(metadatas: metadatas, verifyAlreadyExists: true)
                 }
+                #elseif EXTENSION_WIDGET
+                    if selector == NCGlobal.shared.selectorUploadAutoUpload {
+                        let networkingProcessUpload = NCNetworkingProcessUpload()
+                        networkingProcessUpload.createProcessUploads(metadatas: metadatas, verifyAlreadyExists: true)
+                    }
+                #endif
                 completion(metadatas.count)
             }
         }
@@ -187,7 +209,11 @@ class NCAutoUpload: NSObject {
             guard let assets = assets else { return }
 
             NCManageDatabase.shared.addPhotoLibrary(assets, account: activeAccount.account)
+            #if EXTENSION_WIDGET
+            NKCommon.shared.writeLog("Dashboard widget align Photo Library \(assets.count)")
+            #else
             NCCommunicationCommon.shared.writeLog("Align Photo Library \(assets.count)")
+            #endif
         }
     }
 

+ 1 - 1
iOSClient/Networking/NCNetworking.swift

@@ -387,7 +387,7 @@ import Photos
         NCCommunicationCommon.shared.writeLog("Upload file \(metadata.fileNameView) with Identifier \(metadata.assetLocalIdentifier) with size \(metadata.size) [CHUNCK \(metadata.chunk), E2EE \(metadata.e2eEncrypted)]")
 
         if metadata.e2eEncrypted {
-            #if !EXTENSION_FILE_PROVIDER_EXTENSION
+            #if !EXTENSION_FILE_PROVIDER_EXTENSION && !EXTENSION_WIDGET
             NCNetworkingE2EE.shared.upload(metadata: metadata, start: start) { errorCode, errorDescription in
                 DispatchQueue.main.async {
                     completion(errorCode, errorDescription)

+ 16 - 8
iOSClient/Networking/NCNetworkingProcessUpload.swift

@@ -28,7 +28,6 @@ import Queuer
 
 class NCNetworkingProcessUpload: NSObject {
 
-    let appDelegate = UIApplication.shared.delegate as! AppDelegate
     var timerProcess: Timer?
 
     let maxConcurrentOperationUpload = 5
@@ -54,11 +53,12 @@ class NCNetworkingProcessUpload: NSObject {
     }
 
     @objc private func process() {
-        guard !appDelegate.account.isEmpty else { return }
+        guard let account = NCManageDatabase.shared.getActiveAccount() else {
+            return
+        }
 
         stopTimer()
 
-        let applicationState = UIApplication.shared.applicationState
         var counterUpload: Int = 0
         let sessionSelectors = [NCGlobal.shared.selectorUploadFileNODelete, NCGlobal.shared.selectorUploadFile, NCGlobal.shared.selectorUploadAutoUpload, NCGlobal.shared.selectorUploadAutoUploadAll]
         let metadatasUpload = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "status == %d OR status == %d", NCGlobal.shared.metadataStatusInUpload, NCGlobal.shared.metadataStatusUploading))
@@ -84,8 +84,8 @@ class NCNetworkingProcessUpload: NSObject {
                     for metadata in metadatas {
 
                         // Different account
-                        if self.appDelegate.account != metadata.account {
-                            NCCommunicationCommon.shared.writeLog("Process auto upload skipped file: \(metadata.serverUrl)/\(metadata.fileNameView) on account: \(metadata.account), because the actual account is \(self.appDelegate.account).")
+                        if account.account != metadata.account {
+                            NCCommunicationCommon.shared.writeLog("Process auto upload skipped file: \(metadata.serverUrl)/\(metadata.fileNameView) on account: \(metadata.account), because the actual account is \(account.account).")
                             continue
                         }
 
@@ -114,7 +114,11 @@ class NCNetworkingProcessUpload: NSObject {
                                 NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
                             }
                             for metadata in metadatas {
-                                if (metadata.e2eEncrypted || metadata.chunk) && applicationState != .active {  continue }
+                                #if !EXTENSION
+                                if (metadata.e2eEncrypted || metadata.chunk) && UIApplication.shared.applicationState != .active {  continue }
+                                #else
+                                if (metadata.e2eEncrypted || metadata.chunk) { continue }
+                                #endif
                                 let isWiFi = NCNetworking.shared.networkReachability == NCCommunicationCommon.typeReachability.reachableEthernetOrWiFi
                                 if metadata.session == NCNetworking.shared.sessionIdentifierBackgroundWWan && !isWiFi { continue }
                                 if let metadata = NCManageDatabase.shared.setMetadataStatus(ocId: metadata.ocId, status: NCGlobal.shared.metadataStatusInUpload) {
@@ -142,18 +146,21 @@ class NCNetworkingProcessUpload: NSObject {
             }
              
             // verify delete Asset Local Identifiers in auto upload (DELETE Photos album)
+            #if !EXTENSION
             DispatchQueue.main.async {
-                if (counterUpload == 0 && !self.appDelegate.isPasscodePresented()) {
-                    self.deleteAssetLocalIdentifiers(account: self.appDelegate.account) {
+                if (counterUpload == 0 && !(UIApplication.shared.delegate as! AppDelegate).isPasscodePresented()) {
+                    self.deleteAssetLocalIdentifiers(account: account.account) {
                         self.startTimer()
                     }
                 } else {
                     self.startTimer()
                 }
             }
+            #endif
         })
     }
 
+    #if !EXTENSION
     private func deleteAssetLocalIdentifiers(account: String, completition: @escaping () -> Void) {
 
         if UIApplication.shared.applicationState != .active {
@@ -181,6 +188,7 @@ class NCNetworkingProcessUpload: NSObject {
             }
         })
     }
+    #endif
 
     // MARK: -
 

+ 7 - 3
iOSClient/Utility/NCAskAuthorization.swift

@@ -40,7 +40,9 @@ class NCAskAuthorization: NSObject {
         case AVAudioSession.RecordPermission.denied:
             let alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_err_permission_microphone_", comment: ""), preferredStyle: .alert)
             alert.addAction(UIAlertAction(title: NSLocalizedString("_open_settings_", comment: ""), style: .default, handler: { _ in
+                #if !EXTENSION
                 UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
+                #endif
                 completion(false)
             }))
             alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in
@@ -76,7 +78,9 @@ class NCAskAuthorization: NSObject {
         case PHAuthorizationStatus.denied, PHAuthorizationStatus.limited, PHAuthorizationStatus.restricted:
             let alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_err_permission_photolibrary_", comment: ""), preferredStyle: .alert)
             alert.addAction(UIAlertAction(title: NSLocalizedString("_open_settings_", comment: ""), style: .default, handler: { _ in
+                #if !EXTENSION
                 UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
+                #endif
                 completion(false)
             }))
             alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in
@@ -90,9 +94,9 @@ class NCAskAuthorization: NSObject {
             isRequesting = true
             PHPhotoLibrary.requestAuthorization { allowed in
                 self.isRequesting = false
-                DispatchQueue.main.async {
-                    (UIApplication.shared.delegate as? AppDelegate)?.hidePrivacyProtectionWindow()
-                }
+                #if !EXTENSION
+                DispatchQueue.main.async { (UIApplication.shared.delegate as? AppDelegate)?.hidePrivacyProtectionWindow() }
+                #endif
                 DispatchQueue.main.async {
                     if allowed == PHAuthorizationStatus.authorized {
                         completion(true)