mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
use coredata highlight model in some places
This commit is contained in:
parent
a7a048c3aa
commit
1b4c871276
4 changed files with 37 additions and 9 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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 ?? "",
|
||||
|
|
|
|||
|
|
@ -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)))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue