From 1b4c87127621188950363605e18c4db6650e2d33 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Mon, 18 Apr 2022 22:11:50 -0700 Subject: [PATCH] use coredata highlight model in some places --- .../App/Views/WebReader/WebReaderViewModel.swift | 10 +++++----- .../Sources/Models/DataModels/HighlightDep.swift | 14 ++++++++++++++ .../DataService/Mutations/CreateHighlight.swift | 11 +++++++++-- .../DataService/Mutations/MergeHighlight.swift | 11 +++++++++-- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderViewModel.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderViewModel.swift index 3895f6b35..3d92e3a0a 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderViewModel.swift @@ -61,8 +61,8 @@ final class WebReaderViewModel: ObservableObject { guard case .failure = completion else { return } replyHandler([], "createHighlight: Error encoding response") } receiveValue: { highlight in - if let highlight = encodeHighlightResult(highlight) { - replyHandler(["result": highlight], nil) + if let highlightValue = encodeHighlightResult(HighlightDep.make(from: highlight)) { + replyHandler(["result": highlightValue], nil) } else { replyHandler([], "createHighlight: Error encoding response") } @@ -104,10 +104,10 @@ final class WebReaderViewModel: ObservableObject { guard case .failure = completion else { return } replyHandler([], "mergeHighlight: Error encoding response") } receiveValue: { highlight in - if let highlight = encodeHighlightResult(highlight) { - replyHandler(["result": highlight], nil) + if let highlightValue = encodeHighlightResult(HighlightDep.make(from: highlight)) { + replyHandler(["result": highlightValue], nil) } else { - replyHandler([], "mergeHighlight: Error encoding response") + replyHandler([], "createHighlight: Error encoding response") } } .store(in: &subscriptions) diff --git a/apple/OmnivoreKit/Sources/Models/DataModels/HighlightDep.swift b/apple/OmnivoreKit/Sources/Models/DataModels/HighlightDep.swift index 67e73bfda..25a659703 100644 --- a/apple/OmnivoreKit/Sources/Models/DataModels/HighlightDep.swift +++ b/apple/OmnivoreKit/Sources/Models/DataModels/HighlightDep.swift @@ -54,6 +54,20 @@ public struct HighlightDep: Identifiable, Hashable, Codable { return highlight } + public func persist(context: NSManagedObjectContext, associatedItemID: String) -> Highlight? { + let highlight = toManagedObject(context: context, associatedItemID: associatedItemID) + + do { + try context.save() + print("Highlight saved succesfully") + return highlight + } catch { + context.rollback() + print("Failed to save Highlight: \(error.localizedDescription)") + return nil + } + } + public static func make(from highlight: Highlight) -> HighlightDep { HighlightDep( id: highlight.id ?? "", diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift index 09d9e112e..dc669f0df 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift @@ -11,7 +11,7 @@ public extension DataService { patch: String, articleId: String, annotation: String? = nil - ) -> AnyPublisher { + ) -> AnyPublisher { enum MutationResult { case saved(highlight: HighlightDep) case error(errorCode: Enums.CreateHighlightErrorCode) @@ -54,7 +54,14 @@ public extension DataService { switch payload.data { case let .saved(highlight: highlight): - promise(.success(highlight)) + if let highlightObject = highlight.persist( + context: self.persistentContainer.viewContext, + associatedItemID: articleId + ) { + promise(.success(highlightObject)) + } else { + promise(.failure(.message(messageText: "core data error"))) + } case let .error(errorCode: errorCode): promise(.failure(.message(messageText: errorCode.rawValue))) } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MergeHighlight.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MergeHighlight.swift index 01d6545c6..7a762c83d 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MergeHighlight.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MergeHighlight.swift @@ -12,7 +12,7 @@ public extension DataService { patch: String, articleId: String, overlapHighlightIdList: [String] - ) -> AnyPublisher { + ) -> AnyPublisher { enum MutationResult { case saved(highlight: HighlightDep) case error(errorCode: Enums.MergeHighlightErrorCode) @@ -58,7 +58,14 @@ public extension DataService { switch payload.data { case let .saved(highlight: highlight): - promise(.success(highlight)) + if let highlightObject = highlight.persist( + context: self.persistentContainer.viewContext, + associatedItemID: articleId + ) { + promise(.success(highlightObject)) + } else { + promise(.failure(.message(messageText: "core data error"))) + } case let .error(errorCode: errorCode): promise(.failure(.message(messageText: errorCode.rawValue))) }