marinofaggiana 4 years ago
parent
commit
b4929a4541
1 changed files with 86 additions and 98 deletions
  1. 86 98
      iOSClient/Networking/NCService.swift

+ 86 - 98
iOSClient/Networking/NCService.swift

@@ -56,64 +56,59 @@ class NCService: NSObject {
         NCCommunication.shared.getUserProfile() { (account, userProfile, errorCode, errorDescription) in
                
             if errorCode == 0 && account == self.appDelegate.activeAccount {
+                                    
+                // Update User (+ userProfile.id) & active account & account network
+                guard let tableAccount = NCManageDatabase.sharedInstance.setAccountUserProfile(userProfile!) else {
+                    NCContentPresenter.shared.messageNotification("Account", description: "Internal error : account not found on DB",  delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
+                        
+                    return
+                }
                 
-                DispatchQueue.global().async {
-                    
-                    // Update User (+ userProfile.id) & active account & account network
-                    guard let tableAccount = NCManageDatabase.sharedInstance.setAccountUserProfile(userProfile!) else {
-                        DispatchQueue.main.async {
-                            NCContentPresenter.shared.messageNotification("Account", description: "Internal error : account not found on DB",  delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
-                        }
-                        return
-                    }
-                
-                    let user = tableAccount.user
-                    let url = tableAccount.url
+                let user = tableAccount.user
+                let url = tableAccount.url
                 
-                    self.appDelegate.settingActiveAccount(tableAccount.account, activeUrl: tableAccount.url, activeUser: tableAccount.user, activeUserID: tableAccount.userID, activePassword: CCUtility.getPassword(tableAccount.account))
+                self.appDelegate.settingActiveAccount(tableAccount.account, activeUrl: tableAccount.url, activeUser: tableAccount.user, activeUserID: tableAccount.userID, activePassword: CCUtility.getPassword(tableAccount.account))
                 
-                    // Synchronize Offline ---
-                    let directories = NCManageDatabase.sharedInstance.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND offline == true", tableAccount.account), sorted: "serverUrl", ascending: true)
-                    if (directories != nil) {
-                        for directory: tableDirectory in directories! {
-                            CCSynchronize.shared()?.readFolder(directory.serverUrl, selector: selectorReadFolderWithDownload, account: tableAccount.account)
-                        }
+                // Synchronize Offline ---
+                let directories = NCManageDatabase.sharedInstance.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND offline == true", tableAccount.account), sorted: "serverUrl", ascending: true)
+                if (directories != nil) {
+                    for directory: tableDirectory in directories! {
+                        CCSynchronize.shared()?.readFolder(directory.serverUrl, selector: selectorReadFolderWithDownload, account: tableAccount.account)
                     }
+                }
                 
-                    let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@ AND offline == true", tableAccount.account), sorted: "fileName", ascending: true)
-                    if (files != nil) {
-                        for file: tableLocalFile in files! {
-                            guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", file.ocId)) else {
-                                continue
-                            }
-                            CCSynchronize.shared()?.readFile(metadata.ocId, fileName: metadata.fileName, serverUrl: metadata.serverUrl, selector: selectorReadFileWithDownload, account: tableAccount.account)
+                let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@ AND offline == true", tableAccount.account), sorted: "fileName", ascending: true)
+                if (files != nil) {
+                    for file: tableLocalFile in files! {
+                        guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", file.ocId)) else {
+                            continue
                         }
+                        CCSynchronize.shared()?.readFile(metadata.ocId, fileName: metadata.fileName, serverUrl: metadata.serverUrl, selector: selectorReadFileWithDownload, account: tableAccount.account)
                     }
-                    // ---
+                }
+                // ---
                         
-                    let avatarUrl = "\(self.appDelegate.activeUrl!)/index.php/avatar/\(self.appDelegate.activeUser!)/\(k_avatar_size)".addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
-                    let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(user, activeUrl: url) + "-" + self.appDelegate.activeUser + ".png"
+                let avatarUrl = "\(self.appDelegate.activeUrl!)/index.php/avatar/\(self.appDelegate.activeUser!)/\(k_avatar_size)".addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
+                let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(user, activeUrl: url) + "-" + self.appDelegate.activeUser + ".png"
                         
-                    NCCommunication.shared.downloadContent(serverUrl: avatarUrl) { (account, data, errorCode, errorMessage) in
-                        if errorCode == 0 {
-                            DispatchQueue.global().async {
-                                if let image = UIImage(data: data!) {
-                                    try? FileManager.default.removeItem(atPath: fileNamePath)
-                                    if let data = image.pngData() {
-                                        try? data.write(to: URL(fileURLWithPath: fileNamePath))
-                                    }
+                NCCommunication.shared.downloadContent(serverUrl: avatarUrl) { (account, data, errorCode, errorMessage) in
+                    if errorCode == 0 {
+                        DispatchQueue.global().async {
+                            if let image = UIImage(data: data!) {
+                                try? FileManager.default.removeItem(atPath: fileNamePath)
+                                if let data = image.pngData() {
+                                    try? data.write(to: URL(fileURLWithPath: fileNamePath))
                                 }
                             }
                         }
                     }
+                }
                       
-                    DispatchQueue.main.async {
-                        NotificationCenter.default.post(name: NSNotification.Name(rawValue: k_notificationCenter_changeUserProfile), object: nil)
-                    }
+                NotificationCenter.default.post(name: NSNotification.Name(rawValue: k_notificationCenter_changeUserProfile), object: nil)
+                    
                 
-                    // Get Capabilities
-                    self.requestServerCapabilities()
-                }
+                // Get Capabilities
+                self.requestServerCapabilities()
                 
             } else {
                 
@@ -151,67 +146,60 @@ class NCService: NSObject {
             
             if errorCode == 0 && data != nil {
                 
-                DispatchQueue.global().async {
-                    
-                    NCManageDatabase.sharedInstance.addCapabilitiesJSon(data!, account: account)
-                
-                    // Setup communication
-                    self.appDelegate.settingSetupCommunicationCapabilities(account)
-                
-                    // Theming
-                    self.appDelegate.settingThemingColorBrand()
-                
-                    // File Sharing
-                    let isFilesSharingEnabled = NCManageDatabase.sharedInstance.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesFileSharingApiEnabled, exists: false)
-                    if (isFilesSharingEnabled && self.appDelegate.activeMain != nil) {
-                    
-                        OCNetworking.sharedManager()?.readShare(withAccount: account, completion: { (account, items, message, errorCode) in
-                            if errorCode == 0 && account == self.appDelegate.activeAccount {
-                                
-                                DispatchQueue.global().async {
-                                    let itemsOCSharedDto = items as! [OCSharedDto]
-                                    NCManageDatabase.sharedInstance.deleteTableShare(account: account!)
-                                    self.appDelegate.shares = NCManageDatabase.sharedInstance.addShare(account: account!, activeUrl: self.appDelegate.activeUrl, items: itemsOCSharedDto)
-                                }
-                            } else {
-                                NCContentPresenter.shared.messageNotification("_share_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                            }
-                        })
-                    }
+                NCManageDatabase.sharedInstance.addCapabilitiesJSon(data!, account: account)
+            
+                // Setup communication
+                self.appDelegate.settingSetupCommunicationCapabilities(account)
+            
+                // Theming
+                self.appDelegate.settingThemingColorBrand()
+            
+                // File Sharing
+                let isFilesSharingEnabled = NCManageDatabase.sharedInstance.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesFileSharingApiEnabled, exists: false)
+                if (isFilesSharingEnabled && self.appDelegate.activeMain != nil) {
                 
-                    // NCTextObtainEditorDetails
-                    let serverVersionMajor = NCManageDatabase.sharedInstance.getCapabilitiesServerInt(account: account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
-                    if serverVersionMajor >= k_nextcloud_version_18_0 {
-                        NCCommunication.shared.NCTextObtainEditorDetails() { (account, editors, creators, errorCode, errorMessage) in
-                            if errorCode == 0 && account == self.appDelegate.activeAccount {
-                                DispatchQueue.main.async {
-                                    NCManageDatabase.sharedInstance.addDirectEditing(account: account, editors: editors, creators: creators)
-                                }
-                            }
+                    OCNetworking.sharedManager()?.readShare(withAccount: account, completion: { (account, items, message, errorCode) in
+                        if errorCode == 0 && account == self.appDelegate.activeAccount {
+                            
+                            let itemsOCSharedDto = items as! [OCSharedDto]
+                            NCManageDatabase.sharedInstance.deleteTableShare(account: account!)
+                            self.appDelegate.shares = NCManageDatabase.sharedInstance.addShare(account: account!, activeUrl: self.appDelegate.activeUrl, items: itemsOCSharedDto)
+                            
+                        } else {
+                            
+                            NCContentPresenter.shared.messageNotification("_share_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
                         }
-                    }
-                    
-                    let isExternalSitesServerEnabled = NCManageDatabase.sharedInstance.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesExternalSitesExists, exists: true)
-                    if (isExternalSitesServerEnabled) {
-                        NCCommunication.shared.getExternalSite() { (account, externalSites, errorCode, errorDescription) in
-                            if errorCode == 0 && account == self.appDelegate.activeAccount {
-                                DispatchQueue.main.async {
-                                    NCManageDatabase.sharedInstance.deleteExternalSites(account: account)
-                                    for externalSite in externalSites {
-                                        NCManageDatabase.sharedInstance.addExternalSites(externalSite, account: account)
-                                    }
-                                }
-                            }
+                    })
+                }
+            
+                // NCTextObtainEditorDetails
+                let serverVersionMajor = NCManageDatabase.sharedInstance.getCapabilitiesServerInt(account: account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
+                if serverVersionMajor >= k_nextcloud_version_18_0 {
+                    NCCommunication.shared.NCTextObtainEditorDetails() { (account, editors, creators, errorCode, errorMessage) in
+                        if errorCode == 0 && account == self.appDelegate.activeAccount {
+                            NCManageDatabase.sharedInstance.addDirectEditing(account: account, editors: editors, creators: creators)
                         }
-                    } else {
-                        NCManageDatabase.sharedInstance.deleteExternalSites(account: account)
                     }
+                }
                 
-                    // Handwerkcloud
-                    let isHandwerkcloudEnabled = NCManageDatabase.sharedInstance.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesHWCEnabled, exists: false)
-                    if (isHandwerkcloudEnabled) {
-                        self.requestHC()
+                let isExternalSitesServerEnabled = NCManageDatabase.sharedInstance.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesExternalSitesExists, exists: true)
+                if (isExternalSitesServerEnabled) {
+                    NCCommunication.shared.getExternalSite() { (account, externalSites, errorCode, errorDescription) in
+                        if errorCode == 0 && account == self.appDelegate.activeAccount {
+                            NCManageDatabase.sharedInstance.deleteExternalSites(account: account)
+                            for externalSite in externalSites {
+                                NCManageDatabase.sharedInstance.addExternalSites(externalSite, account: account)
+                            }
+                        }
                     }
+                } else {
+                    NCManageDatabase.sharedInstance.deleteExternalSites(account: account)
+                }
+            
+                // Handwerkcloud
+                let isHandwerkcloudEnabled = NCManageDatabase.sharedInstance.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesHWCEnabled, exists: false)
+                if (isHandwerkcloudEnabled) {
+                    self.requestHC()
                 }
                 
             } else if errorCode != 0 {