mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
99 lines
3.0 KiB
Swift
99 lines
3.0 KiB
Swift
//
|
|
// store.swift
|
|
// Kit
|
|
//
|
|
// Created by Serhiy Mytrovtsiy on 10/04/2020.
|
|
// Using Swift 5.0.
|
|
// Running on macOS 10.15.
|
|
//
|
|
// Copyright © 2020 Serhiy Mytrovtsiy. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
|
|
public class Store {
|
|
public static let shared = Store()
|
|
private let defaults = UserDefaults.standard
|
|
|
|
public init() {}
|
|
|
|
public func exist(key: String) -> Bool {
|
|
return self.defaults.object(forKey: key) == nil ? false : true
|
|
}
|
|
|
|
public func remove(_ key: String) {
|
|
self.defaults.removeObject(forKey: key)
|
|
}
|
|
|
|
public func bool(key: String, defaultValue value: Bool) -> Bool {
|
|
return !self.exist(key: key) ? value : defaults.bool(forKey: key)
|
|
}
|
|
|
|
public func string(key: String, defaultValue value: String) -> String {
|
|
return (!self.exist(key: key) ? value : defaults.string(forKey: key))!
|
|
}
|
|
|
|
public func int(key: String, defaultValue value: Int) -> Int {
|
|
return (!self.exist(key: key) ? value : defaults.integer(forKey: key))
|
|
}
|
|
|
|
public func array(key: String, defaultValue value: [Any]) -> [Any] {
|
|
return (!self.exist(key: key) ? value : defaults.array(forKey: key)!)
|
|
}
|
|
|
|
public func data(key: String) -> Data? {
|
|
return defaults.data(forKey: key)
|
|
}
|
|
|
|
public func set(key: String, value: Bool) {
|
|
self.defaults.set(value, forKey: key)
|
|
}
|
|
|
|
public func set(key: String, value: String) {
|
|
self.defaults.set(value, forKey: key)
|
|
}
|
|
|
|
public func set(key: String, value: Int) {
|
|
self.defaults.set(value, forKey: key)
|
|
}
|
|
|
|
public func set(key: String, value: Data) {
|
|
self.defaults.set(value, forKey: key)
|
|
}
|
|
|
|
public func set(key: String, value: [Any]) {
|
|
self.defaults.set(value, forKey: key)
|
|
}
|
|
|
|
public func reset() {
|
|
self.defaults.dictionaryRepresentation().keys.forEach { key in
|
|
self.defaults.removeObject(forKey: key)
|
|
}
|
|
}
|
|
|
|
public func export(to url: URL) {
|
|
guard let id = Bundle.main.bundleIdentifier,
|
|
var dictionary = self.defaults.persistentDomain(forName: id) else { return }
|
|
dictionary.removeValue(forKey: "telemetry_id")
|
|
dictionary.removeValue(forKey: "access_token")
|
|
dictionary.removeValue(forKey: "refresh_token")
|
|
NSDictionary(dictionary: dictionary).write(to: url, atomically: true)
|
|
}
|
|
|
|
public func `import`(from url: URL) {
|
|
guard let id = Bundle.main.bundleIdentifier, let dict = NSDictionary(contentsOf: url) as? [String: Any] else { return }
|
|
|
|
var importedDict = dict
|
|
let keysToPreserve = ["telemetry_id", "access_token", "refresh_token"]
|
|
|
|
for key in keysToPreserve {
|
|
if let existingValue = self.defaults.string(forKey: key) {
|
|
importedDict[key] = existingValue
|
|
}
|
|
}
|
|
|
|
self.defaults.setPersistentDomain(importedDict, forName: id)
|
|
restartApp(self)
|
|
}
|
|
}
|