-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppManager.swift
More file actions
93 lines (81 loc) · 2.94 KB
/
AppManager.swift
File metadata and controls
93 lines (81 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// AppManager.swift
// CloudflareApp
//
// Created by Lsong on 1/27/26.
//
import Foundation
import SwiftUI
import Security
// MARK: - App Manager
class AppManager: ObservableObject {
// MARK: - 单例
static let shared = AppManager()
@AppStorage("colorScheme") var colorSchemeMode: ColorSchemeMode = .system
@AppStorage("appTintColor") var appTintColor: AppTintColor = .orange
@AppStorage("appFontDesign") var appFontDesign: AppFontDesign = .standard
@AppStorage("appFontSize") var appFontSize: AppFontSize = .xlarge
@AppStorage("appFontWidth") var appFontWidth: AppFontWidth = .expanded
@AppStorage("currentCredentialId") var currentCredentialId: String = ""
@AppStorage("currentAccountId") var currentAccountId: String = ""
private let credentialsKey = "cloudflare.credentials"
@Published var credentials: [CloudflareCredential] = []
var api: CloudflareAPI?
// MARK: - 发布的状态属性
@Published var showingAlert = false
@Published var alertTitle = ""
@Published var alertMessage = ""
var currentCredential: CloudflareCredential? {
return credentials.first { credential in
return credential.id.uuidString == currentCredentialId
}
}
private init() {
loadCredentials()
if let credential = currentCredential {
api = CloudflareAPI(credential: credential)
}
}
func showAlert(title: String, message: String) {
alertTitle = title
alertMessage = message
showingAlert = true
}
func addCredential(credential: CloudflareCredential) {
credentials.append(credential)
saveCredentials()
}
func saveCredentials() {
do {
let data = try JSONEncoder().encode(credentials)
UserDefaults.standard.set(data, forKey: credentialsKey)
} catch {
showAlert(title: "Save Failed", message: "Unable to save credentials.")
}
}
func loadCredentials() {
guard let data = UserDefaults.standard.data(forKey: credentialsKey) else { return }
do {
credentials = try JSONDecoder().decode([CloudflareCredential].self, from: data)
} catch {
showAlert(title: "Load Failed", message: "Unable to load credentials.")
}
}
func switchCredential(credential: CloudflareCredential) {
currentCredentialId = credential.id.uuidString
api = CloudflareAPI(credential: credential)
}
func switchAccount(account: CloudflareAccount) {
currentAccountId = account.id
}
}
extension AppManager {
var appName: String {
Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String
?? Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String
?? "Cloudflare"
}
var appVersion: String {
Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0.0"
}
}