From 3667595f4877a960496113834bfda3c56e733c85 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 28 Dec 2023 15:46:57 +0800 Subject: [PATCH] Sync move operation, attempt to sync before home is loaded --- .../Home/Components/LibraryItemFetcher.swift | 1 - .../Sources/App/Views/RootView/RootView.swift | 3 ++ .../Models/DataModels/ServerSyncStatus.swift | 1 - .../DataService/Mutations/MoveItem.swift | 33 +++++++++---------- .../Services/DataService/OfflineSync.swift | 16 +++------ .../Queries/LinkedItemNetworkQuery.swift | 2 +- .../Sources/Views/SyncingIcon.swift | 4 +-- 7 files changed, 27 insertions(+), 33 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/Components/LibraryItemFetcher.swift b/apple/OmnivoreKit/Sources/App/Views/Home/Components/LibraryItemFetcher.swift index 5b78e6c92..0b8ebc851 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/Components/LibraryItemFetcher.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/Components/LibraryItemFetcher.swift @@ -26,7 +26,6 @@ import Views var receivedIdx = 0 func setItems(_ context: NSManagedObjectContext, _ items: [Models.LibraryItem]) { - print("setting items, old count", self.items.count, "new count", items.count) self.items = items if let filter = FeaturedItemFilter(rawValue: featureFilter) { diff --git a/apple/OmnivoreKit/Sources/App/Views/RootView/RootView.swift b/apple/OmnivoreKit/Sources/App/Views/RootView/RootView.swift index 7f2c9c461..62a77584a 100644 --- a/apple/OmnivoreKit/Sources/App/Views/RootView/RootView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/RootView/RootView.swift @@ -51,6 +51,9 @@ struct InnerRootView: View { @ViewBuilder private var innerBody: some View { if authenticator.isLoggedIn { PrimaryContentView() + .task { + try? await dataService.syncOfflineItemsWithServerIfNeeded() + } } else { if authenticator.isLoggingOut { LogoutView() diff --git a/apple/OmnivoreKit/Sources/Models/DataModels/ServerSyncStatus.swift b/apple/OmnivoreKit/Sources/Models/DataModels/ServerSyncStatus.swift index 43aa729f2..f911cc4de 100644 --- a/apple/OmnivoreKit/Sources/Models/DataModels/ServerSyncStatus.swift +++ b/apple/OmnivoreKit/Sources/Models/DataModels/ServerSyncStatus.swift @@ -6,5 +6,4 @@ public enum ServerSyncStatus: Int { case needsDeletion case needsCreation case needsUpdate - case needsMove } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MoveItem.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MoveItem.swift index 0f8b74fe0..ca28aef3f 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MoveItem.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MoveItem.swift @@ -20,10 +20,10 @@ public extension DataService { } } - try await syncMoveToFolder(itemID: itemID, folder: folder) + syncMoveToFolder(itemID: itemID, folder: folder) } - func syncMoveToFolder(itemID: String, folder: String) async throws { + func syncMoveToFolder(itemID: String, folder: String) { enum MutationResult { case result(success: Bool) case error(errorMessage: String) @@ -48,23 +48,22 @@ public extension DataService { let path = appEnvironment.graphqlPath let headers = networker.defaultHeaders + let context = backgroundContext - return try await withCheckedThrowingContinuation { continuation in - send(mutation, to: path, headers: headers) { queryResult in - guard let payload = try? queryResult.get() else { - continuation.resume(throwing: BasicError.message(messageText: "network error")) - return - } + send(mutation, to: path, headers: headers) { queryResult in + let data = try? queryResult.get() + let syncStatus: ServerSyncStatus = data == nil ? .needsUpdate : .isNSync - switch payload.data { - case let .result(success: success): - if success { - continuation.resume() - } else { - continuation.resume(throwing: BasicError.message(messageText: "operation failed")) - } - case let .error(errorMessage: errorMessage): - continuation.resume(throwing: BasicError.message(messageText: errorMessage)) + 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 sync library item move: \(error.localizedDescription)") } } } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift b/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift index 2aa8ba78a..eedd0a78a 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift @@ -151,21 +151,18 @@ public extension DataService { case .needsUpdate: item.serverSyncStatus = Int64(ServerSyncStatus.isSyncing.rawValue) syncLinkArchiveStatus(itemID: item.unwrappedID, archived: item.isArchived) - syncLinkReadingProgress( - itemID: item.unwrappedID, - readingProgress: item.readingProgress, - anchorIndex: Int(item.readingProgressAnchor), - force: item.isPDF - ) - case .needsMove: + item.serverSyncStatus = Int64(ServerSyncStatus.isSyncing.rawValue) - syncLinkArchiveStatus(itemID: item.unwrappedID, archived: item.isArchived) syncLinkReadingProgress( itemID: item.unwrappedID, readingProgress: item.readingProgress, anchorIndex: Int(item.readingProgressAnchor), force: item.isPDF ) + // If the items folder might have changed, sync that. + if let itemID = item.id, let folder = item.folder, folder != "following" { + syncMoveToFolder(itemID: itemID, folder: folder) + } } } } @@ -193,9 +190,6 @@ public extension DataService { } else { highlight.serverSyncStatus = Int64(ServerSyncStatus.isNSync.rawValue) } - case .needsMove: - // Highlights can't be moved - break } } } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Queries/LinkedItemNetworkQuery.swift b/apple/OmnivoreKit/Sources/Services/DataService/Queries/LinkedItemNetworkQuery.swift index 4403b4ef3..57a65aca2 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Queries/LinkedItemNetworkQuery.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Queries/LinkedItemNetworkQuery.swift @@ -292,7 +292,7 @@ private let libraryArticleSelection = Selection.Article { downloadURL: try $0.url(), recommendations: try $0.recommendations(selection: recommendationSelection.list.nullable) ?? [], labels: try $0.labels(selection: feedItemLabelSelection.list.nullable) ?? [], - highlights: try $0.highlights(selection: highlightSelection.list) ?? [] + highlights: try $0.highlights(selection: highlightSelection.list) ) } diff --git a/apple/OmnivoreKit/Sources/Views/SyncingIcon.swift b/apple/OmnivoreKit/Sources/Views/SyncingIcon.swift index 1100da1e4..3294c389a 100644 --- a/apple/OmnivoreKit/Sources/Views/SyncingIcon.swift +++ b/apple/OmnivoreKit/Sources/Views/SyncingIcon.swift @@ -21,7 +21,7 @@ public struct SyncStatusIcon: View { switch status { case .isNSync: return "exclamationmark.icloud" - case .isSyncing, .needsCreation, .needsDeletion, .needsUpdate, .needsMove: + case .isSyncing, .needsCreation, .needsDeletion, .needsUpdate: return "icloud" } } @@ -30,7 +30,7 @@ public struct SyncStatusIcon: View { switch status { case .isNSync: return .red - case .isSyncing, .needsCreation, .needsDeletion, .needsUpdate, .needsMove: + case .isSyncing, .needsCreation, .needsDeletion, .needsUpdate: return .appGrayText } }