31 lines
1.1 KiB
Swift
31 lines
1.1 KiB
Swift
|
|
import Foundation
|
||
|
|
|
||
|
|
// Bridges NASConnection save/load with Keychain for password and UserDefaults for metadata.
|
||
|
|
enum SavedConnections {
|
||
|
|
private static let key = "savedConnection"
|
||
|
|
|
||
|
|
static func save(_ connection: NASConnection) {
|
||
|
|
KeychainStore.savePassword(connection.password, for: connection.id)
|
||
|
|
if let data = try? JSONEncoder().encode(connection) {
|
||
|
|
UserDefaults.standard.set(data, forKey: key)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static func load() -> NASConnection? {
|
||
|
|
guard let data = UserDefaults.standard.data(forKey: key),
|
||
|
|
var connection = try? JSONDecoder().decode(NASConnection.self, from: data) else {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
connection.password = KeychainStore.loadPassword(for: connection.id) ?? ""
|
||
|
|
return connection
|
||
|
|
}
|
||
|
|
|
||
|
|
static func delete() {
|
||
|
|
if let data = UserDefaults.standard.data(forKey: key),
|
||
|
|
let connection = try? JSONDecoder().decode(NASConnection.self, from: data) {
|
||
|
|
KeychainStore.deletePassword(for: connection.id)
|
||
|
|
}
|
||
|
|
UserDefaults.standard.removeObject(forKey: key)
|
||
|
|
}
|
||
|
|
}
|