mirror of
https://github.com/sparrowcode/PermissionsKit.git
synced 2026-04-07 19:17:42 +00:00
Clean requests permission with sub-access levels.
This commit is contained in:
@@ -30,7 +30,7 @@ import EventKit
|
||||
public extension Permission {
|
||||
|
||||
static var calendar: CalendarPermission {
|
||||
return CalendarPermission(kind: .calendar)
|
||||
return CalendarPermission(kind: .calendar(access: .full))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ public extension Permission {
|
||||
public extension Permission {
|
||||
|
||||
static var calendarWriteOnly: CalendarPermission {
|
||||
return CalendarPermission(kind: .calendarWriteOnly)
|
||||
return CalendarPermission(kind: .calendar(access: .write))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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: "")
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
import UIKit
|
||||
|
||||
open class Permission: Equatable {
|
||||
open class Permission {
|
||||
|
||||
open var authorized: Bool {
|
||||
return status == .authorized
|
||||
@@ -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
|
||||
@@ -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 {
|
||||
@@ -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"
|
||||
@@ -164,4 +159,10 @@ open class Permission: Equatable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum CalendarAccess {
|
||||
|
||||
case full
|
||||
case write
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user