-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPermissionsRequestor.swift
65 lines (54 loc) · 1.82 KB
/
PermissionsRequestor.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// PermissionsRequestor.swift
// IFTTT SDK
//
// Copyright © 2020 IFTTT. All rights reserved.
//
import UIKit
import CoreLocation
/// Handles requesting device permissions
final class PermissionsRequestor {
var showPermissionsPrompts = true
private let locationManager = CLLocationManager()
private let queue: OperationQueue = {
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
return queue
}()
private let registry: ConnectionsRegistry
init(registry: ConnectionsRegistry) {
self.registry = registry
}
func processUpdate(with connections: Set<Connection.ConnectionStorage>) {
if !canUpdate() { return }
let operations = connections.reduce(.init()) { (currSet, connections) -> Set<Trigger> in
return currSet.union(connections.allTriggers)
}
.compactMap { permission -> Library in
switch permission {
case .location:
return LocationLibrary(locationManager: locationManager)
}
}.map { BlockOperation(library: $0) }
queue.addOperations(operations, waitUntilFinished: false)
}
private func canUpdate() -> Bool {
return !registry.getConnections().isEmpty
&& UserAuthenticatedRequestCredentialProvider.standard.userToken != nil
&& UIApplication.shared.applicationState == .active
&& showPermissionsPrompts
}
}
private extension BlockOperation {
convenience init(library: Library) {
self.init {
let semaphore = DispatchSemaphore(value: 0)
DispatchQueue.main.async {
library.requestAccess { _ in
semaphore.signal()
}
}
semaphore.wait()
}
}
}