Make SettingsStore functions non-static, re-implement them as

subscripts and make settingsStore an environmentObject

Fixes: https://github.com/maxgoedjen/secretive/pull/536#discussion_r1508446459
This commit is contained in:
Paul Heidekrüger 2024-04-05 09:24:35 +02:00
parent 46022962b8
commit a5a3f1cec9
2 changed files with 45 additions and 40 deletions

View File

@ -16,6 +16,7 @@ struct Secretive: App {
}() }()
private let agentStatusChecker = AgentStatusChecker() private let agentStatusChecker = AgentStatusChecker()
private let justUpdatedChecker = JustUpdatedChecker() private let justUpdatedChecker = JustUpdatedChecker()
private let settingsStore = SettingsStore()
@AppStorage("defaultsHasRunSetup") var hasRunSetup = false @AppStorage("defaultsHasRunSetup") var hasRunSetup = false
@State private var showingSetup = false @State private var showingSetup = false
@ -27,6 +28,7 @@ struct Secretive: App {
.environmentObject(storeList) .environmentObject(storeList)
.environmentObject(Updater(checkOnLaunch: hasRunSetup)) .environmentObject(Updater(checkOnLaunch: hasRunSetup))
.environmentObject(agentStatusChecker) .environmentObject(agentStatusChecker)
.environmentObject(settingsStore)
.onAppear { .onAppear {
if !hasRunSetup { if !hasRunSetup {
showingSetup = true showingSetup = true

View File

@ -8,53 +8,56 @@
import Foundation import Foundation
class SettingsStore { class SettingsStore: ObservableObject {
static let service = "com.maxgoedjen.Secretive" let service = "com.maxgoedjen.Secretive"
} }
extension SettingsStore { extension SettingsStore {
static func set(key: String, value: String) -> Bool { subscript(key: String) -> String? {
let valueData = value.data(using: String.Encoding.utf8)! set(value) {
guard let valueData = value?.data(using: String.Encoding.utf8)! else {
if let keyVal = get(key: key) { return
if keyVal == value {
return true
} }
let updateQuery: [String: Any] = [kSecClass as String: kSecClassGenericPassword, if let keyVal = self[key] {
kSecAttrServer as String: service] if keyVal == value {
let attributes: [String: Any] = [kSecAttrAccount as String: key, return
kSecValueData as String: valueData] }
// FIXME: Make this non-blocking as described here: https://developer.apple.com/documentation/security/1393617-secitemupdate
let status = SecItemUpdate(updateQuery as CFDictionary, attributes as CFDictionary) let updateQuery: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
guard status == errSecSuccess else { kSecAttrServer as String: service]
print("Couldn't update item in keychain. " + status.description) let attributes: [String: Any] = [kSecAttrAccount as String: key,
return false kSecValueData as String: valueData]
} // FIXME: Make this non-blocking as described here: https://developer.apple.com/documentation/security/1393617-secitemupdate
} else { let status = SecItemUpdate(updateQuery as CFDictionary, attributes as CFDictionary)
let addquery: [String: Any] = [kSecClass as String: kSecClassGenericPassword, guard status == errSecSuccess else {
kSecAttrAccount as String: key, print("Couldn't update item in keychain. " + status.description)
kSecAttrServer as String: service, return
kSecValueData as String: valueData] }
// FIXME: Make this non-blocking as described here: https://developer.apple.com/documentation/security/1401659-secitemadd } else {
let status = SecItemAdd(addquery as CFDictionary, nil) let addquery: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
guard status == errSecSuccess else { kSecAttrAccount as String: key,
print("Couldn't add item to keychain. " + status.description) kSecAttrServer as String: service,
return false kSecValueData as String: valueData]
// FIXME: Make this non-blocking as described here: https://developer.apple.com/documentation/security/1401659-secitemadd
let status = SecItemAdd(addquery as CFDictionary, nil)
guard status == errSecSuccess else {
print("Couldn't add item to keychain. " + status.description)
return
}
} }
} }
return true
}
static func get(key: String) -> String? {
let getquery: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: key,
kSecAttrServer as String: service,
kSecMatchLimit as String: kSecMatchLimitOne,
kSecReturnData as String: true]
var item: CFTypeRef?
let status = SecItemCopyMatching(getquery as CFDictionary, &item)
return status == errSecSuccess ? String(decoding: item as! Data, as: UTF8.self) : nil get {
let getquery: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: key,
kSecAttrServer as String: service,
kSecMatchLimit as String: kSecMatchLimitOne,
kSecReturnData as String: true]
var item: CFTypeRef?
let status = SecItemCopyMatching(getquery as CFDictionary, &item)
return status == errSecSuccess ? String(decoding: item as! Data, as: UTF8.self) : nil
}
} }
} }