IceCubes/IceCubesApp/App/AppAccounts/AppAccount.swift
2022-12-30 08:36:22 +01:00

56 lines
1.2 KiB
Swift

import SwiftUI
import Timeline
import Network
import KeychainSwift
import Models
struct AppAccount: Codable, Identifiable {
let server: String
let oauthToken: OauthToken?
var id: String {
key
}
var key: String {
if let oauthToken {
return "\(server):\(oauthToken.createdAt)"
} else {
return "\(server):anonymous:\(Date().timeIntervalSince1970)"
}
}
func save() throws {
let encoder = JSONEncoder()
let data = try encoder.encode(self)
let keychain = KeychainSwift()
keychain.set(data, forKey: key)
}
func delete() {
KeychainSwift().delete(key)
}
static func retrieveAll() throws -> [AppAccount] {
let keychain = KeychainSwift()
let decoder = JSONDecoder()
let keys = keychain.allKeys
var accounts: [AppAccount] = []
for key in keys {
if let data = keychain.getData(key) {
let account = try decoder.decode(AppAccount.self, from: data)
accounts.append(account)
}
}
return accounts
}
static func deleteAll() {
let keychain = KeychainSwift()
let keys = keychain.allKeys
for key in keys {
keychain.delete(key)
}
}
}