Compare commits

...

2 Commits

  1. 8
      JustOneThing.xcodeproj/project.pbxproj
  2. 64
      WidgetShared/JustOneThingProvider.swift
  3. 14
      WidgetShared/JustOneThingWidget.swift
  4. 15
      WidgetShared/Thing.swift

@ -37,6 +37,9 @@
5493A81B2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5493A81A2A471E36001EBA08 /* JustOneThingProvider.swift */; }; 5493A81B2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5493A81A2A471E36001EBA08 /* JustOneThingProvider.swift */; };
5493A81C2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5493A81A2A471E36001EBA08 /* JustOneThingProvider.swift */; }; 5493A81C2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5493A81A2A471E36001EBA08 /* JustOneThingProvider.swift */; };
5493A81D2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5493A81A2A471E36001EBA08 /* JustOneThingProvider.swift */; }; 5493A81D2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5493A81A2A471E36001EBA08 /* JustOneThingProvider.swift */; };
54EB3C1F2A5434A30082B059 /* Thing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54EB3C1E2A5434A30082B059 /* Thing.swift */; };
54EB3C202A5434A30082B059 /* Thing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54EB3C1E2A5434A30082B059 /* Thing.swift */; };
54EB3C212A5434A30082B059 /* Thing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54EB3C1E2A5434A30082B059 /* Thing.swift */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */ /* Begin PBXContainerItemProxy section */
@ -128,6 +131,7 @@
54928C462A35F8CB00095445 /* WatchWidgetExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = WatchWidgetExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; }; 54928C462A35F8CB00095445 /* WatchWidgetExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = WatchWidgetExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; };
54928C4A2A35F8CB00095445 /* JustOneThingWidget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JustOneThingWidget.swift; sourceTree = "<group>"; }; 54928C4A2A35F8CB00095445 /* JustOneThingWidget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JustOneThingWidget.swift; sourceTree = "<group>"; };
5493A81A2A471E36001EBA08 /* JustOneThingProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JustOneThingProvider.swift; sourceTree = "<group>"; }; 5493A81A2A471E36001EBA08 /* JustOneThingProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JustOneThingProvider.swift; sourceTree = "<group>"; };
54EB3C1E2A5434A30082B059 /* Thing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Thing.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@ -196,6 +200,7 @@
5472018B2A3655E4005B2FCC /* WidgetShared */ = { 5472018B2A3655E4005B2FCC /* WidgetShared */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
54EB3C1E2A5434A30082B059 /* Thing.swift */,
54928C4A2A35F8CB00095445 /* JustOneThingWidget.swift */, 54928C4A2A35F8CB00095445 /* JustOneThingWidget.swift */,
5493A81A2A471E36001EBA08 /* JustOneThingProvider.swift */, 5493A81A2A471E36001EBA08 /* JustOneThingProvider.swift */,
54928C372A35F8B800095445 /* WidgetAssets.xcassets */, 54928C372A35F8B800095445 /* WidgetAssets.xcassets */,
@ -485,6 +490,7 @@
isa = PBXSourcesBuildPhase; isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
54EB3C1F2A5434A30082B059 /* Thing.swift in Sources */,
54928C212A35F89A00095445 /* Widget.intentdefinition in Sources */, 54928C212A35F89A00095445 /* Widget.intentdefinition in Sources */,
5493A81B2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */, 5493A81B2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */,
547201972A461DBE005B2FCC /* JustOneThingWidget.swift in Sources */, 547201972A461DBE005B2FCC /* JustOneThingWidget.swift in Sources */,
@ -495,6 +501,7 @@
isa = PBXSourcesBuildPhase; isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
54EB3C202A5434A30082B059 /* Thing.swift in Sources */,
547201962A461DBE005B2FCC /* JustOneThingWidget.swift in Sources */, 547201962A461DBE005B2FCC /* JustOneThingWidget.swift in Sources */,
5493A81C2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */, 5493A81C2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */,
5472018E2A36563B005B2FCC /* Widget.intentdefinition in Sources */, 5472018E2A36563B005B2FCC /* Widget.intentdefinition in Sources */,
@ -505,6 +512,7 @@
isa = PBXSourcesBuildPhase; isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
54EB3C212A5434A30082B059 /* Thing.swift in Sources */,
547201902A36563C005B2FCC /* Widget.intentdefinition in Sources */, 547201902A36563C005B2FCC /* Widget.intentdefinition in Sources */,
5493A81D2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */, 5493A81D2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */,
54928C4B2A35F8CB00095445 /* JustOneThingWidget.swift in Sources */, 54928C4B2A35F8CB00095445 /* JustOneThingWidget.swift in Sources */,

@ -9,31 +9,51 @@ import WidgetKit
import SwiftUI import SwiftUI
import Intents import Intents
struct Provider: IntentTimelineProvider { struct ThingDataStore {
func placeholder(in context: Context) -> SimpleEntry { public var things:[Thing] = [
SimpleEntry(date: Date(), configuration: ConfigurationIntent()) 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()
} }
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) { extension Date {
let entry = SimpleEntry(date: Date(), configuration: configuration) mutating public func withAddedMinutes(_ minutes:Double) -> Date {
completion(entry) self.addTimeInterval(minutes*60)
return self
} }
}
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> ()) { struct ThingProvider: IntentTimelineProvider {
var entries: [SimpleEntry] = [] var thingStore:ThingDataStore = ThingDataStore()
func placeholder(in context: Context) -> ThingEntry {
ThingEntry(thing: .placeholder)
}
// Generate a timeline consisting of five entries an hour apart, starting from the current date. func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (ThingEntry) -> ()) {
let currentDate = Date() completion(ThingEntry(thing: .placeholder, config: configuration))
for hourOffset in 0 ..< 5 { }
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
let entry = SimpleEntry(date: entryDate, configuration: configuration)
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd) func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<ThingEntry>) -> ()) {
completion(timeline) var timestamp = Date.now
let entries = thingStore.shuffledThings.map {
ThingEntry(thing: $0,
timelineDate: timestamp.withAddedMinutes(15),
config: configuration)
}
completion(Timeline(entries: entries, policy: .atEnd))
} }
}
extension ThingProvider {
func recommendations() -> [IntentRecommendation<ConfigurationIntent>] { func recommendations() -> [IntentRecommendation<ConfigurationIntent>] {
return [ return [
IntentRecommendation(intent: ConfigurationIntent(), description: "My Intent Widget") IntentRecommendation(intent: ConfigurationIntent(), description: "My Intent Widget")
@ -41,7 +61,15 @@ struct Provider: IntentTimelineProvider {
} }
} }
struct SimpleEntry: TimelineEntry {
struct ThingEntry: TimelineEntry {
let date: Date let date: Date
let configuration: ConfigurationIntent 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
}
} }

@ -17,11 +17,11 @@ struct JustOneThingWidgetBundle: WidgetBundle {
} }
struct JustOneThingWidgetEntryView : View { struct ThingEntryView : View {
var entry: Provider.Entry var entry: ThingEntry
var body: some View { var body: some View {
Text(entry.date, style: .time) Text(entry.thing.text)
} }
} }
@ -29,8 +29,10 @@ struct JustOneThingWidget: Widget {
let kind: String = "JustOneThingWidget" let kind: String = "JustOneThingWidget"
var body: some WidgetConfiguration { var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in IntentConfiguration(kind: kind,
JustOneThingWidgetEntryView(entry: entry) intent: ConfigurationIntent.self,
provider: ThingProvider()) { entry in
ThingEntryView(entry: entry)
} }
.configurationDisplayName("My Widget") .configurationDisplayName("My Widget")
.description("This is an example widget.") .description("This is an example widget.")
@ -39,7 +41,7 @@ struct JustOneThingWidget: Widget {
struct Widget_Previews: PreviewProvider { struct Widget_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
JustOneThingWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent())) ThingEntryView(entry: ThingEntry(thing: .placeholder))
#if os(watchOS) #if os(watchOS)
.previewContext(WidgetPreviewContext(family: .accessoryRectangular)) .previewContext(WidgetPreviewContext(family: .accessoryRectangular))
#else #else

@ -0,0 +1,15 @@
//
// Thing.swift
// JustOneThing
//
// Created by Alan Francis on 04/07/2023.
//
import Foundation
struct Thing {
let text: String
let createdAt: Date
static let placeholder: Thing = Thing(text: "placeholder", createdAt: Date.now)
}
Loading…
Cancel
Save