Skip to content

Commit

Permalink
refactor(types): breakdown definitions file (#164)
Browse files Browse the repository at this point in the history
Migrate `checkAllPermissions` to new approach.
  • Loading branch information
ebarooni committed Feb 4, 2025
1 parent 0465d0a commit e921cc8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ public class CapacitorCalendarPlugin: CAPPlugin, CAPBridgedPlugin {
}

@objc public func checkAllPermissions(_ call: CAPPluginCall) {
Task {
do {
let calendarPermissionsState = try await calendar.checkAllPermissions()
let remindersPermissionsState = try await reminders.checkAllPermissions()
call.resolve(calendarPermissionsState.merging(remindersPermissionsState) { (_, new) in new })
} catch {
call.reject("[CapacitorCalendar.\(#function)] Could not determine the status of the requested permissions")
return
}
do {
let result = try implementation.checkAllPermissions()
call.resolve(result.toJSON())
} catch let error {
call.reject(error.localizedDescription)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,38 @@ class CapacitorCalendarNew {

func checkPermission(input: CheckPermissionInput) throws -> CheckPermissionResult {
let scope = input.getScope()
let status: EKAuthorizationStatus
let state: EKAuthorizationStatus

switch scope {
case .readCalendar, .writeCalendar:
status = EKEventStore.authorizationStatus(for: .event)
state = EKEventStore.authorizationStatus(for: .event)
case .readReminders, .writeReminders:
status = EKEventStore.authorizationStatus(for: .reminder)
state = EKEventStore.authorizationStatus(for: .reminder)
}

var result: CheckPermissionResult

switch status {
case .authorized, .fullAccess:
result = CheckPermissionResult(status: CAPPermissionState.granted)
case .denied, .restricted:
result = CheckPermissionResult(status: CAPPermissionState.denied)
case .writeOnly:
if scope == .writeCalendar || scope == .writeReminders {
result = CheckPermissionResult(status: CAPPermissionState.granted)
} else {
result = CheckPermissionResult(status: CAPPermissionState.prompt)
let detectedState = try ImplementationHelper.permissionStateToResult(state: state, scope: scope)
return CheckPermissionResult(status: detectedState)
}

func checkAllPermissions() throws -> CheckAllPermissionsResult {
let calendarState = EKEventStore.authorizationStatus(for: .event)
let remindersState = EKEventStore.authorizationStatus(for: .reminder)
var permissionsResult: [String: String] = [:]

for scope in CalendarPermissionScope.allCases {
let state: EKAuthorizationStatus

switch scope {
case .readCalendar, .writeCalendar:
state = calendarState
case .readReminders, .writeReminders:
state = remindersState
}
case .notDetermined:
result = CheckPermissionResult(status: CAPPermissionState.prompt)
default:
throw PluginError.unhandledPermissionState

let detectedState = try ImplementationHelper.permissionStateToResult(state: state, scope: scope)
permissionsResult[scope.rawValue] = detectedState.rawValue
}

return result
return CheckAllPermissionsResult(statesDict: permissionsResult)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
enum CalendarPermissionScope: String {
enum CalendarPermissionScope: String, CaseIterable {
case readCalendar = "readCalendar"
case readReminders = "readReminders"
case writeCalendar = "writeCalendar"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Capacitor

struct CheckAllPermissionsResult {
private let statesDict: [String: String]

init(statesDict: [String: String]) {
self.statesDict = statesDict
}

func toJSON() -> JSObject {
var result = JSObject()
result["result"] = statesDict as JSObject
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import Capacitor

struct CheckPermissionResult {
private let status: CAPPermissionState
private let scope: CalendarPermissionScope

init(status: CAPPermissionState) {
self.status = status
self.scope = scope
}

func toJSON() -> JSObject {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import EventKit

struct ImplementationHelper {
static func permissionStateToResult(state: EKAuthorizationStatus, scope: CalendarPermissionScope ) throws -> CAPPermissionState {
var result: CAPPermissionState

switch state {
case .authorized, .fullAccess:
result = CAPPermissionState.granted
case .denied, .restricted:
result = CAPPermissionState.denied
case .writeOnly:
if scope == .writeCalendar || scope == .writeReminders {
result = CAPPermissionState.granted
} else {
result = CAPPermissionState.prompt
}
case .notDetermined:
result = CAPPermissionState.prompt
default:
throw PluginError.unhandledPermissionState
}

return result
}
}

0 comments on commit e921cc8

Please sign in to comment.