From bfcfcb6368925a1f4862725691db310ad1637f06 Mon Sep 17 00:00:00 2001 From: Vinh Nguyen Date: Fri, 27 Oct 2023 21:02:44 +0700 Subject: [PATCH] More proper way to handle Event Kit request handler with concurrency --- Sources/Shift/Shift.swift | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Sources/Shift/Shift.swift b/Sources/Shift/Shift.swift index 6c20d80..513b2d5 100644 --- a/Sources/Shift/Shift.swift +++ b/Sources/Shift/Shift.swift @@ -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) @@ -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 {