Fully working

This commit is contained in:
Hariharan Mudaliar 2025-10-20 00:53:16 +05:30
commit b44d369994
6 changed files with 149 additions and 10 deletions

View file

@ -408,7 +408,7 @@
"$(inherited)", "$(inherited)",
"@executable_path/../Frameworks", "@executable_path/../Frameworks",
); );
MARKETING_VERSION = "1.0.0-beta"; MARKETING_VERSION = "1.0.1-beta";
PRODUCT_BUNDLE_IDENTIFIER = com.karthikinformationtechnology.DynamicIsland; PRODUCT_BUNDLE_IDENTIFIER = com.karthikinformationtechnology.DynamicIsland;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = ""; PROVISIONING_PROFILE_SPECIFIER = "";
@ -456,7 +456,7 @@
"$(inherited)", "$(inherited)",
"@executable_path/../Frameworks", "@executable_path/../Frameworks",
); );
MARKETING_VERSION = "1.0.0-beta"; MARKETING_VERSION = "1.0.1-beta";
PRODUCT_BUNDLE_IDENTIFIER = com.karthikinformationtechnology.DynamicIsland; PRODUCT_BUNDLE_IDENTIFIER = com.karthikinformationtechnology.DynamicIsland;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = ""; PROVISIONING_PROFILE_SPECIFIER = "";

View file

@ -658,6 +658,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
previousScreens = NSScreen.screens previousScreens = NSScreen.screens
if Defaults[.enableLockScreenWeatherWidget] { if Defaults[.enableLockScreenWeatherWidget] {
LockScreenWeatherManager.shared.prepareLocationAccess()
Task { @MainActor in Task { @MainActor in
await LockScreenWeatherManager.shared.refresh(force: true) await LockScreenWeatherManager.shared.refresh(force: true)
} }

View file

@ -11,6 +11,10 @@
<true/> <true/>
<key>NSBluetoothAlwaysUsageDescription</key> <key>NSBluetoothAlwaysUsageDescription</key>
<string>Dynamic Island needs Bluetooth access to detect when audio devices connect and display their battery status in the HUD.</string> <string>Dynamic Island needs Bluetooth access to detect when audio devices connect and display their battery status in the HUD.</string>
<key>NSLocationUsageDescription</key>
<string>Dynamic Island uses your location to display accurate lock screen weather.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Dynamic Island uses your location to display accurate lock screen weather.</string>
<key>NSScreenCaptureUsageDescription</key> <key>NSScreenCaptureUsageDescription</key>
<string>Dynamic Island needs screen recording permission to capture screenshots for the AI assistant feature.</string> <string>Dynamic Island needs screen recording permission to capture screenshots for the AI assistant feature.</string>
<key>SUEnableDownloaderService</key> <key>SUEnableDownloaderService</key>

View file

@ -17482,8 +17482,7 @@
}, },
"Weather: %@ %@" : { "Weather: %@ %@" : {
"comment" : "A label that describes the weather and temperature. The value inside the parentheses is a combination of the weather description and the temperature.", "comment" : "Weather description and temperature",
"isCommentAutoGenerated" : true,
"localizations" : { "localizations" : {
"en" : { "en" : {
"stringUnit" : { "stringUnit" : {
@ -17493,6 +17492,17 @@
} }
} }
}, },
"Weather: %@ %@ in %@" : {
"comment" : "Weather description, temperature, and location",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "new",
"value" : "Weather: %1$@ %2$@ in %3$@"
}
}
}
},
"Welcome" : { "Welcome" : {
"localizations" : { "localizations" : {
"ar" : { "ar" : {

View file

@ -4,13 +4,20 @@ struct LockScreenWeatherWidget: View {
let snapshot: LockScreenWeatherSnapshot let snapshot: LockScreenWeatherSnapshot
var body: some View { var body: some View {
HStack(spacing: 6) { HStack(spacing: 8) {
Image(systemName: snapshot.symbolName) Image(systemName: snapshot.symbolName)
.font(.system(size: 26, weight: .medium)) .font(.system(size: 26, weight: .medium))
.symbolRenderingMode(.hierarchical) .symbolRenderingMode(.hierarchical)
Text(snapshot.temperatureText) Text(snapshot.temperatureText)
.font(.system(size: 24, weight: .semibold, design: .rounded)) .font(.system(size: 24, weight: .semibold, design: .rounded))
.kerning(-0.4) .kerning(-0.4)
if let locationName = snapshot.locationName, !locationName.isEmpty {
Text("•")
.font(.system(size: 22, weight: .semibold, design: .rounded))
Text(locationName)
.font(.system(size: 20, weight: .medium, design: .rounded))
.foregroundStyle(Color.white.opacity(0.65))
}
} }
.foregroundStyle(Color.white.opacity(0.65)) .foregroundStyle(Color.white.opacity(0.65))
.padding(.horizontal, 10) .padding(.horizontal, 10)
@ -18,6 +25,23 @@ struct LockScreenWeatherWidget: View {
.background(Color.clear) .background(Color.clear)
.shadow(color: .black.opacity(0.35), radius: 8, x: 0, y: 3) .shadow(color: .black.opacity(0.35), radius: 8, x: 0, y: 3)
.accessibilityElement(children: .ignore) .accessibilityElement(children: .ignore)
.accessibilityLabel("Weather: \(snapshot.description) \(snapshot.temperatureText)") .accessibilityLabel(accessibilityLabel)
}
private var accessibilityLabel: String {
if let locationName = snapshot.locationName, !locationName.isEmpty {
return String(
format: NSLocalizedString("Weather: %@ %@ in %@", comment: "Weather description, temperature, and location"),
snapshot.description,
snapshot.temperatureText,
locationName
)
}
return String(
format: NSLocalizedString("Weather: %@ %@", comment: "Weather description and temperature"),
snapshot.description,
snapshot.temperatureText
)
} }
} }

View file

@ -1,5 +1,6 @@
import Foundation import Foundation
import Defaults import Defaults
import CoreLocation
@MainActor @MainActor
final class LockScreenWeatherManager: ObservableObject { final class LockScreenWeatherManager: ObservableObject {
@ -8,16 +9,23 @@ final class LockScreenWeatherManager: ObservableObject {
@Published private(set) var snapshot: LockScreenWeatherSnapshot? @Published private(set) var snapshot: LockScreenWeatherSnapshot?
private let provider = LockScreenWeatherProvider() private let provider = LockScreenWeatherProvider()
private let locationProvider = LockScreenWeatherLocationProvider()
private var lastFetchDate: Date? private var lastFetchDate: Date?
private init() {} private init() {}
func prepareLocationAccess() {
locationProvider.prepareAuthorization()
}
func showWeatherWidget() { func showWeatherWidget() {
guard Defaults[.enableLockScreenWeatherWidget] else { guard Defaults[.enableLockScreenWeatherWidget] else {
LockScreenWeatherPanelManager.shared.hide() LockScreenWeatherPanelManager.shared.hide()
return return
} }
locationProvider.prepareAuthorization()
Task { @MainActor [weak self] in Task { @MainActor [weak self] in
guard let self else { return } guard let self else { return }
await self.refresh(force: self.snapshot == nil) await self.refresh(force: self.snapshot == nil)
@ -43,7 +51,8 @@ final class LockScreenWeatherManager: ObservableObject {
} }
do { do {
let snapshot = try await provider.fetchSnapshot() let location = await locationProvider.currentLocation()
let snapshot = try await provider.fetchSnapshot(location: location)
self.snapshot = snapshot self.snapshot = snapshot
lastFetchDate = Date() lastFetchDate = Date()
LockScreenWeatherPanelManager.shared.update(with: snapshot) LockScreenWeatherPanelManager.shared.update(with: snapshot)
@ -57,6 +66,7 @@ struct LockScreenWeatherSnapshot: Equatable {
let temperatureText: String let temperatureText: String
let symbolName: String let symbolName: String
let description: String let description: String
let locationName: String?
} }
private actor LockScreenWeatherProvider { private actor LockScreenWeatherProvider {
@ -71,8 +81,19 @@ private actor LockScreenWeatherProvider {
decoder = JSONDecoder() decoder = JSONDecoder()
} }
func fetchSnapshot() async throws -> LockScreenWeatherSnapshot { func fetchSnapshot(location: CLLocation?) async throws -> LockScreenWeatherSnapshot {
guard let url = URL(string: "https://wttr.in/?format=j1") else { let locationSuffix: String
if let coordinate = location?.coordinate {
let lat = String(format: "%.4f", coordinate.latitude)
let lon = String(format: "%.4f", coordinate.longitude)
locationSuffix = "\(lat),\(lon)"
} else {
locationSuffix = ""
}
let urlString = locationSuffix.isEmpty ? "https://wttr.in/?format=j1" : "https://wttr.in/\(locationSuffix)?format=j1"
guard let url = URL(string: urlString) else {
throw WeatherProviderError.invalidURL throw WeatherProviderError.invalidURL
} }
@ -94,10 +115,14 @@ private actor LockScreenWeatherProvider {
let symbol = WeatherSymbolMapper.symbol(for: code) let symbol = WeatherSymbolMapper.symbol(for: code)
let description = condition.localizedDescription let description = condition.localizedDescription
let nearest = payload.nearestArea.first
let locationName = nearest?.preferredName
return LockScreenWeatherSnapshot( return LockScreenWeatherSnapshot(
temperatureText: temperatureText, temperatureText: temperatureText,
symbolName: symbol, symbolName: symbol,
description: description description: description,
locationName: locationName
) )
} }
} }
@ -110,8 +135,10 @@ enum WeatherProviderError: Error {
private struct WTTRResponse: Decodable { private struct WTTRResponse: Decodable {
let current_condition: [WTTRCurrentCondition] let current_condition: [WTTRCurrentCondition]
let nearest_area: [WTTRNearestArea]?
var currentCondition: [WTTRCurrentCondition] { current_condition } var currentCondition: [WTTRCurrentCondition] { current_condition }
var nearestArea: [WTTRNearestArea] { nearest_area ?? [] }
} }
private struct WTTRCurrentCondition: Decodable { private struct WTTRCurrentCondition: Decodable {
@ -144,6 +171,31 @@ private struct WTTRTextValue: Decodable {
let value: String let value: String
} }
private struct WTTRNearestArea: Decodable {
let areaName: [WTTRTextValue]?
let region: [WTTRTextValue]?
let country: [WTTRTextValue]?
private enum CodingKeys: String, CodingKey {
case areaName = "areaName"
case region
case country
}
var preferredName: String? {
if let name = areaName?.first?.value, !name.isEmpty {
return name
}
if let regionName = region?.first?.value, !regionName.isEmpty {
return regionName
}
if let countryName = country?.first?.value, !countryName.isEmpty {
return countryName
}
return nil
}
}
private enum WeatherSymbolMapper { private enum WeatherSymbolMapper {
static func symbol(for code: Int) -> String { static func symbol(for code: Int) -> String {
switch code { switch code {
@ -168,3 +220,51 @@ private enum WeatherSymbolMapper {
} }
} }
} }
@MainActor
private final class LockScreenWeatherLocationProvider: NSObject, CLLocationManagerDelegate {
private let manager: CLLocationManager
private var continuation: CheckedContinuation<CLLocation?, Never>?
private var lastLocation: CLLocation?
override init() {
manager = CLLocationManager()
super.init()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyHundredMeters
}
func prepareAuthorization() {
let status = CLLocationManager.authorizationStatus()
if status == .notDetermined {
manager.requestWhenInUseAuthorization()
}
}
func currentLocation() async -> CLLocation? {
let status = CLLocationManager.authorizationStatus()
switch status {
case .authorizedAlways, .authorizedWhenInUse:
if let lastLocation, abs(lastLocation.timestamp.timeIntervalSinceNow) < 1800 {
return lastLocation
}
manager.requestLocation()
return await withCheckedContinuation { continuation in
self.continuation = continuation
}
default:
return nil
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
lastLocation = locations.last
continuation?.resume(returning: lastLocation)
continuation = nil
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
continuation?.resume(returning: nil)
continuation = nil
}
}