mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-04-29 23:49:39 +00:00
Fix menubar crashes and add reliable auto-refresh
Fixes crash when switching timeframes or providers by handling duplicate dates in history data gracefully. Adds LaunchAgent that posts a distributed notification every 15 seconds to keep prices fresh even after long idle periods.
This commit is contained in:
parent
17b7e6ae6c
commit
68daad5dfa
3 changed files with 67 additions and 3 deletions
|
|
@ -48,6 +48,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate {
|
|||
observeStore()
|
||||
startRefreshLoop()
|
||||
setupWakeObservers()
|
||||
setupDistributedNotificationListener()
|
||||
installLaunchAgentIfNeeded()
|
||||
Task { await updateChecker.checkIfNeeded() }
|
||||
}
|
||||
|
||||
|
|
@ -69,6 +71,68 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate {
|
|||
}
|
||||
}
|
||||
|
||||
private func setupDistributedNotificationListener() {
|
||||
DistributedNotificationCenter.default().addObserver(
|
||||
forName: NSNotification.Name("com.codeburn.refresh"),
|
||||
object: nil,
|
||||
queue: .main
|
||||
) { [weak self] _ in
|
||||
Task { @MainActor in self?.forceRefresh() }
|
||||
}
|
||||
}
|
||||
|
||||
private func installLaunchAgentIfNeeded() {
|
||||
let fm = FileManager.default
|
||||
let agentName = "com.codeburn.refresh.plist"
|
||||
let home = fm.homeDirectoryForCurrentUser.path
|
||||
let destPath = "\(home)/Library/LaunchAgents/\(agentName)"
|
||||
|
||||
let plist = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>com.codeburn.refresh</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>/usr/bin/osascript</string>
|
||||
<string>-l</string>
|
||||
<string>JavaScript</string>
|
||||
<string>-e</string>
|
||||
<string>ObjC.import("Foundation"); $.NSDistributedNotificationCenter.defaultCenter.postNotificationNameObjectUserInfoDeliverImmediately("com.codeburn.refresh", $(), $(), true)</string>
|
||||
</array>
|
||||
<key>StartInterval</key>
|
||||
<integer>15</integer>
|
||||
<key>RunAtLoad</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
"""
|
||||
|
||||
do {
|
||||
let existing = try? String(contentsOfFile: destPath, encoding: .utf8)
|
||||
if existing == plist { return }
|
||||
|
||||
try fm.createDirectory(atPath: "\(home)/Library/LaunchAgents", withIntermediateDirectories: true)
|
||||
try plist.write(toFile: destPath, atomically: true, encoding: .utf8)
|
||||
|
||||
let unload = Process()
|
||||
unload.launchPath = "/bin/launchctl"
|
||||
unload.arguments = ["unload", destPath]
|
||||
try? unload.run()
|
||||
unload.waitUntilExit()
|
||||
|
||||
let load = Process()
|
||||
load.launchPath = "/bin/launchctl"
|
||||
load.arguments = ["load", destPath]
|
||||
try load.run()
|
||||
load.waitUntilExit()
|
||||
} catch {
|
||||
NSLog("CodeBurn: LaunchAgent setup failed: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
private func forceRefresh() {
|
||||
Task {
|
||||
await store.refreshQuietly(period: .today)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue