From 9e99b5d7f857cce0e52ec9db16dc8e65e788e68f Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Fri, 8 Mar 2024 20:41:18 +0800 Subject: [PATCH] Parse libraryItemId from notifications --- .../App/Views/Home/HomeFeedViewIOS.swift | 8 +-- .../Sources/Models/DataModels/FeedItem.swift | 21 ------- .../InternalModels/InternalLibraryItem.swift | 59 ------------------- .../Services/NSNotification+Operation.swift | 4 +- apple/Sources/PushNotificationConfig.swift | 8 +-- packages/web/pages/settings/subscriptions.tsx | 3 + 6 files changed, 9 insertions(+), 94 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift index 7baa71f50..120a092e6 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift @@ -325,12 +325,8 @@ struct AnimatingCellHeight: AnimatableModifier { } } .onReceive(NotificationCenter.default.publisher(for: Notification.Name("PushJSONArticle"))) { notification in - guard let jsonArticle = notification.userInfo?["article"] as? JSONArticle else { return } - guard let objectID = dataService.persist(jsonArticle: jsonArticle) else { return } - guard let linkedItem = dataService.viewContext.object(with: objectID) as? Models.LibraryItem else { return } - viewModel.pushFeedItem(item: linkedItem) - viewModel.selectedItem = linkedItem - viewModel.linkIsActive = true + guard let libraryItemId = notification.userInfo?["libraryItemId"] as? String else { return } + viewModel.pushLinkedRequest(request: LinkRequest(id: UUID(), serverID: libraryItemId)) } .sheet(isPresented: $searchPresented) { LibrarySearchView(homeFeedViewModel: self.viewModel) diff --git a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift index 6d887ff74..53aa7a504 100644 --- a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift +++ b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift @@ -50,27 +50,6 @@ public struct LinkedItemAudioProperties { public let startOffset: Double } -// Internal model used for parsing a push notification object only -public struct JSONArticle: Decodable { - public let id: String - public let title: String - public let createdAt: Date - public let updatedAt: Date - public let savedAt: Date - public let readAt: Date? - public let folder: String - public let image: String - public let readingProgressPercent: Double - public let readingProgressAnchorIndex: Int - public let slug: String - public let contentReader: String - public let url: String - public let isArchived: Bool - public let language: String? - public let wordsCount: Int? - public let downloadURL: String -} - public extension LibraryItem { var unwrappedID: String { id ?? "" } var unwrappedSlug: String { slug ?? "" } diff --git a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLibraryItem.swift b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLibraryItem.swift index 3a2a4e0e9..98c0a73db 100644 --- a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLibraryItem.swift +++ b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLibraryItem.swift @@ -126,62 +126,3 @@ extension Sequence where Element == InternalLibraryItem { } } } - -public extension DataService { - func persist(jsonArticle: JSONArticle) -> NSManagedObjectID? { - jsonArticle.persistAsLinkedItem(context: backgroundContext) - } -} - -extension JSONArticle { - func persistAsLinkedItem(context: NSManagedObjectContext) -> NSManagedObjectID? { - var objectID: NSManagedObjectID? - - let internalLinkedItem = InternalLibraryItem( - id: id, - title: title, - createdAt: createdAt, - savedAt: savedAt, - readAt: readAt, - updatedAt: updatedAt, - folder: folder, - state: .succeeded, - readingProgress: readingProgressPercent, - readingProgressAnchor: readingProgressAnchorIndex, - imageURLString: image, - onDeviceImageURLString: nil, - documentDirectoryPath: nil, - pageURLString: url, - descriptionText: title, - publisherURLString: nil, - siteName: nil, - author: nil, - publishDate: nil, - slug: slug, - isArchived: isArchived, - contentReader: contentReader, - htmlContent: nil, - originalHtml: nil, - language: language, - wordsCount: wordsCount, - downloadURL: downloadURL, - recommendations: [], - labels: [], - highlights: [] - ) - - context.performAndWait { - objectID = internalLinkedItem.asManagedObject(inContext: context).objectID - - do { - try context.save() - logger.debug("LinkedItem saved succesfully") - } catch { - context.rollback() - logger.debug("Failed to save LinkedItem: \(error.localizedDescription)") - } - } - - return objectID - } -} diff --git a/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift b/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift index 0ebb8654b..e5454efab 100644 --- a/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift +++ b/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift @@ -61,11 +61,11 @@ public extension NSNotification { return nil } - static func pushJSONArticle(article: JSONArticle) { + static func pushJSONArticle(libraryItemId: String) { NotificationCenter.default.post( name: NSNotification.PushJSONArticle, object: nil, - userInfo: ["article": article] + userInfo: ["libraryItemId": libraryItemId] ) } diff --git a/apple/Sources/PushNotificationConfig.swift b/apple/Sources/PushNotificationConfig.swift index d6588b779..8ce0e6b60 100644 --- a/apple/Sources/PushNotificationConfig.swift +++ b/apple/Sources/PushNotificationConfig.swift @@ -68,12 +68,8 @@ extension AppDelegate: UNUserNotificationCenterDelegate { let userInfo = response.notification.request.content.userInfo - if let linkData = userInfo["link"] as? String { - guard let jsonData = Data(base64Encoded: linkData) else { return } - - if let article = try? JSONDecoder().decode(JSONArticle.self, from: jsonData) { - NSNotification.pushJSONArticle(article: article) - } + if let libraryItemId = userInfo["libraryItemId"] as? String { + NSNotification.pushJSONArticle(libraryItemId: libraryItemId) } completionHandler() diff --git a/packages/web/pages/settings/subscriptions.tsx b/packages/web/pages/settings/subscriptions.tsx index d10b4453b..321636171 100644 --- a/packages/web/pages/settings/subscriptions.tsx +++ b/packages/web/pages/settings/subscriptions.tsx @@ -120,6 +120,9 @@ export default function SubscriptionsPage(): JSX.Element { onAccept={async () => { await onUnsubscribe(confirmUnsubscribeSubscription) setConfirmUnsubscribeSubscription(null) + setTimeout(() => { + document.body.style.removeProperty('pointer-events') + }, 200) }} onOpenChange={() => setConfirmUnsubscribeSubscription(null)} />