fix linkeditem creation from push notifications

This commit is contained in:
Satindar Dhillon 2022-04-23 22:09:12 -07:00
parent 5425e9b0dc
commit 753eb56542
5 changed files with 84 additions and 81 deletions

View file

@ -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(

View file

@ -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 ?? "" }

View file

@ -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
}
}

View file

@ -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])

View file

@ -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()
}