convert remonder creation to async

This commit is contained in:
Satindar Dhillon 2022-04-27 13:53:30 -07:00
parent 6440709e20
commit 6cf1f0e0fb
3 changed files with 35 additions and 44 deletions

View file

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

View file

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

View file

@ -27,10 +27,10 @@ public enum ReminderItemId {
}
public extension DataService {
func createReminderPublisher(
func createReminder(
reminderItemId: ReminderItemId,
remindAt: Date
) -> AnyPublisher<String, BasicError> {
) 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()
}
}