浏览代码

coding

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 2 年之前
父节点
当前提交
f59d3ff23a
共有 1 个文件被更改,包括 32 次插入37 次删除
  1. 32 37
      iOSClient/Networking/NCNetworking.swift

+ 32 - 37
iOSClient/Networking/NCNetworking.swift

@@ -1563,66 +1563,61 @@ import Photos
 
     public class NKAccountFile: NSObject {
 
-        struct AccountData: Codable {
+        struct Account: Codable {
             let url: String
             let user: String
             let alias: String?
             let avatar: String?
         }
 
-        struct Accounts: Codable {
-            let metadata: [String: [AccountData]]?
-        }
-
-        let encoder = JSONEncoder()
-
-        func decoder(at url: URL) -> Accounts? {
-            let decoder = JSONDecoder()
-
-            do {
-                let data = try Data(contentsOf: url)
-                data.printJson()
-                let json = try decoder.decode(Accounts.self, from: data)
-                return json
-            } catch let error {
-                return nil
-            }
-        }
-
-        func getShareAccount(at url: URL) {
-
+        struct Apps: Codable {
+            let apps: [String: [Account]]?
         }
 
         func putShareAccounts(at url: URL, app: String, items: [NKDataAccountFile]) -> Error? {
 
-            var itemAccount: [AccountData] = []
-            var apps: [String : [AccountData]] = [:]
+            var apps: [String : [Account]] = [:]
+            var accounts: [Account] = []
 
             for item in items {
-                let account = AccountData(url: item.url, user: item.user, alias: item.alias, avatar: item.avatar)
-                itemAccount.append(account)
-            }
-            apps[app] = itemAccount
-
-            if let accountsDecoder = decoder(at: url), let metadata = accountsDecoder.metadata {
-                let otherApps = metadata.filter({ $0.key != app })
-                apps.merge(otherApps){(current, _) in current}
+                let account = Account(url: item.url, user: item.user, alias: item.alias, avatar: item.avatar)
+                accounts.append(account)
             }
-            let accounts = Accounts(metadata:apps)
+            apps[app] = accounts
 
+            // Decode
+            do {
+                let data = try Data(contentsOf: url)
+                let json = try JSONDecoder().decode(Apps.self, from: data)
+                if let appsDecoder = json.apps {
+                    let otherApps = appsDecoder.filter({ $0.key != app })
+                    apps.merge(otherApps){(current, _) in current}
+                }
+            } catch { }
 
-            /*
+            // Encode
             do {
-                let data = try encoder.encode(accounts)
+                let data = try JSONEncoder().encode(Apps(apps: apps))
                 try data.write(to: url)
                 data.printJson()
             } catch let error {
                 return error
             }
-            */
-
             return nil
         }
+
+        func getShareAccount(at url: URL) {
+
+            do {
+                let data = try Data(contentsOf: url)
+                let json = try JSONDecoder().decode(Apps.self, from: data)
+                if let appsDecoder = json.apps {
+                    print("")
+                }
+            } catch { }
+
+
+        }
     }