Sync move operation, attempt to sync before home is loaded

This commit is contained in:
Jackson Harper 2023-12-28 15:46:57 +08:00
parent c09a6fc1de
commit 3667595f48
7 changed files with 27 additions and 33 deletions

View file

@ -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) {

View file

@ -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()

View file

@ -6,5 +6,4 @@ public enum ServerSyncStatus: Int {
case needsDeletion
case needsCreation
case needsUpdate
case needsMove
}

View file

@ -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)")
}
}
}

View file

@ -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
}
}
}

View file

@ -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)
)
}

View file

@ -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
}
}