From f9f548183b8cad9aa921f5dc0dd6ccda33af3d47 Mon Sep 17 00:00:00 2001 From: Alan Francis Date: Tue, 4 Jul 2023 14:32:21 +0100 Subject: [PATCH] Part 4: Show the Things --- WidgetShared/JustOneThingProvider.swift | 56 ++++++++++++++++++------- WidgetShared/JustOneThingWidget.swift | 6 +-- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/WidgetShared/JustOneThingProvider.swift b/WidgetShared/JustOneThingProvider.swift index b1fdd4f..7deee7f 100644 --- a/WidgetShared/JustOneThingProvider.swift +++ b/WidgetShared/JustOneThingProvider.swift @@ -9,32 +9,51 @@ import WidgetKit import SwiftUI import Intents +struct ThingDataStore { + public var things:[Thing] = [ + Thing(text: "The First Thing", createdAt: Date()), + Thing(text: "The Second Thing", createdAt: Date()), + Thing(text: "The Third Thing", createdAt: Date()), + Thing(text: "The Fourth Thing", createdAt: Date()), + Thing(text: "The Fifth Thing", createdAt: Date()), + Thing(text: "The Sixth Thing", createdAt: Date()), + ] + + public var shuffledThings:[Thing] { + things.shuffled() + } +} + +extension Date { + mutating public func withAddedMinutes(_ minutes:Double) -> Date { + self.addTimeInterval(minutes*60) + return self + } +} + struct ThingProvider: IntentTimelineProvider { + var thingStore:ThingDataStore = ThingDataStore() + func placeholder(in context: Context) -> ThingEntry { - ThingEntry(date: Date(), configuration: ConfigurationIntent(), thing: .placeholder) + ThingEntry(thing: .placeholder) } func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (ThingEntry) -> ()) { - let entry = ThingEntry(date: Date(), configuration: configuration, thing: .placeholder) - context. - completion(entry) + completion(ThingEntry(thing: .placeholder, config: configuration)) } func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline) -> ()) { - var entries: [ThingEntry] = [] - - // 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 = ThingEntry(date: entryDate, configuration: configuration, thing: .placeholder) - entries.append(entry) + var timestamp = Date.now + let entries = thingStore.shuffledThings.map { + ThingEntry(thing: $0, + timelineDate: timestamp.withAddedMinutes(15), + config: configuration) } - - let timeline = Timeline(entries: entries, policy: .atEnd) - completion(timeline) + completion(Timeline(entries: entries, policy: .atEnd)) } +} +extension ThingProvider { func recommendations() -> [IntentRecommendation] { return [ IntentRecommendation(intent: ConfigurationIntent(), description: "My Intent Widget") @@ -42,8 +61,15 @@ struct ThingProvider: IntentTimelineProvider { } } + struct ThingEntry: TimelineEntry { let date: Date let configuration: ConfigurationIntent let thing: Thing + + public init(thing:Thing, timelineDate:Date = .now, config:ConfigurationIntent = ConfigurationIntent()) { + self.thing = thing + self.date = timelineDate + self.configuration = config + } } diff --git a/WidgetShared/JustOneThingWidget.swift b/WidgetShared/JustOneThingWidget.swift index 23412ed..62a8d85 100644 --- a/WidgetShared/JustOneThingWidget.swift +++ b/WidgetShared/JustOneThingWidget.swift @@ -18,10 +18,10 @@ struct JustOneThingWidgetBundle: WidgetBundle { struct ThingEntryView : View { - var entry: ThingProvider.Entry + var entry: ThingEntry var body: some View { - Text(entry.date, style: .time) + Text(entry.thing.text) } } @@ -41,7 +41,7 @@ struct JustOneThingWidget: Widget { struct Widget_Previews: PreviewProvider { static var previews: some View { - ThingEntryView(entry: ThingEntry(date: Date(), configuration: ConfigurationIntent(), thing: .placeholder)) + ThingEntryView(entry: ThingEntry(thing: .placeholder)) #if os(watchOS) .previewContext(WidgetPreviewContext(family: .accessoryRectangular)) #else