diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift index 793d9e431..9db2db1a6 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift @@ -67,13 +67,18 @@ import Views viewModel.selectedLinkItem = linkedItem } .formSheet(isPresented: $viewModel.snoozePresented) { - SnoozeView(snoozePresented: $viewModel.snoozePresented, itemToSnoozeID: $viewModel.itemToSnoozeID) { - viewModel.snoozeUntil( - dataService: dataService, - linkId: $0.feedItemId, - until: $0.snoozeUntilDate, - successMessage: $0.successMessage - ) + SnoozeView( + snoozePresented: $viewModel.snoozePresented, + itemToSnoozeID: $viewModel.itemToSnoozeID + ) { snoozeParams in + Task { + await viewModel.snoozeUntil( + dataService: dataService, + linkId: snoozeParams.feedItemId, + until: snoozeParams.snoozeUntilDate, + successMessage: snoozeParams.successMessage + ) + } } } .onAppear { // TODO: use task instead diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift index 1048bb76d..6f4bcaea3 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift @@ -158,31 +158,25 @@ import Views dataService.removeLink(objectID: objectID) } - func snoozeUntil(dataService: DataService, linkId: String, until: Date, successMessage: String?) { + func snoozeUntil(dataService: DataService, linkId: String, until: Date, successMessage: String?) async { isLoading = true if let itemIndex = items.firstIndex(where: { $0.id == linkId }) { items.remove(at: itemIndex) } - dataService.createReminderPublisher( + if await (try? dataService.createReminder( reminderItemId: .link(id: linkId), remindAt: until - ) - .sink( - receiveCompletion: { [weak self] completion in - guard case .failure = completion else { return } - self?.isLoading = false - NSNotification.operationFailed(message: "Failed to snooze") - }, - receiveValue: { [weak self] _ in - self?.isLoading = false - if let message = successMessage { - Snackbar.show(message: message) - } + )) != nil { + if let message = successMessage { + Snackbar.show(message: message) } - ) - .store(in: &subscriptions) + } else { + NSNotification.operationFailed(message: "Failed to snooze") + } + + isLoading = false } private var searchQuery: String? { diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateReminder.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateReminder.swift index 5adfcd776..3804fcc6e 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateReminder.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateReminder.swift @@ -27,10 +27,10 @@ public enum ReminderItemId { } public extension DataService { - func createReminderPublisher( + func createReminder( reminderItemId: ReminderItemId, remindAt: Date - ) -> AnyPublisher { + ) async throws -> String { enum MutationResult { case complete(id: String) case error(errorCode: Enums.CreateReminderErrorCode) @@ -61,28 +61,20 @@ public extension DataService { let path = appEnvironment.graphqlPath let headers = networker.defaultHeaders - return Deferred { - Future { promise in - send(mutation, to: path, headers: headers) { result in - switch result { - case let .success(payload): - if let graphqlError = payload.errors { - promise(.failure(.message(messageText: "graphql error: \(graphqlError)"))) - } + 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 + } - switch payload.data { - case let .complete(id: id): - promise(.success(id)) - case let .error(errorCode: errorCode): - promise(.failure(.message(messageText: errorCode.rawValue))) - } - case .failure: - promise(.failure(.message(messageText: "graphql error"))) - } + switch payload.data { + case let .complete(id: id): + continuation.resume(returning: id) + case let .error(errorCode: errorCode): + continuation.resume(throwing: BasicError.message(messageText: errorCode.rawValue)) } } } - .receive(on: DispatchQueue.main) - .eraseToAnyPublisher() } }