Skip to content

Commit

Permalink
More proper way to handle Event Kit request handler with concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
vinhnx committed Oct 27, 2023
1 parent 8a131ab commit bfcfcb6
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions Sources/Shift/Shift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import SwiftUI
import EventKit
import Algorithms

typealias RequestAccessCompletion = ((Bool, Error?) -> Void)

/// ShiftError definition
public enum ShiftError: Error, LocalizedError {
case mapFromError(Error)
Expand Down Expand Up @@ -222,13 +224,26 @@ public final class Shift: ObservableObject {
return calendar
}


private func requestCalendarAccess() async throws -> Bool {
if #available(iOS 17, *) {
try await eventStore.requestFullAccessToEvents()
} else {
try await eventStore.requestAccess(to: .event)
try await withCheckedThrowingContinuation { [weak self] continuation in
guard let self else { return }
let completion: RequestAccessCompletion = { granted, error in
if let error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: granted)
}
}

if #available(iOS 17.0, *) {
self.eventStore.requestFullAccessToEvents(completion: completion)
} else {
self.eventStore.requestAccess(to: .event, completion: completion)
}
}
}

}

extension EKEventStore {
Expand Down

0 comments on commit bfcfcb6

Please sign in to comment.