From 6cb6f19e1e7944fab423baf5bb12c9204c8b1c7e Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Fri, 22 Apr 2022 09:59:51 -0700 Subject: [PATCH] add helper functions to internalLabel --- .../Sources/Models/DataModels/FeedItem.swift | 81 ++++++++++--------- .../InternalModels/InternalLinkedItem.swift | 1 + .../InternalLinkedItemLabel.swift | 24 +++++- .../Services/NSNotification+Operation.swift | 7 +- apple/Sources/PushNotificationConfig.swift | 13 +-- 5 files changed, 75 insertions(+), 51 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift index f4cae84a9..e49464a82 100644 --- a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift +++ b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift @@ -71,9 +71,9 @@ public struct FeedItemDep: Identifiable, Hashable { self.labels = labels } - public static func fromJsonArticle(linkData: Data) -> FeedItemDep? { - try? JSONDecoder().decode(JSONArticle.self, from: linkData).feedItem - } +// public static func fromJsonArticle(linkData: Data) -> FeedItemDe-----p? { +// try? JSONDecoder().decode(JSONArticle.self, from: linkData).feedItem +// } public var isRead: Bool { readingProgress >= 0.98 @@ -101,43 +101,44 @@ public struct FeedItemDep: Identifiable, Hashable { } } -/// 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: FeedItemDep { - FeedItemDep( - 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: [] - ) - } -} +// TODO: 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-----Dep { +// FeedItem--------Dep( +// 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 extension FeedItemDep { func asManagedObject(inContext context: NSManagedObjectContext) -> LinkedItem { diff --git a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItem.swift b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItem.swift index 7c986944e..1e1a06ce7 100644 --- a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItem.swift +++ b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItem.swift @@ -75,6 +75,7 @@ struct InternalLinkedItem { } extension Sequence where Element == InternalLinkedItem { + // TODO: use batch update? func persist(context: NSManagedObjectContext) -> [LinkedItem]? { let linkedItems = map { $0.asManagedObject(inContext: context) } diff --git a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItemLabel.swift b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItemLabel.swift index 511d5ee92..cd243f606 100644 --- a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItemLabel.swift +++ b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItemLabel.swift @@ -17,11 +17,11 @@ struct InternalLinkedItemLabel { do { try context.save() - logger.debug("NewsletterEmail saved succesfully") + logger.debug("LinkedItemLabel saved succesfully") objectID = label.objectID } catch { context.rollback() - logger.debug("Failed to save NewsletterEmail: \(error.localizedDescription)") + logger.debug("Failed to save LinkedItemLabel: \(error.localizedDescription)") } } @@ -56,3 +56,23 @@ extension LinkedItemLabel { return label } } + +extension Sequence where Element == InternalLinkedItemLabel { + func persist(context: NSManagedObjectContext) -> [NSManagedObjectID]? { + var result: [NSManagedObjectID]? + + context.performAndWait { + let labels = map { $0.asManagedObject(inContext: context) } + do { + try context.save() + logger.debug("labels saved succesfully") + result = labels.map(\.objectID) + } catch { + context.rollback() + logger.debug("Failed to save labels: \(error.localizedDescription)") + } + } + + return result + } +} diff --git a/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift b/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift index 0aad9b201..af6a14bbf 100644 --- a/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift +++ b/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift @@ -32,9 +32,10 @@ public extension NSNotification { return nil } - static func pushFeedItem(feedItem: FeedItemDep) { - NotificationCenter.default.post(name: NSNotification.PushFeedItem, object: nil, userInfo: ["feedItem": feedItem]) - } + // TODO: re-enable later +// static func pushFeedItem(feedItem: FeedItem-----Dep) { +// NotificationCenter.default.post(name: NSNotification.PushFeedItem, object: nil, userInfo: ["feedItem": feedItem]) +// } 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 96e68e557..1a49bb280 100644 --- a/apple/Sources/PushNotificationConfig.swift +++ b/apple/Sources/PushNotificationConfig.swift @@ -55,12 +55,13 @@ extension AppDelegate: UNUserNotificationCenterDelegate { withCompletionHandler completionHandler: @escaping () -> Void ) { let userInfo = response.notification.request.content.userInfo - if let linkData = userInfo["link"] as? String { - guard let jsonData = Data(base64Encoded: linkData) else { return } - if let item = FeedItemDep.fromJsonArticle(linkData: jsonData) { - NSNotification.pushFeedItem(feedItem: item) - } - } + // TODO: fix +// if let linkData = userInfo["link"] as? String { +// guard let jsonData = Data(base64Encoded: linkData) else { return } +// if let item = FeedItemDep.fromJsonArticle(linkData: jsonData) { +// NSNotification.pushFeedItem(feedItem: item) +// } +// } print(userInfo) completionHandler()