Part 4: Show the Things

xcode15-and-vision
Alan Francis 2 years ago
parent a3d1af8337
commit f9f548183b
  1. 56
      WidgetShared/JustOneThingProvider.swift
  2. 6
      WidgetShared/JustOneThingWidget.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<ThingEntry>) -> ()) {
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<ConfigurationIntent>] {
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
}
}

@ -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

Loading…
Cancel
Save