feat: add summary dashboard history charts
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
#if os(macOS)
|
||||
import AppKit
|
||||
#endif
|
||||
@@ -13,6 +14,7 @@ import AppKit
|
||||
@main
|
||||
struct iKeyMonApp: App {
|
||||
@StateObject private var sparkleUpdater = SparkleUpdater()
|
||||
private let modelContainer: ModelContainer
|
||||
|
||||
init() {
|
||||
#if os(macOS)
|
||||
@@ -20,6 +22,8 @@ struct iKeyMonApp: App {
|
||||
NSApplication.shared.applicationIconImage = customIcon
|
||||
}
|
||||
#endif
|
||||
|
||||
self.modelContainer = Self.makeModelContainer()
|
||||
}
|
||||
|
||||
var body: some Scene {
|
||||
@@ -30,6 +34,7 @@ struct iKeyMonApp: App {
|
||||
NSApp.terminate(nil)
|
||||
}
|
||||
}
|
||||
.modelContainer(modelContainer)
|
||||
.windowResizability(.contentMinSize)
|
||||
|
||||
Settings {
|
||||
@@ -38,4 +43,55 @@ struct iKeyMonApp: App {
|
||||
.environmentObject(sparkleUpdater)
|
||||
}
|
||||
}
|
||||
|
||||
private static func makeModelContainer() -> ModelContainer {
|
||||
let schema = Schema([MetricSample.self])
|
||||
let storeURL = metricStoreURL()
|
||||
let configuration = ModelConfiguration(url: storeURL)
|
||||
|
||||
do {
|
||||
return try ModelContainer(for: schema, configurations: [configuration])
|
||||
} catch {
|
||||
print("⚠️ [SwiftData] Failed to open metric store at \(storeURL.path): \(error)")
|
||||
resetMetricStore(at: storeURL)
|
||||
|
||||
do {
|
||||
return try ModelContainer(for: schema, configurations: [configuration])
|
||||
} catch {
|
||||
fatalError("Unable to create metric history store: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static func metricStoreURL() -> URL {
|
||||
let fileManager = FileManager.default
|
||||
let appSupport = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
|
||||
let directory = appSupport
|
||||
.appendingPathComponent(Bundle.main.bundleIdentifier ?? "net.24unix.iKeyMon", isDirectory: true)
|
||||
|
||||
do {
|
||||
try fileManager.createDirectory(at: directory, withIntermediateDirectories: true)
|
||||
} catch {
|
||||
fatalError("Unable to create application support directory: \(error)")
|
||||
}
|
||||
|
||||
return directory.appendingPathComponent("metric-history.store")
|
||||
}
|
||||
|
||||
private static func resetMetricStore(at url: URL) {
|
||||
let fileManager = FileManager.default
|
||||
let sidecarURLs = [
|
||||
url,
|
||||
url.appendingPathExtension("shm"),
|
||||
url.appendingPathExtension("wal")
|
||||
]
|
||||
|
||||
for sidecarURL in sidecarURLs where fileManager.fileExists(atPath: sidecarURL.path) {
|
||||
do {
|
||||
try fileManager.removeItem(at: sidecarURL)
|
||||
} catch {
|
||||
print("⚠️ [SwiftData] Failed to remove \(sidecarURL.path): \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user