From 753eb56542ceae4adba77c41550e2b90b88ebbd5 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Sat, 23 Apr 2022 22:09:12 -0700 Subject: [PATCH] fix linkeditem creation from push notifications --- .../App/Views/Home/HomeFeedViewIOS.swift | 14 ++-- .../Sources/Models/DataModels/FeedItem.swift | 50 ++++--------- .../InternalModels/InternalLinkedItem.swift | 70 +++++++++++++------ .../Services/NSNotification+Operation.swift | 15 ++-- apple/Sources/PushNotificationConfig.swift | 16 ++--- 5 files changed, 84 insertions(+), 81 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift index 3b343f115..bc4b757ac 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift @@ -55,13 +55,13 @@ import Views viewModel.loadItems(dataService: dataService, isRefresh: true) } } - // TODO: -push-notification fix this -// .onReceive(NotificationCenter.default.publisher(for: Notification.Name("PushFeedItem"))) { notification in -// if let feedItem = notification.userInfo?["feedItem"] as? FeedItemD---ep { -// viewModel.pushFeedItem(item: feedItem) -// viewModel.selectedLinkItem = feedItem -// } -// } + .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? LinkedItem else { return } + viewModel.pushFeedItem(item: linkedItem) + viewModel.selectedLinkItem = linkedItem + } .formSheet(isPresented: $viewModel.snoozePresented) { SnoozeView(snoozePresented: $viewModel.snoozePresented, itemToSnoozeID: $viewModel.itemToSnoozeID) { viewModel.snoozeUntil( diff --git a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift index 328885e9e..3df45fda1 100644 --- a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift +++ b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift @@ -11,44 +11,20 @@ public struct HomeFeedData { } } -// TODO: -push-notification delete this? // Internal model used for parsing a push notification object only -// struct JSONArticle: Decodable { -// let id: String -// let title: String -// let createdAt: Date -// let savedAt: Date -// let image: String -// let readingProgressPercent: Double -// let readingProgressAnchorIndex: Int -// let slug: String -// let contentReader: String -// let url: String -// let isArchived: Bool -// -// var feedItem: FeedItem-----De---p { -// FeedItem--------De----p( -// id: id, -// title: title, -// createdAt: createdAt, -// savedAt: savedAt, -// readingProgress: readingProgressPercent, -// readingProgressAnchor: readingProgressAnchorIndex, -// imageURLString: image, -// onDeviceImageURLString: nil, -// documentDirectoryPath: nil, -// pageURLString: url, -// descriptionText: title, -// publisherURLString: nil, -// author: nil, -// publishDate: nil, -// slug: slug, -// isArchived: isArchived, -// contentReader: contentReader, -// labels: [] -// ) -// } -// } +public struct JSONArticle: Decodable { + public let id: String + public let title: String + public let createdAt: Date + public let savedAt: Date + 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 extension LinkedItem { var unwrappedID: String { id ?? "" } diff --git a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItem.swift b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItem.swift index 1526b7802..fb69d1fcb 100644 --- a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItem.swift +++ b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItem.swift @@ -49,29 +49,6 @@ struct InternalLinkedItem { return linkedItem } - - static func make(from item: LinkedItem) -> InternalLinkedItem { - InternalLinkedItem( - id: item.id ?? "", - title: item.title ?? "", - createdAt: item.createdAt ?? Date(), - savedAt: item.savedAt ?? Date(), - readingProgress: item.readingProgress, - readingProgressAnchor: Int(item.readingProgressAnchor), - imageURLString: item.imageURLString, - onDeviceImageURLString: item.onDeviceImageURLString, - documentDirectoryPath: nil, - pageURLString: item.pageURLString ?? "", - descriptionText: item.title, - publisherURLString: item.publisherURLString, - author: item.author, - publishDate: item.publishDate, - slug: item.slug ?? "", - isArchived: item.isArchived, - contentReader: item.contentReader, - labels: [] - ) - } } extension Sequence where Element == InternalLinkedItem { @@ -92,3 +69,50 @@ extension Sequence where Element == InternalLinkedItem { return linkedItems } } + +public extension DataService { + func persist(jsonArticle: JSONArticle) -> NSManagedObjectID? { + jsonArticle.persistAsLinkedItem(context: backgroundContext) + } +} + +extension JSONArticle { + func persistAsLinkedItem(context: NSManagedObjectContext) -> NSManagedObjectID? { + var objectID: NSManagedObjectID? + + let internalLinkedItem = InternalLinkedItem( + id: id, + title: title, + createdAt: createdAt, + savedAt: savedAt, + readingProgress: readingProgressPercent, + readingProgressAnchor: readingProgressAnchorIndex, + imageURLString: image, + onDeviceImageURLString: nil, + documentDirectoryPath: nil, + pageURLString: url, + descriptionText: title, + publisherURLString: nil, + author: nil, + publishDate: nil, + slug: slug, + isArchived: isArchived, + contentReader: contentReader, + labels: [] + ) + + 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 9282dbfbc..0616e8375 100644 --- a/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift +++ b/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift @@ -9,12 +9,12 @@ import Foundation import Models public extension NSNotification { - static let PushFeedItem = Notification.Name("PushFeedItem") + static let PushJSONArticle = Notification.Name("PushJSONArticle") static let OperationSuccess = Notification.Name("OperationSuccess") static let OperationFailure = Notification.Name("OperationFailure") static var pushFeedItemPublisher: NotificationCenter.Publisher { - NotificationCenter.default.publisher(for: PushFeedItem) + NotificationCenter.default.publisher(for: PushJSONArticle) } static var operationSuccessPublisher: NotificationCenter.Publisher { @@ -32,10 +32,13 @@ public extension NSNotification { return nil } - // TODO: -push-notification re-enable later -// static func pushFeedItem(feedItem: FeedItem-----D---ep) { -// NotificationCenter.default.post(name: NSNotification.PushFeedItem, object: nil, userInfo: ["feedItem": feedItem]) -// } + static func pushJSONArticle(article: JSONArticle) { + NotificationCenter.default.post( + name: NSNotification.PushJSONArticle, + object: nil, + userInfo: ["article": article] + ) + } static func operationSuccess(message: String) { NotificationCenter.default.post(name: NSNotification.OperationSuccess, object: nil, userInfo: ["message": message]) diff --git a/apple/Sources/PushNotificationConfig.swift b/apple/Sources/PushNotificationConfig.swift index b53f86f72..81496ff40 100644 --- a/apple/Sources/PushNotificationConfig.swift +++ b/apple/Sources/PushNotificationConfig.swift @@ -55,15 +55,15 @@ extension AppDelegate: UNUserNotificationCenterDelegate { withCompletionHandler completionHandler: @escaping () -> Void ) { let userInfo = response.notification.request.content.userInfo - // TODO: -push-notification fix -// if let linkData = userInfo["link"] as? String { -// guard let jsonData = Data(base64Encoded: linkData) else { return } -// if let item = FeedItem---D--ep.fromJsonArticle(linkData: jsonData) { -// NSNotification.pushFeedItem(feedItem: item) -// } -// } - print(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) + } + } + completionHandler() }