-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharedWidgetManager.swift
More file actions
107 lines (107 loc) · 3.25 KB
/
SharedWidgetManager.swift
File metadata and controls
107 lines (107 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// SharedWidgetManager.swift
// SwahiLib
//
// Created by @sirodevs on 24/10/2025.
//
//
//import Foundation
//
//public final class SharedDataManager {
// public static let shared = SharedDataManager()
//
// private let userDefaults: UserDefaults
// private let wordsKey = "shared_words_data"
// private let currentWordKey = "shared_current_word"
//
// #if DEBUG
// private let appGroup = "group.com.swahilib.stg.widget"
// #else
// private let appGroup = "group.com.swahilib.widget"
// #endif
//
// private init() {
// // Use app group for sharing data between app and widget
// self.userDefaults = UserDefaults(suiteName: appGroup)
// }
//
// // MARK: - Save Data
// public func saveAllWords(_ words: [Word]) {
// do {
// let encoder = JSONEncoder()
// let encoded = try encoder.encode(words)
// userDefaults.set(encoded, forKey: wordsKey)
// userDefaults.synchronize()
//
// // Update current word for widget
// updateCurrentWord(from: words)
// } catch {
// print("Failed to save words: \(error)")
// }
// }
//
// public func saveCurrentWord(_ word: Word) {
// do {
// let encoder = JSONEncoder()
// let encoded = try encoder.encode(word.widgetData)
// userDefaults.set(encoded, forKey: currentWordKey)
// userDefaults.synchronize()
// } catch {
// print("Failed to save current word: \(error)")
// }
// }
//
// // MARK: - Retrieve Data
// public func getAllWords() -> [Word] {
// guard let data = userDefaults.data(forKey: wordsKey) else {
// return []
// }
//
// do {
// let decoder = JSONDecoder()
// return try decoder.decode([Word].self, from: data)
// } catch {
// print("Failed to decode words: \(error)")
// return []
// }
// }
//
// public func getCurrentWord() -> WordWidgetData? {
// guard let data = userDefaults.data(forKey: currentWordKey) else {
// return nil
// }
//
// do {
// let decoder = JSONDecoder()
// return try decoder.decode(WordWidgetData.self, from: data)
// } catch {
// print("Failed to decode current word: \(error)")
// return nil
// }
// }
//
// // MARK: - Helper Methods
// private func updateCurrentWord(from words: [Word]) {
// // Strategy: Use date to determine which word to show
// let calendar = Calendar.current
// let dayOfYear = calendar.ordinality(of: .day, in: .year, for: Date()) ?? 0
//
// if !words.isEmpty {
// let index = dayOfYear % words.count
// let currentWord = words[index]
// saveCurrentWord(currentWord)
// }
// }
//
// public func refreshCurrentWord() {
// let words = getAllWords()
// updateCurrentWord(from: words)
// }
//
// // MARK: - Clear Data
// public func clearAllData() {
// userDefaults.removeObject(forKey: wordsKey)
// userDefaults.removeObject(forKey: currentWordKey)
// userDefaults.synchronize()
// }
//}