mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Handle response ids when saving items
This will be used when handling pages that change their IDs when saved.
This commit is contained in:
parent
bf2c0d6b66
commit
b107a86ba9
3 changed files with 33 additions and 16 deletions
|
|
@ -110,8 +110,7 @@ public extension DataService {
|
|||
}
|
||||
}
|
||||
|
||||
// swiftlint:disable:next line_length
|
||||
func saveFilePublisher(requestId: String, uploadFileId: String, url: String) async throws {
|
||||
func saveFilePublisher(requestId: String, uploadFileId: String, url: String) async throws -> String? {
|
||||
enum MutationResult {
|
||||
case saved(requestId: String, url: String)
|
||||
case error(errorCode: Enums.SaveErrorCode)
|
||||
|
|
@ -127,7 +126,13 @@ public extension DataService {
|
|||
let selection = Selection<MutationResult, Unions.SaveResult> {
|
||||
try $0.on(
|
||||
saveError: .init { .error(errorCode: (try? $0.errorCodes().first) ?? .unknown) },
|
||||
saveSuccess: .init { .saved(requestId: requestId, url: (try? $0.url()) ?? "") }
|
||||
saveSuccess: .init {
|
||||
if let requestId = try? $0.clientRequestId(), let url = try? $0.url() {
|
||||
return .saved(requestId: requestId, url: url)
|
||||
} else {
|
||||
return .error(errorCode: .unknown)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -148,8 +153,8 @@ public extension DataService {
|
|||
}
|
||||
|
||||
switch payload.data {
|
||||
case .saved:
|
||||
continuation.resume()
|
||||
case let .saved(requestId: requestId, url: _):
|
||||
continuation.resume(returning: requestId)
|
||||
case let .error(errorCode: errorCode):
|
||||
switch errorCode {
|
||||
case .unauthorized:
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import Models
|
|||
import SwiftGraphQL
|
||||
|
||||
public extension DataService {
|
||||
func saveURL(id: String, url: String) async throws {
|
||||
func saveURL(id: String, url: String) async throws -> String? {
|
||||
enum MutationResult {
|
||||
case saved(requestId: String, url: String)
|
||||
case error(errorCode: Enums.SaveErrorCode)
|
||||
|
|
@ -18,7 +18,13 @@ public extension DataService {
|
|||
let selection = Selection<MutationResult, Unions.SaveResult> {
|
||||
try $0.on(
|
||||
saveError: .init { .error(errorCode: (try? $0.errorCodes().first) ?? .unknown) },
|
||||
saveSuccess: .init { .saved(requestId: id, url: (try? $0.url()) ?? "") }
|
||||
saveSuccess: .init {
|
||||
if let requestId = try? $0.clientRequestId(), let url = try? $0.url() {
|
||||
return .saved(requestId: requestId, url: url)
|
||||
} else {
|
||||
return .error(errorCode: .unknown)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -41,8 +47,8 @@ public extension DataService {
|
|||
}
|
||||
|
||||
switch payload.data {
|
||||
case .saved:
|
||||
continuation.resume()
|
||||
case let .saved(requestId: requestId, url: _):
|
||||
continuation.resume(returning: requestId)
|
||||
case let .error(errorCode: errorCode):
|
||||
switch errorCode {
|
||||
case .unauthorized:
|
||||
|
|
|
|||
|
|
@ -35,18 +35,22 @@ public extension DataService {
|
|||
}
|
||||
}
|
||||
|
||||
private func updateLinkedItemStatus(id: String, status: ServerSyncStatus) async throws {
|
||||
private func updateLinkedItemStatus(id: String, newId _: String?, status: ServerSyncStatus) async throws {
|
||||
try backgroundContext.performAndWait {
|
||||
let fetchRequest: NSFetchRequest<Models.LinkedItem> = LinkedItem.fetchRequest()
|
||||
fetchRequest.predicate = NSPredicate(format: "id == %@", id)
|
||||
|
||||
guard let linkedItem = (try? backgroundContext.fetch(fetchRequest))?.first else { return }
|
||||
// TODO: handle item id changes
|
||||
// linkedItem.id =
|
||||
linkedItem.serverSyncStatus = Int64(status.rawValue)
|
||||
}
|
||||
}
|
||||
|
||||
func syncPdf(id: String, localPdfURL: URL, url: String) async throws {
|
||||
do {
|
||||
try await updateLinkedItemStatus(id: id, newId: nil, status: .isSyncing)
|
||||
|
||||
let uploadRequest = try await uploadFileRequest(id: id, url: url)
|
||||
if let urlString = uploadRequest.urlString, let uploadUrl = URL(string: urlString) {
|
||||
try await uploadFile(id: id, localPdfURL: localPdfURL, url: uploadUrl)
|
||||
|
|
@ -55,7 +59,7 @@ public extension DataService {
|
|||
throw SaveArticleError.badData
|
||||
}
|
||||
|
||||
try await updateLinkedItemStatus(id: id, status: .isNSync)
|
||||
try await updateLinkedItemStatus(id: id, newId: nil, status: .isNSync)
|
||||
try backgroundContext.performAndWait {
|
||||
try backgroundContext.save()
|
||||
}
|
||||
|
|
@ -69,14 +73,14 @@ public extension DataService {
|
|||
|
||||
func syncPage(id: String, originalHtml: String, title: String?, url: String) async throws {
|
||||
do {
|
||||
try await updateLinkedItemStatus(id: id, newId: nil, status: .isSyncing)
|
||||
|
||||
let newId = try await savePage(id: id, url: url, title: title ?? url, originalHtml: originalHtml)
|
||||
print("NEW ID FOR ITEM", newId, "FROM OLD ID", id)
|
||||
try await updateLinkedItemStatus(id: id, status: .isNSync)
|
||||
try await updateLinkedItemStatus(id: id, newId: newId, status: .isNSync)
|
||||
try backgroundContext.performAndWait {
|
||||
try backgroundContext.save()
|
||||
}
|
||||
} catch {
|
||||
print("ERROR SYNCING PAGE", error)
|
||||
backgroundContext.performAndWait {
|
||||
backgroundContext.rollback()
|
||||
}
|
||||
|
|
@ -86,8 +90,10 @@ public extension DataService {
|
|||
|
||||
func syncUrl(id: String, url: String) async throws {
|
||||
do {
|
||||
try await updateLinkedItemStatus(id: id, status: .isSyncing)
|
||||
try await saveURL(id: id, url: url)
|
||||
try await updateLinkedItemStatus(id: id, newId: nil, status: .isSyncing)
|
||||
|
||||
let newId = try await saveURL(id: id, url: url)
|
||||
try await updateLinkedItemStatus(id: id, newId: newId, status: .isNSync)
|
||||
try backgroundContext.performAndWait {
|
||||
try backgroundContext.save()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue