Skip to content

Commit

Permalink
[Fix] #252 - 요일 가져오기 및 명언 자정에만 업데이트 되도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
yungu0010 committed May 10, 2024
1 parent 5f8ad7b commit 46b4083
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
33 changes: 21 additions & 12 deletions iOS-NOTTODO/Widget-NOTTODO/Provider/MissionProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimpleEntry> {
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
}
}
12 changes: 12 additions & 0 deletions iOS-NOTTODO/Widget-NOTTODO/Provider/TimeEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
7 changes: 3 additions & 4 deletions iOS-NOTTODO/Widget-NOTTODO/View/MediumFamily.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down
5 changes: 2 additions & 3 deletions iOS-NOTTODO/Widget-NOTTODO/View/SmallFamily.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down

0 comments on commit 46b4083

Please sign in to comment.