Parse libraryItemId from notifications
This commit is contained in:
@ -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)
|
||||
|
||||
@ -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 ?? "" }
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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]
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@ -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()
|
||||
|
||||
Reference in New Issue
Block a user