2022-07-03 06:52:41 +00:00
|
|
|
//
|
|
|
|
// LocalData.swift
|
|
|
|
// MastoSearch
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 12/15/21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2022-07-03 18:58:59 +00:00
|
|
|
public class LocalData {
|
2022-07-03 06:52:41 +00:00
|
|
|
private init() {}
|
|
|
|
|
|
|
|
private static let encoder = PropertyListEncoder()
|
|
|
|
private static let decoder = PropertyListDecoder()
|
|
|
|
|
2022-07-03 18:58:59 +00:00
|
|
|
public static var account: AccountInfo? {
|
2022-07-03 06:52:41 +00:00
|
|
|
get {
|
|
|
|
guard let data = UserDefaults.standard.data(forKey: "account") else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return try? decoder.decode(AccountInfo.self, from: data)
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
guard let newValue = newValue else {
|
|
|
|
UserDefaults.standard.set(nil, forKey: "account")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let data = try! encoder.encode(newValue)
|
|
|
|
UserDefaults.standard.set(data, forKey: "account")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-03 18:58:59 +00:00
|
|
|
public struct AccountInfo: Codable {
|
|
|
|
public let instanceURL: URL
|
|
|
|
public var clientID: String!
|
|
|
|
public var clientSecret: String!
|
|
|
|
public var accessToken: String!
|
|
|
|
|
|
|
|
public init(instanceURL: URL, clientID: String? = nil, clientSecret: String? = nil, accessToken: String? = nil) {
|
|
|
|
self.instanceURL = instanceURL
|
|
|
|
self.clientID = clientID
|
|
|
|
self.clientSecret = clientSecret
|
|
|
|
self.accessToken = accessToken
|
|
|
|
}
|
2022-07-03 06:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|