Skip to content

Commit

Permalink
fix: Account json migration
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Weidmann <[email protected]>
  • Loading branch information
PhilippeWeidmann committed Jul 11, 2024
1 parent e522c8d commit 301f488
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions kDriveCore/Data/Cache/AccountManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,20 +352,38 @@ public class AccountManager: RefreshTokenDelegate, AccountManageable {
}

public func loadAccounts() -> [Account] {
var accounts = [Account]()
if let groupDirectoryURL = FileManager.default
guard let groupDirectoryURL = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: AccountManager.appGroup)?
.appendingPathComponent("preferences", isDirectory: true) {
.appendingPathComponent("preferences", isDirectory: true) else { return [] }
let data: Data
do {
data = try Data(contentsOf: groupDirectoryURL.appendingPathComponent("accounts.json"))
} catch {
DDLogError("Error loading accounts \(error)")
return []
}

do {
let decoder = JSONDecoder()
let savedAccounts = try decoder.decode([Account].self, from: data)

return savedAccounts
} catch is DecodingError {
do {
let data = try Data(contentsOf: groupDirectoryURL.appendingPathComponent("accounts.json"))
let savedAccounts = try decoder.decode([Account].self, from: data)
accounts = savedAccounts
let migrationDecoder = JSONDecoder()
migrationDecoder.keyDecodingStrategy = .convertFromSnakeCase

let savedAccounts = try migrationDecoder.decode([Account].self, from: data)

return savedAccounts
} catch {
DDLogError("Error loading accounts \(error)")
DDLogError("Error migrating accounts \(error)")
return []
}
} catch {
DDLogError("Error loading accounts \(error)")
return []
}
return accounts
}

public func saveAccounts() {
Expand Down

0 comments on commit 301f488

Please sign in to comment.