From 42580684c19ff3707d4d26b43e6b6b36c321a560 Mon Sep 17 00:00:00 2001 From: Alan Francis Date: Fri, 23 Jun 2023 20:16:57 +0100 Subject: [PATCH] End of Step 10: Widget code moved to a shared folder. --- JustOneThing.xcodeproj/project.pbxproj | 24 +++---- MacWidget/MacWidget.swift | 69 ------------------- PhoneWidget/PhoneWidget.swift | 68 ------------------ PhoneWidget/PhoneWidgetBundle.swift | 16 ----- .../JustOneThingWidget.swift | 27 ++++---- 5 files changed, 21 insertions(+), 183 deletions(-) delete mode 100644 MacWidget/MacWidget.swift delete mode 100644 PhoneWidget/PhoneWidget.swift delete mode 100644 PhoneWidget/PhoneWidgetBundle.swift rename WatchWidget/WatchWidget.swift => WidgetShared/JustOneThingWidget.swift (80%) diff --git a/JustOneThing.xcodeproj/project.pbxproj b/JustOneThing.xcodeproj/project.pbxproj index f991fa1..078b9b7 100644 --- a/JustOneThing.xcodeproj/project.pbxproj +++ b/JustOneThing.xcodeproj/project.pbxproj @@ -16,24 +16,23 @@ 547201902A36563C005B2FCC /* Widget.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = 54928C1C2A35F89900095445 /* Widget.intentdefinition */; }; 547201912A36563C005B2FCC /* WidgetAssets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 54928C372A35F8B800095445 /* WidgetAssets.xcassets */; }; 547201932A365868005B2FCC /* Widget.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = 54928C1C2A35F89900095445 /* Widget.intentdefinition */; }; + 547201962A461DBE005B2FCC /* JustOneThingWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54928C4A2A35F8CB00095445 /* JustOneThingWidget.swift */; }; + 547201972A461DBE005B2FCC /* JustOneThingWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54928C4A2A35F8CB00095445 /* JustOneThingWidget.swift */; }; 5483903B2A2A773900BF5624 /* JustOneThingApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5483903A2A2A773900BF5624 /* JustOneThingApp.swift */; }; 5483903D2A2A773900BF5624 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5483903C2A2A773900BF5624 /* ContentView.swift */; }; 5483903F2A2A773A00BF5624 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5483903E2A2A773A00BF5624 /* Assets.xcassets */; }; 54928C162A35F89900095445 /* WidgetKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 54928C152A35F89900095445 /* WidgetKit.framework */; }; 54928C182A35F89900095445 /* SwiftUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 54928C172A35F89900095445 /* SwiftUI.framework */; }; - 54928C1B2A35F89900095445 /* MacWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54928C1A2A35F89900095445 /* MacWidget.swift */; }; 54928C212A35F89A00095445 /* Widget.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = 54928C1C2A35F89900095445 /* Widget.intentdefinition */; }; 54928C222A35F89A00095445 /* Widget.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = 54928C1C2A35F89900095445 /* Widget.intentdefinition */; }; 54928C252A35F89A00095445 /* MacWidgetExtension.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = 54928C132A35F89900095445 /* MacWidgetExtension.appex */; platformFilters = (macos, ); settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; 54928C2F2A35F8B800095445 /* WidgetKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 54928C152A35F89900095445 /* WidgetKit.framework */; }; 54928C302A35F8B800095445 /* SwiftUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 54928C172A35F89900095445 /* SwiftUI.framework */; }; - 54928C332A35F8B800095445 /* PhoneWidgetBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54928C322A35F8B800095445 /* PhoneWidgetBundle.swift */; }; - 54928C352A35F8B800095445 /* PhoneWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54928C342A35F8B800095445 /* PhoneWidget.swift */; }; 54928C382A35F8B800095445 /* WidgetAssets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 54928C372A35F8B800095445 /* WidgetAssets.xcassets */; }; 54928C3E2A35F8B800095445 /* PhoneWidgetExtension.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = 54928C2E2A35F8B800095445 /* PhoneWidgetExtension.appex */; platformFilter = ios; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; 54928C472A35F8CB00095445 /* WidgetKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 54928C152A35F89900095445 /* WidgetKit.framework */; }; 54928C482A35F8CB00095445 /* SwiftUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 54928C172A35F89900095445 /* SwiftUI.framework */; }; - 54928C4B2A35F8CB00095445 /* WatchWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54928C4A2A35F8CB00095445 /* WatchWidget.swift */; }; + 54928C4B2A35F8CB00095445 /* JustOneThingWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54928C4A2A35F8CB00095445 /* JustOneThingWidget.swift */; }; 54928C542A35F8CC00095445 /* WatchWidgetExtension.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = 54928C462A35F8CB00095445 /* WatchWidgetExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; /* End PBXBuildFile section */ @@ -118,16 +117,13 @@ 54928C132A35F89900095445 /* MacWidgetExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = MacWidgetExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; }; 54928C152A35F89900095445 /* WidgetKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WidgetKit.framework; path = System/Library/Frameworks/WidgetKit.framework; sourceTree = SDKROOT; }; 54928C172A35F89900095445 /* SwiftUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftUI.framework; path = System/Library/Frameworks/SwiftUI.framework; sourceTree = SDKROOT; }; - 54928C1A2A35F89900095445 /* MacWidget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacWidget.swift; sourceTree = ""; }; 54928C1C2A35F89900095445 /* Widget.intentdefinition */ = {isa = PBXFileReference; lastKnownFileType = file.intentdefinition; path = Widget.intentdefinition; sourceTree = ""; }; 54928C202A35F89A00095445 /* MacWidget.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = MacWidget.entitlements; sourceTree = ""; }; 54928C2E2A35F8B800095445 /* PhoneWidgetExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = PhoneWidgetExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; }; - 54928C322A35F8B800095445 /* PhoneWidgetBundle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhoneWidgetBundle.swift; sourceTree = ""; }; - 54928C342A35F8B800095445 /* PhoneWidget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhoneWidget.swift; sourceTree = ""; }; 54928C372A35F8B800095445 /* WidgetAssets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = WidgetAssets.xcassets; sourceTree = ""; }; 54928C392A35F8B800095445 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 54928C462A35F8CB00095445 /* WatchWidgetExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = WatchWidgetExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; }; - 54928C4A2A35F8CB00095445 /* WatchWidget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchWidget.swift; sourceTree = ""; }; + 54928C4A2A35F8CB00095445 /* JustOneThingWidget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JustOneThingWidget.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -199,6 +195,7 @@ 54928C372A35F8B800095445 /* WidgetAssets.xcassets */, 54928C392A35F8B800095445 /* Info.plist */, 54928C1C2A35F89900095445 /* Widget.intentdefinition */, + 54928C4A2A35F8CB00095445 /* JustOneThingWidget.swift */, ); path = WidgetShared; sourceTree = ""; @@ -251,7 +248,6 @@ isa = PBXGroup; children = ( 54928C202A35F89A00095445 /* MacWidget.entitlements */, - 54928C1A2A35F89900095445 /* MacWidget.swift */, ); path = MacWidget; sourceTree = ""; @@ -260,8 +256,6 @@ isa = PBXGroup; children = ( 547201942A461554005B2FCC /* PhoneWidget.entitlements */, - 54928C322A35F8B800095445 /* PhoneWidgetBundle.swift */, - 54928C342A35F8B800095445 /* PhoneWidget.swift */, ); path = PhoneWidget; sourceTree = ""; @@ -270,7 +264,6 @@ isa = PBXGroup; children = ( 547201952A461558005B2FCC /* WatchWidget.entitlements */, - 54928C4A2A35F8CB00095445 /* WatchWidget.swift */, ); path = WatchWidget; sourceTree = ""; @@ -488,7 +481,7 @@ buildActionMask = 2147483647; files = ( 54928C212A35F89A00095445 /* Widget.intentdefinition in Sources */, - 54928C1B2A35F89900095445 /* MacWidget.swift in Sources */, + 547201972A461DBE005B2FCC /* JustOneThingWidget.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -496,8 +489,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 54928C352A35F8B800095445 /* PhoneWidget.swift in Sources */, - 54928C332A35F8B800095445 /* PhoneWidgetBundle.swift in Sources */, + 547201962A461DBE005B2FCC /* JustOneThingWidget.swift in Sources */, 5472018E2A36563B005B2FCC /* Widget.intentdefinition in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -507,7 +499,7 @@ buildActionMask = 2147483647; files = ( 547201902A36563C005B2FCC /* Widget.intentdefinition in Sources */, - 54928C4B2A35F8CB00095445 /* WatchWidget.swift in Sources */, + 54928C4B2A35F8CB00095445 /* JustOneThingWidget.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/MacWidget/MacWidget.swift b/MacWidget/MacWidget.swift deleted file mode 100644 index 969134b..0000000 --- a/MacWidget/MacWidget.swift +++ /dev/null @@ -1,69 +0,0 @@ -// -// MacWidget.swift -// MacWidget -// -// Created by Alan Francis on 11/06/2023. -// - -import WidgetKit -import SwiftUI -import Intents - -struct Provider: IntentTimelineProvider { - func placeholder(in context: Context) -> SimpleEntry { - SimpleEntry(date: Date(), configuration: ConfigurationIntent()) - } - - func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) { - let entry = SimpleEntry(date: Date(), configuration: configuration) - completion(entry) - } - - func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline) -> ()) { - var entries: [SimpleEntry] = [] - - // 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 = SimpleEntry(date: entryDate, configuration: configuration) - entries.append(entry) - } - - let timeline = Timeline(entries: entries, policy: .atEnd) - completion(timeline) - } -} - -struct SimpleEntry: TimelineEntry { - let date: Date - let configuration: ConfigurationIntent -} - -struct MacWidgetEntryView : View { - var entry: Provider.Entry - - var body: some View { - Text(entry.date, style: .time) - } -} - -@main -struct MacWidget: Widget { - let kind: String = "MacWidget" - - var body: some WidgetConfiguration { - IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in - MacWidgetEntryView(entry: entry) - } - .configurationDisplayName("My Widget") - .description("This is an example widget.") - } -} - -struct MacWidget_Previews: PreviewProvider { - static var previews: some View { - MacWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent())) - .previewContext(WidgetPreviewContext(family: .systemSmall)) - } -} diff --git a/PhoneWidget/PhoneWidget.swift b/PhoneWidget/PhoneWidget.swift deleted file mode 100644 index 0cea054..0000000 --- a/PhoneWidget/PhoneWidget.swift +++ /dev/null @@ -1,68 +0,0 @@ -// -// PhoneWidget.swift -// PhoneWidget -// -// Created by Alan Francis on 11/06/2023. -// - -import WidgetKit -import SwiftUI -import Intents - -struct Provider: IntentTimelineProvider { - func placeholder(in context: Context) -> SimpleEntry { - SimpleEntry(date: Date(), configuration: ConfigurationIntent()) - } - - func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) { - let entry = SimpleEntry(date: Date(), configuration: configuration) - completion(entry) - } - - func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline) -> ()) { - var entries: [SimpleEntry] = [] - - // 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 = SimpleEntry(date: entryDate, configuration: configuration) - entries.append(entry) - } - - let timeline = Timeline(entries: entries, policy: .atEnd) - completion(timeline) - } -} - -struct SimpleEntry: TimelineEntry { - let date: Date - let configuration: ConfigurationIntent -} - -struct PhoneWidgetEntryView : View { - var entry: Provider.Entry - - var body: some View { - Text(entry.date, style: .time) - } -} - -struct PhoneWidget: Widget { - let kind: String = "PhoneWidget" - - var body: some WidgetConfiguration { - IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in - PhoneWidgetEntryView(entry: entry) - } - .configurationDisplayName("My Widget") - .description("This is an example widget.") - } -} - -struct PhoneWidget_Previews: PreviewProvider { - static var previews: some View { - PhoneWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent())) - .previewContext(WidgetPreviewContext(family: .systemSmall)) - } -} diff --git a/PhoneWidget/PhoneWidgetBundle.swift b/PhoneWidget/PhoneWidgetBundle.swift deleted file mode 100644 index 6c0d041..0000000 --- a/PhoneWidget/PhoneWidgetBundle.swift +++ /dev/null @@ -1,16 +0,0 @@ -// -// PhoneWidgetBundle.swift -// PhoneWidget -// -// Created by Alan Francis on 11/06/2023. -// - -import WidgetKit -import SwiftUI - -@main -struct PhoneWidgetBundle: WidgetBundle { - var body: some Widget { - PhoneWidget() - } -} diff --git a/WatchWidget/WatchWidget.swift b/WidgetShared/JustOneThingWidget.swift similarity index 80% rename from WatchWidget/WatchWidget.swift rename to WidgetShared/JustOneThingWidget.swift index 1b54594..ccf8e51 100644 --- a/WatchWidget/WatchWidget.swift +++ b/WidgetShared/JustOneThingWidget.swift @@ -1,6 +1,6 @@ // -// WatchWidget.swift -// WatchWidget +// JustOneThingWidget.swift +// JustOneThing // // Created by Alan Francis on 11/06/2023. // @@ -9,6 +9,13 @@ import WidgetKit import SwiftUI import Intents +@main +struct JustOneThingWidgetBundle: WidgetBundle { + var body: some Widget { + JustOneThingWidget() + } +} + struct Provider: IntentTimelineProvider { func placeholder(in context: Context) -> SimpleEntry { SimpleEntry(date: Date(), configuration: ConfigurationIntent()) @@ -46,7 +53,7 @@ struct SimpleEntry: TimelineEntry { let configuration: ConfigurationIntent } -struct WatchWidgetEntryView : View { +struct JustOneThingWidgetEntryView : View { var entry: Provider.Entry var body: some View { @@ -54,22 +61,14 @@ struct WatchWidgetEntryView : View { } } -@main -struct WatchWidget: Widget { - let kind: String = "WatchWidget" +struct JustOneThingWidget: Widget { + let kind: String = "JustOneThingWidget" var body: some WidgetConfiguration { IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in - WatchWidgetEntryView(entry: entry) + JustOneThingWidgetEntryView(entry: entry) } .configurationDisplayName("My Widget") .description("This is an example widget.") } } - -struct WatchWidget_Previews: PreviewProvider { - static var previews: some View { - WatchWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent())) - .previewContext(WidgetPreviewContext(family: .accessoryRectangular)) - } -}