From eaba4b12bd73a24afa7321b4bfc563e3d1b5c74e Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Sat, 14 May 2022 20:56:58 -0400 Subject: [PATCH] Add Core Data --- Model.xcdatamodeld/Model.xcdatamodel/contents | 10 +++++ ntfy.xcodeproj/project.pbxproj | 35 ++++++++++++--- ntfy/{ntfyApp.swift => App.swift} | 4 +- ntfy/AppDelegate.swift | 8 ++-- ntfy/ContentView.swift | 3 +- ntfy/DataController.swift | 21 +++++++++ ntfy/SubscriptionsList.swift | 43 +++++++++++++++++++ 7 files changed, 111 insertions(+), 13 deletions(-) create mode 100644 Model.xcdatamodeld/Model.xcdatamodel/contents rename ntfy/{ntfyApp.swift => App.swift} (70%) create mode 100644 ntfy/DataController.swift create mode 100644 ntfy/SubscriptionsList.swift diff --git a/Model.xcdatamodeld/Model.xcdatamodel/contents b/Model.xcdatamodeld/Model.xcdatamodel/contents new file mode 100644 index 0000000..a4a8bc6 --- /dev/null +++ b/Model.xcdatamodeld/Model.xcdatamodel/contents @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/ntfy.xcodeproj/project.pbxproj b/ntfy.xcodeproj/project.pbxproj index 86d1d27..5a06bbb 100644 --- a/ntfy.xcodeproj/project.pbxproj +++ b/ntfy.xcodeproj/project.pbxproj @@ -7,7 +7,7 @@ objects = { /* Begin PBXBuildFile section */ - 9474F1C1282F2AA700CDE4DD /* ntfyApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9474F1C0282F2AA700CDE4DD /* ntfyApp.swift */; }; + 9474F1C1282F2AA700CDE4DD /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9474F1C0282F2AA700CDE4DD /* App.swift */; }; 9474F1C3282F2AA700CDE4DD /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9474F1C2282F2AA700CDE4DD /* ContentView.swift */; }; 9474F1C5282F2AA800CDE4DD /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9474F1C4282F2AA800CDE4DD /* Assets.xcassets */; }; 9474F1C8282F2AA800CDE4DD /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9474F1C7282F2AA800CDE4DD /* Preview Assets.xcassets */; }; @@ -16,6 +16,9 @@ 9474F1DC282F30B500CDE4DD /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 9474F1DB282F30B500CDE4DD /* GoogleService-Info.plist */; }; 9474F1E7282F3FFD00CDE4DD /* NotificationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9474F1E6282F3FFD00CDE4DD /* NotificationService.swift */; }; 9474F1EB282F3FFD00CDE4DD /* ntfyNSE.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 9474F1E4282F3FFD00CDE4DD /* ntfyNSE.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; + 9474F1F22830825600CDE4DD /* SubscriptionsList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9474F1F12830825600CDE4DD /* SubscriptionsList.swift */; }; + 9474F1F72830830700CDE4DD /* Model.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 9474F1F52830830700CDE4DD /* Model.xcdatamodeld */; }; + 9474F1F92830835400CDE4DD /* DataController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9474F1F82830835400CDE4DD /* DataController.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -44,7 +47,7 @@ /* Begin PBXFileReference section */ 9474F1BD282F2AA700CDE4DD /* ntfy.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ntfy.app; sourceTree = BUILT_PRODUCTS_DIR; }; - 9474F1C0282F2AA700CDE4DD /* ntfyApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ntfyApp.swift; sourceTree = ""; }; + 9474F1C0282F2AA700CDE4DD /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = ""; }; 9474F1C2282F2AA700CDE4DD /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 9474F1C4282F2AA800CDE4DD /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 9474F1C7282F2AA800CDE4DD /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; @@ -55,6 +58,9 @@ 9474F1E4282F3FFD00CDE4DD /* ntfyNSE.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = ntfyNSE.appex; sourceTree = BUILT_PRODUCTS_DIR; }; 9474F1E6282F3FFD00CDE4DD /* NotificationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationService.swift; sourceTree = ""; }; 9474F1E8282F3FFD00CDE4DD /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 9474F1F12830825600CDE4DD /* SubscriptionsList.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SubscriptionsList.swift; sourceTree = ""; }; + 9474F1F62830830700CDE4DD /* Model.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Model.xcdatamodel; sourceTree = ""; }; + 9474F1F82830835400CDE4DD /* DataController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataController.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -79,6 +85,7 @@ 9474F1B4282F2AA700CDE4DD = { isa = PBXGroup; children = ( + 9474F1F52830830700CDE4DD /* Model.xcdatamodeld */, 9474F1BF282F2AA700CDE4DD /* ntfy */, 9474F1E5282F3FFD00CDE4DD /* ntfyNSE */, 9474F1BE282F2AA700CDE4DD /* Products */, @@ -98,14 +105,16 @@ 9474F1BF282F2AA700CDE4DD /* ntfy */ = { isa = PBXGroup; children = ( + 9474F1C2282F2AA700CDE4DD /* ContentView.swift */, + 9474F1F12830825600CDE4DD /* SubscriptionsList.swift */, 9474F1D6282F2FF700CDE4DD /* Info.plist */, 9474F1D5282F2FED00CDE4DD /* ntfy.entitlements */, - 9474F1C0282F2AA700CDE4DD /* ntfyApp.swift */, - 9474F1C2282F2AA700CDE4DD /* ContentView.swift */, + 9474F1C0282F2AA700CDE4DD /* App.swift */, 9474F1DB282F30B500CDE4DD /* GoogleService-Info.plist */, 9474F1C4282F2AA800CDE4DD /* Assets.xcassets */, 9474F1C6282F2AA800CDE4DD /* Preview Content */, 9474F1D1282F2D2C00CDE4DD /* AppDelegate.swift */, + 9474F1F82830835400CDE4DD /* DataController.swift */, ); path = ntfy; sourceTree = ""; @@ -241,9 +250,12 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 9474F1F92830835400CDE4DD /* DataController.swift in Sources */, 9474F1D2282F2D2C00CDE4DD /* AppDelegate.swift in Sources */, 9474F1C3282F2AA700CDE4DD /* ContentView.swift in Sources */, - 9474F1C1282F2AA700CDE4DD /* ntfyApp.swift in Sources */, + 9474F1F22830825600CDE4DD /* SubscriptionsList.swift in Sources */, + 9474F1C1282F2AA700CDE4DD /* App.swift in Sources */, + 9474F1F72830830700CDE4DD /* Model.xcdatamodeld in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -546,6 +558,19 @@ productName = FirebaseMessaging; }; /* End XCSwiftPackageProductDependency section */ + +/* Begin XCVersionGroup section */ + 9474F1F52830830700CDE4DD /* Model.xcdatamodeld */ = { + isa = XCVersionGroup; + children = ( + 9474F1F62830830700CDE4DD /* Model.xcdatamodel */, + ); + currentVersion = 9474F1F62830830700CDE4DD /* Model.xcdatamodel */; + path = Model.xcdatamodeld; + sourceTree = ""; + versionGroupType = wrapper.xcdatamodel; + }; +/* End XCVersionGroup section */ }; rootObject = 9474F1B5282F2AA700CDE4DD /* Project object */; } diff --git a/ntfy/ntfyApp.swift b/ntfy/App.swift similarity index 70% rename from ntfy/ntfyApp.swift rename to ntfy/App.swift index 9aba019..19fc3b7 100644 --- a/ntfy/ntfyApp.swift +++ b/ntfy/App.swift @@ -12,7 +12,8 @@ import Firebase @main struct ntfyApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate: AppDelegate - + @StateObject private var dataController = DataController() + init() { FirebaseApp.configure() } @@ -20,6 +21,7 @@ struct ntfyApp: App { var body: some Scene { WindowGroup { ContentView() + .environment(\.managedObjectContext, dataController.container.viewContext) } } } diff --git a/ntfy/AppDelegate.swift b/ntfy/AppDelegate.swift index e670d13..537396f 100644 --- a/ntfy/AppDelegate.swift +++ b/ntfy/AppDelegate.swift @@ -4,11 +4,7 @@ import UserNotifications import Firebase import FirebaseCore - -enum Identifiers { - static let viewAction = "VIEW_IDENTIFIER" - static let newsCategory = "NEWS_CATEGORY" -} +// https://stackoverflow.com/a/41783666/1440785 class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? @@ -22,6 +18,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // FirebaseApp.configure() DOES NOT WORK FirebaseConfiguration.shared.setLoggerLevel(.max) Messaging.messaging().delegate = self + + Messaging.messaging().subscribe(toTopic: "philtest") registerForPushNotifications() UNUserNotificationCenter.current().delegate = self diff --git a/ntfy/ContentView.swift b/ntfy/ContentView.swift index 584a6eb..97c4a4b 100644 --- a/ntfy/ContentView.swift +++ b/ntfy/ContentView.swift @@ -9,8 +9,7 @@ import SwiftUI struct ContentView: View { var body: some View { - Text("Hello, world!") - .padding() + SubscriptionsList() } } diff --git a/ntfy/DataController.swift b/ntfy/DataController.swift new file mode 100644 index 0000000..72fb84b --- /dev/null +++ b/ntfy/DataController.swift @@ -0,0 +1,21 @@ +// +// DataController.swift +// ntfy +// +// Created by Philipp Heckel on 5/14/22. +// + +import Foundation +import CoreData + +class DataController: ObservableObject { + let container = NSPersistentContainer(name: "Model") + + init() { + container.loadPersistentStores { description, error in + if let error = error { + print("Core Data failed to load: \(error.localizedDescription)") + } + } + } +} diff --git a/ntfy/SubscriptionsList.swift b/ntfy/SubscriptionsList.swift new file mode 100644 index 0000000..e9adc87 --- /dev/null +++ b/ntfy/SubscriptionsList.swift @@ -0,0 +1,43 @@ +// +// ContentView.swift +// ntfy-ios +// +// Created by Andrew Cope on 1/15/22. +// + +// https://www.hackingwithswift.com/books/ios-swiftui/how-to-combine-core-data-and-swiftui + +import SwiftUI + +struct SubscriptionsList: View { + @Environment(\.managedObjectContext) var context + @FetchRequest(sortDescriptors: []) var subscriptions: FetchedResults + + var body: some View { + VStack { + List(subscriptions) { subscription in + Text("\(subscription.topic ?? "")") + } + Button("Add") { + let firstNames = ["Ginny", "Harry", "Hermione", "Luna", "Ron"] + let chosenFirstName = firstNames.randomElement()! + + let subscription = Subscription(context: context) + subscription.baseUrl = "https://ntfy.sh" + subscription.topic = chosenFirstName + try? context.save() + } + } + } +} + +/* +struct SubscriptionsList_Previews: PreviewProvider { + static var previews: some View { + SubscriptionsList( + subscriptions: NtfySubscriptionList, + currentView: (.subscriptionList) + ) + } +} +*/