Files
Rsync-Manager/RsyncManager/RsyncManagerApp.swift

79 lines
2.9 KiB
Swift

import SwiftUI
import SwiftData
@main
@MainActor
struct RsyncManagerApp: App {
let sharedModelContainer: ModelContainer
let startupStorageError: String?
@StateObject private var runner: RsyncRunner
@StateObject private var automation: BackgroundSyncManager
init() {
let schema = Schema([
SyncJob.self,
SyncRun.self,
])
let configuration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
KeychainStore.delete("defaultSSHPassword")
do {
let container = try ModelContainer(for: schema, configurations: [configuration])
sharedModelContainer = container
startupStorageError = nil
let rsyncRunner = RsyncRunner()
_runner = StateObject(wrappedValue: rsyncRunner)
_automation = StateObject(wrappedValue: BackgroundSyncManager(modelContext: container.mainContext, runner: rsyncRunner))
} catch {
let fallbackConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: true)
do {
let fallback = try ModelContainer(for: schema, configurations: [fallbackConfiguration])
sharedModelContainer = fallback
startupStorageError = "Rsync Manager could not open its persistent database and is running with temporary in-memory storage for this session. Jobs or history created now will not persist. Original error: \(error.localizedDescription)"
let rsyncRunner = RsyncRunner()
_runner = StateObject(wrappedValue: rsyncRunner)
_automation = StateObject(wrappedValue: BackgroundSyncManager(modelContext: fallback.mainContext, runner: rsyncRunner))
} catch {
fatalError("Rsync Manager could not initialise persistent or temporary storage: \(error)")
}
}
}
var body: some Scene {
WindowGroup {
ContentView(startupStorageError: startupStorageError)
.frame(minWidth: 1040, minHeight: 680)
.environmentObject(runner)
.environmentObject(automation)
.onAppear { automation.start() }
}
.modelContainer(sharedModelContainer)
.commands {
RsyncManagerCommands()
}
Window("Rsync Manager Help", id: "help") {
HelpView()
}
.defaultSize(width: 940, height: 780)
Settings {
SettingsView()
.environmentObject(automation)
}
}
}
private struct RsyncManagerCommands: Commands {
@Environment(\.openWindow) private var openWindow
var body: some Commands {
CommandGroup(replacing: .help) {
Button("Rsync Manager Help") {
openWindow(id: "help")
}
.keyboardShortcut("?", modifiers: [.command])
}
}
}