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