tokenary/Safari Shared/ExtensionBridge.swift
2023-11-07 20:18:29 +03:00

63 lines
1.6 KiB
Swift

// Copyright © 2021 Tokenary. All rights reserved.
import Foundation
struct ExtensionBridge {
private static let defaults = UserDefaults(suiteName: "group.io.tokenary")
private static func key(id: Int) -> String {
return String(id)
}
static var defaultInfuraKeys: [String]? {
get {
return defaults?.array(forKey: "defaultInfuraKeys") as? [String]
}
set {
defaults?.set(newValue, forKey: "defaultInfuraKeys")
}
}
private static var initiatedRequests: Set<Int> {
get {
Set(defaults?.array(forKey: "initiatedRequests") as? [Int] ?? [])
}
set {
defaults?.set(Array(newValue), forKey: "initiatedRequests")
}
}
static func makeRequest(id: Int) {
initiatedRequests.insert(id)
}
static func hasRequest(id: Int) -> Bool {
if initiatedRequests.contains(id) {
removeRequest(id: id)
return true
} else {
return false
}
}
static func respond(response: ResponseToExtension) {
defaults?.set(response.json, forKey: key(id: response.id))
}
static func removeRequest(id: Int) {
initiatedRequests.remove(id)
}
static func removeResponse(id: Int) {
let key = key(id: id)
defaults?.removeObject(forKey: key)
}
static func getResponse(id: Int) -> [String: AnyHashable]? {
let key = key(id: id)
return defaults?.value(forKey: key) as? [String: AnyHashable]
}
}