Skip to content

Commit

Permalink
Clean requests permission with sub-access levels.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvorobei committed Sep 27, 2023
1 parent 539e3ce commit 6d13eb8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 37 deletions.
61 changes: 52 additions & 9 deletions Sources/CalendarPermission/CalendarPermission.swift
Expand Up @@ -30,15 +30,15 @@ import EventKit
public extension Permission {

static var calendar: CalendarPermission {
return CalendarPermission(kind: .calendar)
return CalendarPermission(kind: .calendar(access: .full))
}
}

@available(iOS 17.0, *)
public extension Permission {

static var calendarWriteOnly: CalendarPermission {
return CalendarPermission(kind: .calendarWriteOnly)
return CalendarPermission(kind: .calendar(access: .write))
}
}

Expand All @@ -53,9 +53,23 @@ public class CalendarPermission: Permission {
}

open override var kind: Permission.Kind { self._kind }
open var usageDescriptionKey: String? { "NSCalendarsUsageDescription" }
open var usageFullAccessDescriptionKey: String? { "NSCalendarsFullAccessUsageDescription" }
open var usageWriteOnlyAccessDescriptionKey: String? { "NSCalendarsWriteOnlyAccessUsageDescription" }
open var usageDescriptionKey: String? {
if #available(iOS 17, *) {
switch kind {
case .calendar(let access):
switch access {
case .full:
return "NSCalendarsFullAccessUsageDescription"
case .write:
return "NSCalendarsWriteOnlyAccessUsageDescription"
}
default:
fatalError()
}
} else {
return "NSCalendarsUsageDescription"
}
}

public override var status: Permission.Status {
switch EKEventStore.authorizationStatus(for: EKEntityType.event) {
Expand All @@ -64,7 +78,22 @@ public class CalendarPermission: Permission {
case .fullAccess: return .authorized
case .notDetermined: return .notDetermined
case .restricted: return .denied
case .writeOnly: return .authorized
case .writeOnly:
if #available(iOS 17, *) {
switch kind {
case .calendar(let access):
switch access {
case .full:
return .denied
case .write:
return .authorized
}
default:
fatalError()
}
} else {
return .authorized
}
@unknown default: return .denied
}
}
Expand All @@ -74,19 +103,33 @@ public class CalendarPermission: Permission {
let eventStore = EKEventStore()

if #available(iOS 17.0, *) {
if self._kind == .calendarWriteOnly {

let requestWriteOnly = {
eventStore.requestWriteOnlyAccessToEvents { (accessGranted: Bool, error: Error?) in
DispatchQueue.main.async {
completion()
}
}
} else {
eventStore.requestFullAccessToEvents { (accessGranted: Bool, error: Error?) in
}

let requestFull = {
eventStore.requestWriteOnlyAccessToEvents { (accessGranted: Bool, error: Error?) in
DispatchQueue.main.async {
completion()
}
}
}

switch kind {
case .calendar(let access):
if access == .write {
requestWriteOnly()
} else {
requestFull()
}
default:
requestFull()
}
} else {
eventStore.requestAccess(to: EKEntityType.event) { (accessGranted: Bool, error: Error?) in
DispatchQueue.main.async {
Expand Down
3 changes: 1 addition & 2 deletions Sources/PermissionsKit/Data/Text.swift
Expand Up @@ -31,8 +31,7 @@ enum Texts {
return NSLocalizedString("permission photoLibrary name", bundle: bundle, comment: "")
case .microphone:
return NSLocalizedString("permission microphone name", bundle: bundle, comment: "")
case .calendar,
.calendarWriteOnly:
case .calendar(access: .full), .calendar(access: .write):
return NSLocalizedString("permission calendar name", bundle: bundle, comment: "")
case .contacts:
return NSLocalizedString("permission contacts name", bundle: bundle, comment: "")
Expand Down
53 changes: 27 additions & 26 deletions Sources/PermissionsKit/Permission.swift
Expand Up @@ -21,7 +21,7 @@

import UIKit

open class Permission: Equatable {
open class Permission {

open var authorized: Bool {
return status == .authorized
Expand Down Expand Up @@ -78,10 +78,6 @@ open class Permission: Equatable {

// MARK: Internal

public static func == (lhs: Permission, rhs: Permission) -> Bool {
return lhs.kind == rhs.kind
}

public init() {}

// MARK: - Models
Expand All @@ -103,26 +99,26 @@ open class Permission: Equatable {
}
}

@objc public enum Kind: Int {
public enum Kind {

case camera = 0
case notification = 2
case photoLibrary = 1
case microphone = 3
case calendar = 4
case calendarWriteOnly = 5
case contacts = 6
case reminders = 7
case speech = 8
case locationWhenInUse = 9
case locationAlways = 10
case motion = 11
case mediaLibrary = 12
case bluetooth = 13
case tracking = 14
case faceID = 15
case siri = 16
case health = 17
case camera
case notification
case photoLibrary
case microphone
case calendar(access: CalendarAccess)
case contacts
case reminders
case speech
// Upgrade location as calendar style
case locationWhenInUse
case locationAlways
case motion
case mediaLibrary
case bluetooth
case tracking
case faceID
case siri
case health

public var name: String {
switch self {
Expand All @@ -132,8 +128,7 @@ open class Permission: Equatable {
return "Photo Library"
case .microphone:
return "Microphone"
case .calendar,
.calendarWriteOnly:
case .calendar(access: .write), .calendar(access: .full):
return "Calendar"
case .contacts:
return "Contacts"
Expand Down Expand Up @@ -164,4 +159,10 @@ open class Permission: Equatable {
}
}
}

public enum CalendarAccess {

case full
case write
}
}

0 comments on commit 6d13eb8

Please sign in to comment.