Skip to content

Commit 2d199fc

Browse files
authored
add update checker (#2)
* update plist version with tag * check for updates
1 parent bfdbc29 commit 2d199fc

File tree

5 files changed

+76
-3
lines changed

5 files changed

+76
-3
lines changed

.github/workflows/release.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ jobs:
2121
with:
2222
xcode-version: latest-stable
2323

24+
- name: Set App Version
25+
run: |
26+
VERSION=${GITHUB_REF#refs/tags/v}
27+
echo "Setting version to $VERSION"
28+
plutil -replace CFBundleShortVersionString -string "$VERSION" insomnia/Info.plist
29+
plutil -replace CFBundleVersion -string "$VERSION" insomnia/Info.plist
30+
31+
- name: Run Tests
32+
run: swift test
33+
2434
- name: Build App and Create DMG
2535
run: |
2636
chmod +x install_app.sh

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let package = Package(
1414
.target(
1515
name: "InsomniaCore",
1616
path: "insomnia",
17-
exclude: ["InsomniaApp.swift", "Info.plist", "insomnia.entitlements", "Assets.xcassets"]
17+
exclude: ["InsomniaApp.swift", "UpdateChecker.swift", "Info.plist", "insomnia.entitlements", "Assets.xcassets"]
1818
),
1919
.executableTarget(
2020
name: "Insomnia",

insomnia/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
<key>CFBundlePackageType</key>
1818
<string>APPL</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>1.1</string>
20+
<string>2.0.1</string>
2121
<key>CFBundleVersion</key>
22-
<string>1</string>
22+
<string>201</string>
2323
<key>LSMinimumSystemVersion</key>
2424
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
2525
<key>LSUIElement</key>

insomnia/InsomniaApp.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@ import InsomniaCore
44
@main
55
struct InsomniaApp: App {
66
@StateObject private var sleepManager = SleepManager()
7+
@StateObject private var updateChecker = UpdateChecker()
78

89
var body: some Scene {
910
MenuBarExtra {
11+
// Update Banner
12+
if updateChecker.updateAvailable, let url = updateChecker.releaseURL {
13+
Button("Update Available: \(updateChecker.latestVersion)") {
14+
NSWorkspace.shared.open(url)
15+
}
16+
Divider()
17+
}
18+
1019
// Unconditional keep awake
1120
Button {
1221
sleepManager.toggle()
@@ -59,5 +68,17 @@ struct InsomniaApp: App {
5968
} label: {
6069
Image(sleepManager.iconName)
6170
}
71+
.menuBarExtraStyle(.menu) // Ensure standard menu style
72+
}
73+
74+
init() {
75+
// Trigger update check on launch
76+
let checker = UpdateChecker()
77+
checker.checkForUpdates()
78+
_updateChecker = StateObject(wrappedValue: checker)
79+
80+
// Initialize Core
81+
let manager = SleepManager()
82+
_sleepManager = StateObject(wrappedValue: manager)
6283
}
6384
}

insomnia/UpdateChecker.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Foundation
2+
import SwiftUI
3+
4+
class UpdateChecker: ObservableObject {
5+
@Published var updateAvailable: Bool = false
6+
@Published var latestVersion: String = ""
7+
@Published var releaseURL: URL? = nil
8+
9+
private var currentVersion: String {
10+
return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "1.0.0"
11+
}
12+
private let repoOwner = "karansangha"
13+
private let repoName = "Insomnia"
14+
15+
func checkForUpdates() {
16+
guard let url = URL(string: "https://api.github.com/repos/\(repoOwner)/\(repoName)/releases/latest") else { return }
17+
18+
URLSession.shared.dataTask(with: url) { [weak self] data, response, error in
19+
guard let data = data, error == nil else { return }
20+
21+
do {
22+
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
23+
let tagName = json["tag_name"] as? String {
24+
25+
let cleanTag = tagName.replacingOccurrences(of: "v", with: "")
26+
27+
DispatchQueue.main.async {
28+
if cleanTag.compare(self?.currentVersion ?? "", options: .numeric) == .orderedDescending {
29+
self?.latestVersion = tagName
30+
self?.updateAvailable = true
31+
if let htmlUrl = json["html_url"] as? String {
32+
self?.releaseURL = URL(string: htmlUrl)
33+
}
34+
}
35+
}
36+
}
37+
} catch {
38+
print("Failed to check for updates: \(error)")
39+
}
40+
}.resume()
41+
}
42+
}

0 commit comments

Comments
 (0)