diff --git a/AppShared/JustOneThingApp.swift b/AppShared/JustOneThingApp.swift index 9de021d..281468a 100644 --- a/AppShared/JustOneThingApp.swift +++ b/AppShared/JustOneThingApp.swift @@ -9,7 +9,7 @@ import SwiftUI @main struct JustOneThingApp: App { - let persistenceController = PersistenceController.preview // or PersistenceController.shared + let persistenceController = PersistenceController.shared var body: some Scene { WindowGroup { ContentView() diff --git a/JustOneThing.xcodeproj/project.pbxproj b/JustOneThing.xcodeproj/project.pbxproj index c1ff1b5..dc3e241 100644 --- a/JustOneThing.xcodeproj/project.pbxproj +++ b/JustOneThing.xcodeproj/project.pbxproj @@ -17,6 +17,9 @@ 5416C7392A56A867002196EE /* InterfaceExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5416C7372A56A867002196EE /* InterfaceExtensions.swift */; }; 5416C73A2A56A867002196EE /* InterfaceExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5416C7372A56A867002196EE /* InterfaceExtensions.swift */; }; 5431ED7A2A2A77CA00515680 /* JustOneThingWatch.app in Embed Watch Content */ = {isa = PBXBuildFile; fileRef = 5431ED6D2A2A77C900515680 /* JustOneThingWatch.app */; platformFilter = ios; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; + 545A50F32A6335D400CD3924 /* PersistentThingExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 545A50F02A6335D400CD3924 /* PersistentThingExtension.swift */; }; + 545A50F42A6335D400CD3924 /* PersistentThingExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 545A50F02A6335D400CD3924 /* PersistentThingExtension.swift */; }; + 545A50F52A6335D400CD3924 /* PersistentThingExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 545A50F02A6335D400CD3924 /* PersistentThingExtension.swift */; }; 5472018C2A36563A005B2FCC /* WidgetAssets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 54928C372A35F8B800095445 /* WidgetAssets.xcassets */; }; 5472018E2A36563B005B2FCC /* Widget.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = 54928C1C2A35F89900095445 /* Widget.intentdefinition */; }; 547201902A36563C005B2FCC /* Widget.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = 54928C1C2A35F89900095445 /* Widget.intentdefinition */; }; @@ -141,6 +144,7 @@ 5431ED6D2A2A77C900515680 /* JustOneThingWatch.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JustOneThingWatch.app; sourceTree = BUILT_PRODUCTS_DIR; }; 5432A6032A582D5D00FE2351 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 5432A6042A582D6D00FE2351 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 545A50F02A6335D400CD3924 /* PersistentThingExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersistentThingExtension.swift; sourceTree = ""; }; 547201942A461554005B2FCC /* PhoneWidget.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = PhoneWidget.entitlements; sourceTree = ""; }; 547201952A461558005B2FCC /* WatchWidget.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = WatchWidget.entitlements; sourceTree = ""; }; 548390372A2A773900BF5624 /* JustOneThing.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JustOneThing.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -341,6 +345,7 @@ children = ( 54AE283F2A5823D900719A56 /* JustOneThingCloudModel.xcdatamodeld */, 54AE28462A58245700719A56 /* PersistenceController.swift */, + 545A50F02A6335D400CD3924 /* PersistentThingExtension.swift */, ); path = Model; sourceTree = ""; @@ -567,6 +572,7 @@ 54AE28492A58245700719A56 /* PersistenceController.swift in Sources */, 54EB3C1F2A5434A30082B059 /* Thing.swift in Sources */, 54928C212A35F89A00095445 /* Widget.intentdefinition in Sources */, + 545A50F32A6335D400CD3924 /* PersistentThingExtension.swift in Sources */, 5493A81B2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */, 54BD36092A5700480022D6BE /* ThingView.swift in Sources */, 54BD360D2A57020E0022D6BE /* WidgetBundle.swift in Sources */, @@ -590,6 +596,7 @@ 5416C7392A56A867002196EE /* InterfaceExtensions.swift in Sources */, 5472018E2A36563B005B2FCC /* Widget.intentdefinition in Sources */, 54AE284A2A58245700719A56 /* PersistenceController.swift in Sources */, + 545A50F42A6335D400CD3924 /* PersistentThingExtension.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -600,6 +607,7 @@ 54AE284B2A58245700719A56 /* PersistenceController.swift in Sources */, 54BD36132A57021E0022D6BE /* AccessoryWidget.swift in Sources */, 54EB3C212A5434A30082B059 /* Thing.swift in Sources */, + 545A50F52A6335D400CD3924 /* PersistentThingExtension.swift in Sources */, 547201902A36563C005B2FCC /* Widget.intentdefinition in Sources */, 5493A81D2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */, 54BD360B2A5700480022D6BE /* ThingView.swift in Sources */, diff --git a/Model/PersistentThingExtension.swift b/Model/PersistentThingExtension.swift new file mode 100644 index 0000000..c0d3cb9 --- /dev/null +++ b/Model/PersistentThingExtension.swift @@ -0,0 +1,23 @@ +// +// PersistentThingExtension.swift +// JustOneThing +// +// Created by Alan Francis on 15/07/2023. +// + +import CoreData + +extension PersistentThing { + var asThing: Thing { + get { + Thing(text: self.text ?? "[no text]", + createdAt: self.createdAt ?? Date.distantPast) + } + } + + static var request: NSFetchRequest { + get { + return NSFetchRequest(entityName: PersistentThing.entity().name!) + } + } +} diff --git a/WidgetShared/JustOneThingProvider.swift b/WidgetShared/JustOneThingProvider.swift index 264b6df..ba7933a 100644 --- a/WidgetShared/JustOneThingProvider.swift +++ b/WidgetShared/JustOneThingProvider.swift @@ -9,6 +9,25 @@ import WidgetKit import SwiftUI import Intents +class ThingCoreDataStore { + let managedObjectContext = PersistenceController.shared.viewContext + + public var things:[Thing] { + do { + return try managedObjectContext.fetch(PersistentThing.request).map { persistentThing in + persistentThing.asThing + } + } + catch { + return [] + } + } + + public var shuffledThings:[Thing] { + return self.things.shuffled() + } +} + struct ThingDataStore { public var things:[Thing] = [ Thing(text: "Eat", createdAt: Date()), @@ -32,7 +51,7 @@ extension Date { } struct ThingProvider: IntentTimelineProvider { - var thingStore:ThingDataStore = ThingDataStore() + var thingStore = ThingCoreDataStore() func placeholder(in context: Context) -> ThingEntry { ThingEntry(thing: .placeholder)