Files
omnivore/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift
Jackson Harper 03766734e5 Pull out popup view and replace with Transmission snackbars
PopupView and Transimission can interfere with each other and
cause an issue with the root screen becoming black and the app
getting stuck.
2024-02-06 11:43:43 +08:00

66 lines
2.0 KiB
Swift

import CoreData
import Foundation
import Models
import SwiftGraphQL
import Utils
extension DataService {
public func archiveLink(objectID: NSManagedObjectID, archived: Bool) {
// Update CoreData
backgroundContext.perform { [weak self] in
guard let self = self else { return }
guard let linkedItem = self.backgroundContext.object(with: objectID) as? LibraryItem else { return }
linkedItem.update(inContext: self.backgroundContext, newIsArchivedValue: archived)
// Send update to server
self.syncLinkArchiveStatus(itemID: linkedItem.unwrappedID, archived: archived)
}
}
func syncLinkArchiveStatus(itemID: String, archived: Bool) {
enum MutationResult {
case success(linkId: String)
case error(errorCode: Enums.ArchiveLinkErrorCode)
}
let selection = Selection<MutationResult, Unions.ArchiveLinkResult> {
try $0.on(
archiveLinkError: .init { .error(errorCode: try $0.errorCodes().first ?? .badRequest) },
archiveLinkSuccess: .init { .success(linkId: try $0.linkId()) }
)
}
let mutation = Selection.Mutation {
try $0.setLinkArchived(
input: InputObjects.ArchiveLinkInput(
archived: archived,
linkId: itemID
),
selection: selection
)
}
let path = appEnvironment.graphqlPath
let headers = networker.defaultHeaders
let context = backgroundContext
send(mutation, to: path, headers: headers) { result in
let data = try? result.get()
let syncStatus: ServerSyncStatus = data == nil ? .needsUpdate : .isNSync
context.perform {
guard let linkedItem = LibraryItem.lookup(byID: itemID, inContext: context) else { return }
linkedItem.serverSyncStatus = Int64(syncStatus.rawValue)
do {
try context.save()
logger.debug("LinkedItem updated succesfully")
} catch {
context.rollback()
logger.debug("Failed to update LinkedItem: \(error.localizedDescription)")
}
}
}
}
}