summaryrefslogtreecommitdiffstats
path: root/Shared/Utilities/AppGroupManager.swift
diff options
context:
space:
mode:
authorivar <i@oiee.no>2026-05-06 21:01:10 +0200
committerivar <i@oiee.no>2026-05-06 21:01:10 +0200
commit01eee1c4fe8252bffc9334e4bb2dbbc15f002835 (patch)
treedba40ea6312844c66183043058cfead8b0a5c9d3 /Shared/Utilities/AppGroupManager.swift
parent7328b2e18121d3047ac142eaf0c8b048933d17dc (diff)
downloadsolverv-01eee1c4fe8252bffc9334e4bb2dbbc15f002835.tar.xz
solverv-01eee1c4fe8252bffc9334e4bb2dbbc15f002835.zip
feat: add Shared/ folder with merged source files
Diffstat (limited to 'Shared/Utilities/AppGroupManager.swift')
-rw-r--r--Shared/Utilities/AppGroupManager.swift67
1 files changed, 67 insertions, 0 deletions
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")
+ }
+}