use coredata highlight model in some places

This commit is contained in:
Satindar Dhillon 2022-04-18 22:11:50 -07:00
parent a7a048c3aa
commit 1b4c871276
4 changed files with 37 additions and 9 deletions

View file

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

View file

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

View file

@ -11,7 +11,7 @@ public extension DataService {
patch: String,
articleId: String,
annotation: String? = nil
) -> AnyPublisher<HighlightDep, BasicError> {
) -> AnyPublisher<Highlight, BasicError> {
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)))
}

View file

@ -12,7 +12,7 @@ public extension DataService {
patch: String,
articleId: String,
overlapHighlightIdList: [String]
) -> AnyPublisher<HighlightDep, BasicError> {
) -> AnyPublisher<Highlight, BasicError> {
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)))
}