Skip to content

Commit

Permalink
Fix Widget timetableConfig issue (#212)
Browse files Browse the repository at this point in the history
* load `timetableConfig` from UserDefaults to feed into Widgets

* reload widget when timetableConfig gets modified
  • Loading branch information
shp7724 authored Jan 27, 2023
1 parent 081910b commit 815d392
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 34 deletions.
8 changes: 0 additions & 8 deletions SNUTT-2022/SNUTT.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,6 @@
children = (
BE98A068288AFC1500C2CE95 /* SNUTTWidgetBundle.swift */,
BE804C052894157500782F16 /* SNUTTWidgetProvider.swift */,
BE98A07C288B02F500C2CE95 /* DailyWidget */,
BE98A079288B029000C2CE95 /* TimetableWidget */,
BE98A06A288AFC1600C2CE95 /* SNUTTWidget.intentdefinition */,
BE98A06B288AFC1600C2CE95 /* Assets.xcassets */,
Expand All @@ -647,13 +646,6 @@
path = TimetableWidget;
sourceTree = "<group>";
};
BE98A07C288B02F500C2CE95 /* DailyWidget */ = {
isa = PBXGroup;
children = (
);
path = DailyWidget;
sourceTree = "<group>";
};
BEB3B6A328CDE1EF00E56062 /* Utils */ = {
isa = PBXGroup;
children = (
Expand Down
6 changes: 6 additions & 0 deletions SNUTT-2022/SNUTT/AppState/States/TimetableState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
33 changes: 15 additions & 18 deletions SNUTT-2022/SNUTTWidget/SNUTTWidgetProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entry>) -> 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,18 @@ 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)
}
}
}

#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))
}
}
Expand Down

0 comments on commit 815d392

Please sign in to comment.