From 34ec406877865ad8aa38dc8cc14b59e4de5e3687 Mon Sep 17 00:00:00 2001 From: "jijo.pulikkottil" Date: Wed, 12 Jun 2024 17:38:38 +0530 Subject: [PATCH] Irefresh the widget from main app --- StreakWidget/StreakWidget.swift | 18 +++--- StreakWidgetHelper/StreakWidgetHelper.swift | 63 ++++++++++--------- .../xcshareddata/swiftpm/Package.resolved | 14 ----- mindLAMP2Tests/mindLAMP2Tests.swift | 55 ++++++++-------- 4 files changed, 72 insertions(+), 78 deletions(-) delete mode 100644 mindLAMP.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved diff --git a/StreakWidget/StreakWidget.swift b/StreakWidget/StreakWidget.swift index a4afc94..e4e7d16 100644 --- a/StreakWidget/StreakWidget.swift +++ b/StreakWidget/StreakWidget.swift @@ -63,15 +63,15 @@ struct StreakWidgetEntryView : View { .aspectRatio(contentMode: .fit) .frame(width: 40) -// let daysText1: String = entry.currentStreak == 1 ? "day" : "days" -// Text("Current streak: \(entry.currentStreak) \(daysText1)") -// .font(.system(size: 12)) -// .fontWeight(.black) -// -// let daysText: String = entry.longestStreak == 1 ? "day" : "days" -// Text("Longest Streak: \(entry.longestStreak) \(daysText)") -// .font(.system(size: 12)) -// .fontWeight(.black) + let daysText1: String = entry.currentStreak == 1 ? "day" : "days" + Text("Current streak: \(entry.currentStreak) \(daysText1)") + .font(.system(size: 12)) + .fontWeight(.black) + + let daysText: String = entry.longestStreak == 1 ? "day" : "days" + Text("Longest Streak: \(entry.longestStreak) \(daysText)") + .font(.system(size: 12)) + .fontWeight(.black) Text("Way To Go!") .font(.system(size: 14)) diff --git a/StreakWidgetHelper/StreakWidgetHelper.swift b/StreakWidgetHelper/StreakWidgetHelper.swift index 3f25139..6b805cc 100644 --- a/StreakWidgetHelper/StreakWidgetHelper.swift +++ b/StreakWidgetHelper/StreakWidgetHelper.swift @@ -2,6 +2,7 @@ import Foundation //import LAMP +import WidgetKit public class StreakWidgetHelper { @@ -35,8 +36,6 @@ public class StreakWidgetHelper { let task = URLSession.shared.dataTask(with: urlRequest(participantId: participantId)) { [weak self] data, response, error in if let data, let urlResponse = response as? HTTPURLResponse { - - print("data jj = \(String(data: data, encoding: .utf8))") let decoder = JSONDecoder() let formatter = ISO8601DateFormatter() decoder.dateDecodingStrategy = .formatted(formatter) @@ -50,7 +49,7 @@ public class StreakWidgetHelper { if let reslut { StreakWidgetHelper.cachedEntry = reslut } - + WidgetCenter.shared.reloadAllTimelines() completion?(responseData.dates) @@ -119,6 +118,30 @@ public class StreakWidgetHelper { return false } + + // Function to extract unique days from an array of dates + func uniqueDays(from dates: [Date]) -> [Date] { + var uniqueDaysSet = Set() + var uniqueDaysArray = [Date]() + + let dateFormatter = DateFormatter() + dateFormatter.timeZone = .current + dateFormatter.locale = Locale(identifier: "en_US_POSIX") + dateFormatter.dateFormat = "yyyy-MM-dd" + + for date in dates { + print("date = \(date)") + let dayString = dateFormatter.string(from: date) + print("dayString = \(dayString)") + if !uniqueDaysSet.contains(dayString) { + uniqueDaysSet.insert(dayString) + uniqueDaysArray.append(dateFormatter.date(from: dayString)!) + } + } + + return uniqueDaysArray + } + public func findCurrentStreak(from dates: [Date]) -> Int { var currentStreak = 0 @@ -148,34 +171,14 @@ public class StreakWidgetHelper { return currentStreak } - - // Function to extract unique days from an array of dates - func uniqueDays(from dates: [Date]) -> [Date] { - var uniqueDaysSet = Set() - var uniqueDaysArray = [Date]() - - let dateFormatter = DateFormatter() - dateFormatter.locale = Locale(identifier: "en_US_POSIX") - dateFormatter.dateFormat = "yyyy-MM-dd" - - for date in dates { - let dayString = dateFormatter.string(from: date) - if !uniqueDaysSet.contains(dayString) { - uniqueDaysSet.insert(dayString) - uniqueDaysArray.append(dateFormatter.date(from: dayString)!) - } - } - - return uniqueDaysArray - } - public func findCurentAndLongestStreak(dates: [Date]) -> (Int, Int) { guard dates.count > 0 else { return (0, 0) } - let sortedDates = dates.sorted() + let uniqueDays = uniqueDays(from: dates) + let sortedDatesDesc = uniqueDays.sorted() //filter duplicates? @@ -187,8 +190,8 @@ public class StreakWidgetHelper { let today = Date() // Iterate through the sorted dates - for i in 1..