diff --git a/SNUTT-2022/SNUTT.xcodeproj/project.pbxproj b/SNUTT-2022/SNUTT.xcodeproj/project.pbxproj index 2039ec72..0ff8f3a5 100644 --- a/SNUTT-2022/SNUTT.xcodeproj/project.pbxproj +++ b/SNUTT-2022/SNUTT.xcodeproj/project.pbxproj @@ -629,7 +629,6 @@ children = ( BE98A068288AFC1500C2CE95 /* SNUTTWidgetBundle.swift */, BE804C052894157500782F16 /* SNUTTWidgetProvider.swift */, - BE98A07C288B02F500C2CE95 /* DailyWidget */, BE98A079288B029000C2CE95 /* TimetableWidget */, BE98A06A288AFC1600C2CE95 /* SNUTTWidget.intentdefinition */, BE98A06B288AFC1600C2CE95 /* Assets.xcassets */, @@ -647,13 +646,6 @@ path = TimetableWidget; sourceTree = ""; }; - BE98A07C288B02F500C2CE95 /* DailyWidget */ = { - isa = PBXGroup; - children = ( - ); - path = DailyWidget; - sourceTree = ""; - }; BEB3B6A328CDE1EF00E56062 /* Utils */ = { isa = PBXGroup; children = ( diff --git a/SNUTT-2022/SNUTT/AppState/States/TimetableState.swift b/SNUTT-2022/SNUTT/AppState/States/TimetableState.swift index cc5564d5..34e9d42d 100644 --- a/SNUTT-2022/SNUTT/AppState/States/TimetableState.swift +++ b/SNUTT-2022/SNUTT/AppState/States/TimetableState.swift @@ -29,6 +29,12 @@ class TimetableState: ObservableObject { } .store(in: &bag) + $configuration + .sink { _ in + WidgetCenter.shared.reloadTimelines(ofKind: "TimetableWidget") + } + .store(in: &bag) + // sync between current timetable and timetable metadata $current .compactMap { $0 } diff --git a/SNUTT-2022/SNUTTWidget/SNUTTWidgetProvider.swift b/SNUTT-2022/SNUTTWidget/SNUTTWidgetProvider.swift index 7518692f..4c8596fb 100644 --- a/SNUTT-2022/SNUTTWidget/SNUTTWidgetProvider.swift +++ b/SNUTT-2022/SNUTTWidget/SNUTTWidgetProvider.swift @@ -12,44 +12,41 @@ struct SNUTTWidgetProvider: IntentTimelineProvider { var userDefaultsRepository: UserDefaultsRepositoryProtocol = UserDefaultsRepository(storage: .shared) + var currentTimetable: Timetable? { + guard let dto = userDefaultsRepository.get(TimetableDto.self, key: .currentTimetable) else { return nil } + return Timetable(from: dto) + } + + var timetableConfig: TimetableConfiguration { + userDefaultsRepository.get(TimetableConfiguration.self, key: .timetableConfig, defaultValue: TimetableConfiguration()) + } + func placeholder(in _: Context) -> Entry { - Entry(date: Date(), configuration: ConfigurationIntent(), currentTimetable: getCurrentTimetable()) + Entry(date: Date(), configuration: ConfigurationIntent(), currentTimetable: currentTimetable, timetableConfig: timetableConfig) } func getSnapshot(for configuration: ConfigurationIntent, in _: Context, completion: @escaping (Entry) -> Void) { - let entry = Entry(date: Date(), configuration: configuration, currentTimetable: getCurrentTimetable()) + let entry = Entry(date: Date(), configuration: configuration, currentTimetable: currentTimetable, timetableConfig: timetableConfig) completion(entry) } func getTimeline(for configuration: ConfigurationIntent, in _: Context, completion: @escaping (Timeline) -> Void) { - var entries: [Entry] = [] - - // Generate a timeline consisting of five entries an hour apart, starting from the current date. - let currentDate = Date() - for hourOffset in 0 ..< 5 { - let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)! - let entry = Entry(date: entryDate, configuration: configuration, currentTimetable: getCurrentTimetable()) - entries.append(entry) - } - + let entries = [Entry(date: Date(), configuration: configuration, currentTimetable: currentTimetable, timetableConfig: timetableConfig)] let timeline = Timeline(entries: entries, policy: .atEnd) completion(timeline) } - - func getCurrentTimetable() -> Timetable? { - guard let dto = userDefaultsRepository.get(TimetableDto.self, key: .currentTimetable) else { return nil } - return Timetable(from: dto) - } } struct TimetableEntry: TimelineEntry { let date: Date let configuration: ConfigurationIntent let currentTimetable: Timetable? + let timetableConfig: TimetableConfiguration - init(date: Date, configuration: ConfigurationIntent, currentTimetable: Timetable? = nil) { + init(date: Date, configuration: ConfigurationIntent, currentTimetable: Timetable?, timetableConfig: TimetableConfiguration) { self.date = date self.configuration = configuration self.currentTimetable = currentTimetable + self.timetableConfig = timetableConfig } } diff --git a/SNUTT-2022/SNUTTWidget/TimetableWidget/TimetableWidgetEntryView.swift b/SNUTT-2022/SNUTTWidget/TimetableWidget/TimetableWidgetEntryView.swift index 3cdfdbef..370d3c06 100644 --- a/SNUTT-2022/SNUTTWidget/TimetableWidget/TimetableWidgetEntryView.swift +++ b/SNUTT-2022/SNUTTWidget/TimetableWidget/TimetableWidgetEntryView.swift @@ -11,16 +11,10 @@ import WidgetKit struct TimetableWidgetEntryView: View { var entry: SNUTTWidgetProvider.Entry - var config: TimetableConfiguration = { - var config = TimetableConfiguration() - config.maxHour = 18 - return config - }() - var body: some View { ZStack { STColor.systemBackground - TimetableZStack(current: entry.currentTimetable, config: config) + TimetableZStack(current: entry.currentTimetable, config: entry.timetableConfig) } } } @@ -28,7 +22,7 @@ struct TimetableWidgetEntryView: View { #if DEBUG struct TimetableWidgetEntryView_Previews: PreviewProvider { static var previews: some View { - TimetableWidgetEntryView(entry: TimetableEntry(date: Date(), configuration: ConfigurationIntent(), currentTimetable: .preview)) + TimetableWidgetEntryView(entry: TimetableEntry(date: Date(), configuration: ConfigurationIntent(), currentTimetable: .preview, timetableConfig: TimetableConfiguration())) .previewContext(WidgetPreviewContext(family: .systemLarge)) } }