summaryrefslogtreecommitdiffstats
path: root/Solsnu.Widget/Utilities/AppGroupManager.swift
diff options
context:
space:
mode:
authorivar <i@oiee.no>2026-03-24 12:29:47 +0100
committerivar <i@oiee.no>2026-03-24 12:29:47 +0100
commitc0f84ad32693afed9d53a5962303e91172e95a3d (patch)
tree4b6f4d3546cf66d686b3d85bbd68df5c62a5a7c5 /Solsnu.Widget/Utilities/AppGroupManager.swift
parentdef2bdee1007f05d0fb6f1a69b2817d2e03b2c54 (diff)
downloadsolverv-c0f84ad32693afed9d53a5962303e91172e95a3d.tar.xz
solverv-c0f84ad32693afed9d53a5962303e91172e95a3d.zip
feat: add location fetching and sun time calculation to widget provider
- Fetch cached user location from AppGroupManager (via App Group storage) - Check if location is fresh (< 24 hours old) - Calculate sunrise/sunset times using SunTimes utility if location is fresh - Pass sun times to SolvervDef for widget display - Create widget-local copies of AppGroupManager and SunTimes utilities - Widget maintains midnight refresh policy Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Diffstat (limited to 'Solsnu.Widget/Utilities/AppGroupManager.swift')
-rw-r--r--Solsnu.Widget/Utilities/AppGroupManager.swift67
1 files changed, 67 insertions, 0 deletions
diff --git a/Solsnu.Widget/Utilities/AppGroupManager.swift b/Solsnu.Widget/Utilities/AppGroupManager.swift
new file mode 100644
index 0000000..95ff2de
--- /dev/null
+++ b/Solsnu.Widget/Utilities/AppGroupManager.swift
@@ -0,0 +1,67 @@
+import Foundation
+
+class AppGroupManager {
+ static let shared = AppGroupManager()
+ static let appGroupID = "group.com.ivarlovlie.solverv"
+
+ private lazy var userDefaults: UserDefaults? = {
+ UserDefaults(suiteName: Self.appGroupID)
+ }()
+
+ // MARK: - Location Storage
+
+ struct UserLocation: Codable {
+ let latitude: Double
+ let longitude: Double
+ let timestamp: String // ISO 8601
+ let isDefaultLocation: Bool
+ }
+
+ func saveLocation(_ location: UserLocation) {
+ guard let ud = userDefaults else { return }
+ if let encoded = try? JSONEncoder().encode(location) {
+ ud.set(encoded, forKey: "userLocation")
+ }
+ }
+
+ func getLocation() -> UserLocation? {
+ guard let ud = userDefaults,
+ let data = ud.data(forKey: "userLocation"),
+ let location = try? JSONDecoder().decode(UserLocation.self, from: data) else {
+ return nil
+ }
+ return location
+ }
+
+ // MARK: - Sunrise/Sunset Storage
+
+ struct SunTimes: Codable {
+ let date: String // ISO 8601 date only (YYYY-MM-DD)
+ let sunrise: String // ISO 8601 datetime
+ let sunset: String // ISO 8601 datetime
+ let timestamp: String // ISO 8601 when calculated
+ }
+
+ func saveSunTimes(_ sunTimes: SunTimes) {
+ guard let ud = userDefaults else { return }
+ if let encoded = try? JSONEncoder().encode(sunTimes) {
+ ud.set(encoded, forKey: "sunTimes")
+ }
+ }
+
+ func getSunTimes() -> SunTimes? {
+ guard let ud = userDefaults,
+ let data = ud.data(forKey: "sunTimes"),
+ let sunTimes = try? JSONDecoder().decode(SunTimes.self, from: data) else {
+ return nil
+ }
+ return sunTimes
+ }
+
+ // MARK: - Helpers
+
+ func clearAllData() {
+ userDefaults?.removeObject(forKey: "userLocation")
+ userDefaults?.removeObject(forKey: "sunTimes")
+ }
+}