Ver código fonte

fix

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 2 anos atrás
pai
commit
31d8d69f11

+ 6 - 6
Widget/Lockscreen/LockscreenData.swift

@@ -76,14 +76,14 @@ func getLockscreenDataEntry(configuration: AccountIntent?, isPreview: Bool, fami
     if #available(iOSApplicationExtension 16.0, *) {
         if family == .accessoryCircular {
             NextcloudKit.shared.getUserProfile(options: options) { _, userProfile, _, error in
-                if error == .success, let userProfile = userProfile, let account = NCManageDatabase.shared.setAccountUserProfile(userProfile) {
-                    if account.quotaRelative > 0 {
-                        quotaRelative = Float(account.quotaRelative) / 100
+                if error == .success, let userProfile = userProfile {
+                    if userProfile.quotaRelative > 0 {
+                        quotaRelative = Float(userProfile.quotaRelative) / 100
                     }
-                    let quotaUsed: String = CCUtility.transformedSize(account.quotaUsed)
+                    let quotaUsed: String = CCUtility.transformedSize(userProfile.quotaUsed)
                     var quotaTotal: String = ""
 
-                    switch account.quotaTotal {
+                    switch userProfile.quotaTotal {
                     case -1:
                         quotaTotal = ""
                     case -2:
@@ -91,7 +91,7 @@ func getLockscreenDataEntry(configuration: AccountIntent?, isPreview: Bool, fami
                     case -3:
                         quotaTotal = ""
                     default:
-                        quotaTotal = CCUtility.transformedSize(account.quotaTotal)
+                        quotaTotal = CCUtility.transformedSize(userProfile.quotaTotal)
                     }
                     completion(LockscreenData(date: Date(), isPlaceholder: false, activity: "", link: URL(string: "https://")!, quotaRelative: quotaRelative, quotaUsed: quotaUsed, quotaTotal: quotaTotal, error: false))
                 } else {

+ 29 - 71
iOSClient/Data/NCManageDatabase+Account.swift

@@ -79,7 +79,6 @@ extension NCManageDatabase {
         do {
             try realm.write {
                 let result = realm.objects(tableAccount.self).filter("account == %@", account)
-
                 realm.delete(result)
             }
         } catch let error {
@@ -285,88 +284,47 @@ extension NCManageDatabase {
         }
     }
 
-    @objc func setAccountUserProfile(_ userProfile: NKUserProfile) -> tableAccount? {
+    @objc func setAccountUserProfile(account: String, userProfile: NKUserProfile) -> tableAccount? {
 
         let realm = try! Realm()
 
-        var returnAccount = tableAccount()
-
         do {
-            guard let activeAccount = self.getActiveAccount() else {
-                return nil
-            }
-
             try realm.write {
-
-                guard let result = realm.objects(tableAccount.self).filter("account == %@", activeAccount.account).first else {
-                    return
+                if let result = realm.objects(tableAccount.self).filter("account == %@", account).first {
+                    result.address = userProfile.address
+                    result.backend = userProfile.backend
+                    result.backendCapabilitiesSetDisplayName = userProfile.backendCapabilitiesSetDisplayName
+                    result.backendCapabilitiesSetPassword = userProfile.backendCapabilitiesSetPassword
+                    result.displayName = userProfile.displayName
+                    result.email = userProfile.email
+                    result.enabled = userProfile.enabled
+                    result.groups = userProfile.groups.joined(separator: ",")
+                    result.language = userProfile.language
+                    result.lastLogin = userProfile.lastLogin
+                    result.locale = userProfile.locale
+                    result.organisation = userProfile.organisation
+                    result.phone = userProfile.phone
+                    result.quota = userProfile.quota
+                    result.quotaFree = userProfile.quotaFree
+                    result.quotaRelative = userProfile.quotaRelative
+                    result.quotaTotal = userProfile.quotaTotal
+                    result.quotaUsed = userProfile.quotaUsed
+                    result.storageLocation = userProfile.storageLocation
+                    result.subadmin = userProfile.subadmin.joined(separator: ",")
+                    result.twitter = userProfile.twitter
+                    result.userId = userProfile.userId
+                    result.website = userProfile.website
                 }
-
-                result.address = userProfile.address
-                result.backend = userProfile.backend
-                result.backendCapabilitiesSetDisplayName = userProfile.backendCapabilitiesSetDisplayName
-                result.backendCapabilitiesSetPassword = userProfile.backendCapabilitiesSetPassword
-                result.displayName = userProfile.displayName
-                result.email = userProfile.email
-                result.enabled = userProfile.enabled
-                result.groups = userProfile.groups.joined(separator: ",")
-                result.language = userProfile.language
-                result.lastLogin = userProfile.lastLogin
-                result.locale = userProfile.locale
-                result.organisation = userProfile.organisation
-                result.phone = userProfile.phone
-                result.quota = userProfile.quota
-                result.quotaFree = userProfile.quotaFree
-                result.quotaRelative = userProfile.quotaRelative
-                result.quotaTotal = userProfile.quotaTotal
-                result.quotaUsed = userProfile.quotaUsed
-                result.storageLocation = userProfile.storageLocation
-                result.subadmin = userProfile.subadmin.joined(separator: ",")
-                result.twitter = userProfile.twitter
-                result.userId = userProfile.userId
-                result.website = userProfile.website
-
-                returnAccount = result
             }
         } catch let error {
             NKCommon.shared.writeLog("Could not write to database: \(error)")
         }
 
-        return tableAccount.init(value: returnAccount)
-    }
-
-    @objc func setAccountUserProfileHC(businessSize: String, businessType: String, city: String, organisation: String, country: String, role: String, zip: String) -> tableAccount? {
-
-        let realm = try! Realm()
-
-        var returnAccount = tableAccount()
-
-        do {
-            guard let activeAccount = self.getActiveAccount() else {
-                return nil
-            }
-
-            try realm.write {
-
-                guard let result = realm.objects(tableAccount.self).filter("account == %@", activeAccount.account).first else {
-                    return
-                }
-
-                result.businessSize = businessSize
-                result.businessType = businessType
-                result.city = city
-                result.organisation =  organisation
-                result.country = country
-                result.role = role
-                result.zip = zip
-
-                returnAccount = result
-            }
-        } catch let error {
-            NKCommon.shared.writeLog("Could not write to database: \(error)")
+        if let result = realm.objects(tableAccount.self).filter("account == %@", account).first {
+            return tableAccount.init(value: result)
+        } else {
+            return nil
         }
-
-        return tableAccount.init(value: returnAccount)
     }
 
     @objc func setAccountMediaPath(_ path: String, account: String) {

+ 2 - 2
iOSClient/Networking/NCService.swift

@@ -109,7 +109,7 @@ class NCService: NSObject {
         let options = NKRequestOptions(queue: NKCommon.shared.backgroundQueue)
 
         NextcloudKit.shared.getUserProfile(options: options) { account, userProfile, data, error in
-            guard error == .success, account == self.appDelegate.account else {
+            guard error == .success, let userProfile = userProfile else {
                 
                 // Ops the server has Unauthorized
                 NKCommon.shared.writeLog("[ERROR] The server has response with Unauthorized \(error.errorCode)")
@@ -127,7 +127,7 @@ class NCService: NSObject {
             }
 
             // Update User (+ userProfile.id) & active account & account network
-            guard let tableAccount = NCManageDatabase.shared.setAccountUserProfile(userProfile!) else {
+            guard let tableAccount = NCManageDatabase.shared.setAccountUserProfile(account: account, userProfile: userProfile) else {
                 let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "Internal error : account not found on DB")
                 NCContentPresenter.shared.showError(error: error, priority: .max)
                 return