Part 4: Add a Thing struct

xcode15-and-vision
Alan Francis 2 years ago
parent 1dd9520257
commit a3d1af8337
  1. 8
      JustOneThing.xcodeproj/project.pbxproj
  2. 20
      WidgetShared/JustOneThingProvider.swift
  3. 12
      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,24 +9,25 @@ import WidgetKit
import SwiftUI import SwiftUI
import Intents import Intents
struct Provider: IntentTimelineProvider { struct ThingProvider: IntentTimelineProvider {
func placeholder(in context: Context) -> SimpleEntry { func placeholder(in context: Context) -> ThingEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent()) ThingEntry(date: Date(), configuration: ConfigurationIntent(), thing: .placeholder)
} }
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) { func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (ThingEntry) -> ()) {
let entry = SimpleEntry(date: Date(), configuration: configuration) let entry = ThingEntry(date: Date(), configuration: configuration, thing: .placeholder)
context.
completion(entry) completion(entry)
} }
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> ()) { func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<ThingEntry>) -> ()) {
var entries: [SimpleEntry] = [] var entries: [ThingEntry] = []
// Generate a timeline consisting of five entries an hour apart, starting from the current date. // Generate a timeline consisting of five entries an hour apart, starting from the current date.
let currentDate = Date() let currentDate = Date()
for hourOffset in 0 ..< 5 { for hourOffset in 0 ..< 5 {
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)! let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
let entry = SimpleEntry(date: entryDate, configuration: configuration) let entry = ThingEntry(date: entryDate, configuration: configuration, thing: .placeholder)
entries.append(entry) entries.append(entry)
} }
@ -41,7 +42,8 @@ struct Provider: IntentTimelineProvider {
} }
} }
struct SimpleEntry: TimelineEntry { struct ThingEntry: TimelineEntry {
let date: Date let date: Date
let configuration: ConfigurationIntent let configuration: ConfigurationIntent
let thing: Thing
} }

@ -17,8 +17,8 @@ struct JustOneThingWidgetBundle: WidgetBundle {
} }
struct JustOneThingWidgetEntryView : View { struct ThingEntryView : View {
var entry: Provider.Entry var entry: ThingProvider.Entry
var body: some View { var body: some View {
Text(entry.date, style: .time) Text(entry.date, style: .time)
@ -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(date: Date(), configuration: ConfigurationIntent(), 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