frenzy-ios/Reader/LocalData.swift

48 lines
1.1 KiB
Swift

//
// LocalData.swift
// Reader
//
// Created by Shadowfacts on 11/25/21.
//
import Foundation
struct LocalData {
private init() {}
private static let encoder = JSONEncoder()
private static let decoder = JSONDecoder()
static var account: Account? {
get {
guard let data = UserDefaults.standard.data(forKey: "account") else {
return nil
}
return try? decoder.decode(Account.self, from: data)
}
set {
let data = try! encoder.encode(newValue)
UserDefaults.standard.set(data, forKey: "account")
}
}
struct Account: Codable {
let id: UUID
let instanceURL: URL
let clientID: String
let clientSecret: String
let accessToken: String
// todo: refresh tokens
init(instanceURL: URL, clientID: String, clientSecret: String, accessToken: String) {
self.id = UUID()
self.instanceURL = instanceURL
self.clientID = clientID
self.clientSecret = clientSecret
self.accessToken = accessToken
}
}
}