From 01eee1c4fe8252bffc9334e4bb2dbbc15f002835 Mon Sep 17 00:00:00 2001 From: ivar Date: Wed, 6 May 2026 21:01:10 +0200 Subject: feat: add Shared/ folder with merged source files --- Shared/Utilities/AppGroupManager.swift | 67 ++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Shared/Utilities/AppGroupManager.swift (limited to 'Shared/Utilities/AppGroupManager.swift') diff --git a/Shared/Utilities/AppGroupManager.swift b/Shared/Utilities/AppGroupManager.swift new file mode 100644 index 0000000..69b5d73 --- /dev/null +++ b/Shared/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 + 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 + let sunrise: String + let sunset: String + let timestamp: String + } + + 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") + } +} -- cgit v1.3