diff --git a/iOS-NOTTODO/Widget-NOTTODO/Provider/MissionProvider.swift b/iOS-NOTTODO/Widget-NOTTODO/Provider/MissionProvider.swift index 6dccff5..0beabfa 100644 --- a/iOS-NOTTODO/Widget-NOTTODO/Provider/MissionProvider.swift +++ b/iOS-NOTTODO/Widget-NOTTODO/Provider/MissionProvider.swift @@ -10,37 +10,46 @@ import WidgetKit struct Provider: AppIntentTimelineProvider { @AppStorage("dailyMission", store: UserDefaults.shared) var sharedData: Data = Data() + @AppStorage("quote", store: UserDefaults.shared) var quote: String = "" func placeholder(in context: Context) -> SimpleEntry { SimpleEntry(todayMission: [], quote: "") } - + func snapshot(for configuration: ConfigurationAppIntent, in context: Context) async -> SimpleEntry { do { - let entry = try await getTimelineEntry() - return entry + try await getQuote() } catch { return SimpleEntry(todayMission: [], quote: "") } + return SimpleEntry(todayMission: [], quote: quote) } func timeline(for configuration: ConfigurationAppIntent, in context: Context) async -> Timeline { do { - let entry = try await getTimelineEntry() - return Timeline(entries: [entry], policy: .never) + let now = Date() + let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: now) ?? now + let midnightTomorrow = Calendar.current.startOfDay(for: tomorrow) + let midnightToday = Calendar.current.startOfDay(for: now) + + if now == midnightToday { + try await getQuote() + } + + guard let decodedData = try? JSONDecoder().decode([DailyMissionResponseDTO].self, from: sharedData) else { + return Timeline(entries: [], policy: .never) + } + + let entry = SimpleEntry(todayMission: decodedData, quote: quote) + return Timeline(entries: [entry], policy: .after(midnightTomorrow)) } catch { return Timeline(entries: [], policy: .never) } } - private func getTimelineEntry() async throws -> SimpleEntry { - guard let decodedData = try? JSONDecoder().decode([DailyMissionResponseDTO].self, from: sharedData) else { - return SimpleEntry(todayMission: [], quote: "") - } - + private func getQuote() async throws { let quoteResponse = try await WidgetService.shared.fetchWiseSaying() - let quote = quoteResponse.description + " - " + quoteResponse.author - return SimpleEntry(todayMission: decodedData, quote: quote) + quote = quoteResponse.description + " - " + quoteResponse.author } } diff --git a/iOS-NOTTODO/Widget-NOTTODO/Provider/TimeEntity.swift b/iOS-NOTTODO/Widget-NOTTODO/Provider/TimeEntity.swift index 2679e15..c8fa417 100644 --- a/iOS-NOTTODO/Widget-NOTTODO/Provider/TimeEntity.swift +++ b/iOS-NOTTODO/Widget-NOTTODO/Provider/TimeEntity.swift @@ -9,6 +9,18 @@ import WidgetKit struct SimpleEntry: TimelineEntry { var date: Date = .now + + var dayOfWeek: String { + return dateFormatterString(format: "E", date: date) + } var todayMission: [DailyMissionResponseDTO] let quote: String + + private func dateFormatterString(format: String?, date: Date) -> String { + let formatter = Foundation.DateFormatter() + formatter.dateFormat = format ?? "yyyy-MM-dd" + formatter.locale = Locale(identifier: "ko_KR") + let convertStr = formatter.string(from: date) + return convertStr + } } diff --git a/iOS-NOTTODO/Widget-NOTTODO/View/MediumFamily.swift b/iOS-NOTTODO/Widget-NOTTODO/View/MediumFamily.swift index 3d6dea6..9600748 100644 --- a/iOS-NOTTODO/Widget-NOTTODO/View/MediumFamily.swift +++ b/iOS-NOTTODO/Widget-NOTTODO/View/MediumFamily.swift @@ -10,15 +10,14 @@ import WidgetKit struct MediumFamily: View { var entry: Provider.Entry - @AppStorage("dayOfWeek", store: UserDefaults.shared) var dayOfWeek: String = "" - + var body: some View { let progressPercent = Double(entry.todayMission.filter { $0.completionStatus == .CHECKED }.count) / Double(entry.todayMission.count) HStack { VStack { ZStack { - Text(dayOfWeek) - .foregroundStyle(dayOfWeek == "일" ? .wdgRed : .ntdBlack) + Text(entry.dayOfWeek) + .foregroundStyle(entry.dayOfWeek == "일" ? .wdgRed : .ntdBlack) .font(.custom("Pretendard", size: 18)) .fontWeight(.semibold) CircularProgressBarView(percent: progressPercent, size: 42, lineWidth: 4.34)} diff --git a/iOS-NOTTODO/Widget-NOTTODO/View/SmallFamily.swift b/iOS-NOTTODO/Widget-NOTTODO/View/SmallFamily.swift index 8bb6127..361fc25 100644 --- a/iOS-NOTTODO/Widget-NOTTODO/View/SmallFamily.swift +++ b/iOS-NOTTODO/Widget-NOTTODO/View/SmallFamily.swift @@ -10,7 +10,6 @@ import WidgetKit struct SmallFamily: View { var entry: Provider.Entry - @AppStorage("dayOfWeek", store: UserDefaults.shared) var dayOfWeek: String = "" var body: some View { let progressPercent = Double(entry.todayMission.filter { $0.completionStatus == .CHECKED }.count) / Double(entry.todayMission.count) @@ -20,8 +19,8 @@ struct SmallFamily: View { Spacer() ZStack { - Text(dayOfWeek) - .foregroundStyle(dayOfWeek == "일" ? .wdgRed : .white) + Text(entry.dayOfWeek) + .foregroundStyle(entry.dayOfWeek == "일" ? .wdgRed : .white) .font(.custom("Pretendard", size: 13)) .fontWeight(.semibold)