update highlight publishers to make coredata changes
This commit is contained in:
@ -32,6 +32,7 @@ public final class PDFViewerViewModel: ObservableObject {
|
||||
.store(in: &subscriptions)
|
||||
}
|
||||
|
||||
// TODO: use core data instead
|
||||
private func allHighlights(fetchedHighlights: [HighlightDep]) -> [HighlightDep] {
|
||||
var resultSet = [String: HighlightDep]()
|
||||
|
||||
@ -79,6 +80,7 @@ public final class PDFViewerViewModel: ObservableObject {
|
||||
.store(in: &subscriptions)
|
||||
}
|
||||
|
||||
// TODO: able to delete this now?
|
||||
public func mergeHighlight(
|
||||
shortId: String,
|
||||
highlightID: String,
|
||||
@ -86,22 +88,6 @@ public final class PDFViewerViewModel: ObservableObject {
|
||||
patch: String,
|
||||
overlapHighlightIdList: [String]
|
||||
) {
|
||||
services.dataService.persistHighlight(
|
||||
pdfID: feedItem.id,
|
||||
highlight: HighlightDep(
|
||||
id: highlightID,
|
||||
shortId: shortId,
|
||||
quote: quote,
|
||||
prefix: nil,
|
||||
suffix: nil,
|
||||
patch: patch,
|
||||
annotation: nil,
|
||||
createdByMe: true
|
||||
)
|
||||
)
|
||||
|
||||
removeLocalHighlights(highlightIds: overlapHighlightIdList)
|
||||
|
||||
services.dataService
|
||||
.mergeHighlightPublisher(
|
||||
shortId: shortId,
|
||||
@ -121,8 +107,7 @@ public final class PDFViewerViewModel: ObservableObject {
|
||||
}
|
||||
|
||||
public func removeHighlights(highlightIds: [String]) {
|
||||
removeLocalHighlights(highlightIds: highlightIds)
|
||||
|
||||
// TODO: update function to take an array?
|
||||
highlightIds.forEach { highlightId in
|
||||
services.dataService.deleteHighlightPublisher(highlightId: highlightId)
|
||||
.sink { [weak self] completion in
|
||||
@ -162,8 +147,4 @@ public final class PDFViewerViewModel: ObservableObject {
|
||||
|
||||
return components?.url
|
||||
}
|
||||
|
||||
private func removeLocalHighlights(highlightIds: [String]) {
|
||||
services.dataService.removeHighlights(highlightIds: highlightIds)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import Combine
|
||||
import CoreData
|
||||
import Foundation
|
||||
import Models
|
||||
import SwiftGraphQL
|
||||
@ -42,6 +43,7 @@ public extension DataService {
|
||||
|
||||
switch payload.data {
|
||||
case let .saved(id: id):
|
||||
self.deletePersistedHighlight(objectID: id)
|
||||
promise(.success(id))
|
||||
case let .error(errorCode: errorCode):
|
||||
promise(.failure(.message(messageText: errorCode.rawValue)))
|
||||
@ -55,4 +57,21 @@ public extension DataService {
|
||||
.receive(on: DispatchQueue.main)
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
func deletePersistedHighlight(objectID: String) {
|
||||
let context = persistentContainer.viewContext
|
||||
let fetchRequest: NSFetchRequest<Models.Highlight> = Highlight.fetchRequest()
|
||||
fetchRequest.predicate = NSPredicate(format: "id == %@", objectID)
|
||||
for highlight in (try? context.fetch(fetchRequest)) ?? [] {
|
||||
context.delete(highlight)
|
||||
}
|
||||
|
||||
do {
|
||||
try context.save()
|
||||
print("Highlight deleted succesfully")
|
||||
} catch {
|
||||
context.rollback()
|
||||
print("Failed to delete Highlight: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,7 +60,8 @@ public extension DataService {
|
||||
case let .saved(highlight: highlight):
|
||||
_ = highlight.persist(
|
||||
context: self.persistentContainer.viewContext,
|
||||
associatedItemID: articleId
|
||||
associatedItemID: articleId,
|
||||
oldHighlightsIds: overlapHighlightIdList
|
||||
)
|
||||
promise(.success(highlight.encoded()))
|
||||
case let .error(errorCode: errorCode):
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import Combine
|
||||
import CoreData
|
||||
import Foundation
|
||||
import Models
|
||||
import SwiftGraphQL
|
||||
@ -48,9 +49,14 @@ public extension DataService {
|
||||
|
||||
switch payload.data {
|
||||
case let .saved(highlight: highlight):
|
||||
let context = self.persistentContainer.viewContext
|
||||
let fetchRequest: NSFetchRequest<Models.Highlight> = Highlight.fetchRequest()
|
||||
fetchRequest.predicate = NSPredicate(format: "id == %@", highlight.id)
|
||||
let itemID = (try? context.fetch(fetchRequest))?.first?.linkedItemId ?? ""
|
||||
|
||||
_ = highlight.persist(
|
||||
context: self.persistentContainer.viewContext,
|
||||
associatedItemID: "" // TODO: pass in articleID or just use update core data func
|
||||
associatedItemID: itemID
|
||||
)
|
||||
promise(.success(highlight.id))
|
||||
case let .error(errorCode: errorCode):
|
||||
|
||||
@ -31,9 +31,21 @@ struct InternalHighlight: Encodable {
|
||||
return highlight
|
||||
}
|
||||
|
||||
func persist(context: NSManagedObjectContext, associatedItemID: String) -> Highlight? {
|
||||
func persist(
|
||||
context: NSManagedObjectContext,
|
||||
associatedItemID: String,
|
||||
oldHighlightsIds: [String] = []
|
||||
) -> Highlight? {
|
||||
let highlight = asManagedObject(context: context, associatedItemID: associatedItemID)
|
||||
|
||||
if !oldHighlightsIds.isEmpty {
|
||||
let fetchRequest: NSFetchRequest<Models.Highlight> = Highlight.fetchRequest()
|
||||
fetchRequest.predicate = NSPredicate(format: "id IN %@", oldHighlightsIds)
|
||||
for highlight in (try? context.fetch(fetchRequest)) ?? [] {
|
||||
context.delete(highlight)
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
try context.save()
|
||||
print("Highlight saved succesfully")
|
||||
|
||||
@ -3,6 +3,7 @@ import CoreData
|
||||
import Foundation
|
||||
import Models
|
||||
|
||||
// TODO: possibly remove this file?
|
||||
public extension DataService {
|
||||
func cachedHighlights(pdfID: String) -> [HighlightDep] {
|
||||
let fetchRequest: NSFetchRequest<Models.Highlight> = Highlight.fetchRequest()
|
||||
@ -28,26 +29,4 @@ public extension DataService {
|
||||
print("Failed to save Highlight: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
func removeHighlights(highlightIds: [String]) {
|
||||
for highlightID in highlightIds {
|
||||
deletedHighlightsIDs.insert(highlightID)
|
||||
}
|
||||
|
||||
let fetchRequest: NSFetchRequest<Models.Highlight> = Highlight.fetchRequest()
|
||||
fetchRequest.predicate = NSPredicate(format: "id IN %@", highlightIds)
|
||||
guard let highlights = try? persistentContainer.viewContext.fetch(fetchRequest) else { return }
|
||||
|
||||
for highlight in highlights {
|
||||
highlight.markedForDeletion = true
|
||||
}
|
||||
|
||||
do {
|
||||
try persistentContainer.viewContext.save()
|
||||
print("Highlight(s) updated succesfully")
|
||||
} catch {
|
||||
persistentContainer.viewContext.rollback()
|
||||
print("Failed to update Highlight(s): \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user