diff --git a/ios/Plugin/DailyNotificationPlugin.swift b/ios/Plugin/DailyNotificationPlugin.swift index d49918c..6e4744b 100644 --- a/ios/Plugin/DailyNotificationPlugin.swift +++ b/ios/Plugin/DailyNotificationPlugin.swift @@ -667,6 +667,74 @@ public class DailyNotificationPlugin: CAPPlugin { call.resolve(result) } + // MARK: - Configuration Methods + + /** + * Update starred plan IDs + * + * Stores plan IDs in UserDefaults for native fetcher to use. + * Matches Android SharedPreferences storage pattern. + * + * Equivalent to Android's updateStarredPlans method. + */ + @objc func updateStarredPlans(_ call: CAPPluginCall) { + guard let options = call.options else { + call.reject("Options are required") + return + } + + // Extract planIds array from options + guard let planIdsValue = options["planIds"] else { + call.reject("planIds array is required") + return + } + + var planIds: [String] = [] + + // Handle different array formats from Capacitor + if let planIdsArray = planIdsValue as? [String] { + planIds = planIdsArray + } else if let planIdsArray = planIdsValue as? [Any] { + planIds = planIdsArray.compactMap { $0 as? String } + } else if let singlePlanId = planIdsValue as? String { + planIds = [singlePlanId] + } else { + call.reject("planIds must be an array of strings") + return + } + + print("DNP-PLUGIN: Updating starred plans: count=\(planIds.count)") + + // Store in UserDefaults (matching Android SharedPreferences) + // Use suite name to match Android prefs name pattern + let prefsName = "daily_notification_timesafari" + let keyStarredPlanIds = "starredPlanIds" + + // Convert planIds to JSON array string (matching Android format) + do { + let jsonData = try JSONSerialization.data(withJSONObject: planIds, options: []) + let jsonString = String(data: jsonData, encoding: .utf8) ?? "[]" + + // Store in UserDefaults with suite name + let userDefaults = UserDefaults(suiteName: prefsName) ?? UserDefaults.standard + userDefaults.set(jsonString, forKey: keyStarredPlanIds) + userDefaults.synchronize() + + let result: [String: Any] = [ + "success": true, + "planIdsCount": planIds.count, + "updatedAt": Int64(Date().timeIntervalSince1970 * 1000) + ] + + print("DNP-PLUGIN: Starred plans updated: count=\(planIds.count)") + call.resolve(result) + + } catch { + print("DNP-PLUGIN: Failed to serialize planIds: \(error)") + call.reject("Failed to update starred plans: \(error.localizedDescription)") + } + } + // MARK: - Private Implementation Methods private func setupBackgroundTasks() {