-
-
Notifications
You must be signed in to change notification settings - Fork 452
/
SPPermissionsListController.swift
245 lines (201 loc) · 10.1 KB
/
SPPermissionsListController.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// The MIT License (MIT)
// Copyright © 2020 Ivan Vorobei (hello@ivanvorobei.by)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#if os(iOS)
import UIKit
@available(iOSApplicationExtension, unavailable)
public class SPPermissionsListController: UITableViewController, SPPermissionsControllerInterface {
/**
SPPermissions: Show or hide close button. By default `false`.
New guidelines not allow has it, so for clean review you shoud left it `false`.
*/
public var showCloseButton: Bool = false
/**
SPPermissions: Disable or allow dismiss controller with swipe.
Default value is `true`.
*/
public var allowSwipeDismiss: Bool = true
/**
SPPermissions: Manage dismising condition.
By default dismiss controller hapen when all permission allowed.
If you need dismiss controller when all permissions has state determinated, change it to `allPermissionsDeterminated`.
*/
public var dismissCondition: SPPermissionsDismissCondition = .default
public weak var dataSource: SPPermissionsDataSource?
public weak var delegate: SPPermissionsDelegate?
public var titleText = Texts.header
public var headerText = Texts.description
public var footerText = Texts.comment
public var prefersLargeTitles = true
private var permissions: [SPPermissions.Permission]
// MARK: - Init
init(_ permissions: [SPPermissions.Permission]) {
self.permissions = permissions
if #available(iOS 13.0, *) {
super.init(style: .insetGrouped)
} else {
super.init(style: .grouped)
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
}
// MARK: - Lifecycle
override public func viewDidLoad() {
super.viewDidLoad()
if showCloseButton {
if #available(iOS 13.0, *) {
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .close, target: self, action: #selector(dismissAnimated))
} else {
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissAnimated))
}
}
navigationItem.title = titleText
navigationItem.largeTitleDisplayMode = .automatic
navigationController?.navigationBar.prefersLargeTitles = prefersLargeTitles
navigationController?.presentationController?.delegate = self
tableView.delaysContentTouches = false
tableView.allowsSelection = false
tableView.register(SPPermissionsTableViewCell.self, forCellReuseIdentifier: SPPermissionsTableViewCell.id)
tableView.register(SPPermissionsListHeaderView.self, forHeaderFooterViewReuseIdentifier: SPPermissionsListHeaderView.id)
tableView.register(SPPermissionsListFooterCommentView.self, forHeaderFooterViewReuseIdentifier: SPPermissionsListFooterCommentView.id)
NotificationCenter.default.addObserver(self, selector: #selector(self.applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
}
@objc func applicationDidBecomeActive() {
tableView.reloadData()
}
// MARK: - Helpers
public func present(on controller: UIViewController) {
let navigationController = UINavigationController(rootViewController: self)
navigationController.modalPresentationStyle = .formSheet
navigationController.preferredContentSize = CGSize.init(width: 480, height: 560)
controller.present(navigationController, animated: true, completion: nil)
}
@objc func process(button: SPPermissionsActionButton) {
guard let permission = button.permission else { return }
let firstRequest = permission.status == .notDetermined
permission.request { [weak self] in
guard let self = self else { return }
if let cell = button.superview as? SPPermissionsTableViewCell {
cell.updateInterface(animated: true)
}
let authorized = permission.authorized
if authorized { Haptic.impact(.light) }
// Update `.locationWhenInUse` if allowed `.locationAlwaysAndWhenInUse`
if permission.type == .locationAlways {
if self.permissions.contains(where: { $0.type == .locationWhenInUse }) {
if let index = self.permissions.firstIndex(where: { $0.type == .locationWhenInUse }) {
if let cell = self.tableView.cellForRow(at: IndexPath(row: index, section: 0)) as? SPPermissionsTableViewCell {
cell.updateInterface(animated: true)
}
}
}
}
let dismissByCondition: () -> Bool = {
switch self.dismissCondition {
case .allPermissionsAuthorized:
let allowedPermissions = self.permissions.filter { $0.authorized }
if allowedPermissions.count == self.permissions.count {
Delay.wait(0.2, closure: {
self.dismiss(animated: true)
})
return true
}
case .allPermissionsDeterminated:
let determiatedPermissions = self.permissions.filter { !$0.notDetermined }
if determiatedPermissions.count == self.permissions.count {
Delay.wait(0.2, closure: {
self.dismiss(animated: true)
})
return true
}
}
return false
}
if permission.authorized {
self.delegate?.didAllowPermission(permission)
let _ = dismissByCondition()
} else {
self.delegate?.didDeniedPermission(permission)
if firstRequest {
let _ = dismissByCondition()
} else {
// Delay using for fix animation freeze.
Delay.wait(0.3, closure: { [weak self] in
guard let self = self else { return }
Presenter.presentAlertAboutDeniedPermission(permission, dataSource: self.dataSource, on: self)
})
}
}
}
}
@objc func dismissAnimated() {
self.dismiss(animated: true, completion: nil)
}
public override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: flag, completion: { [weak self] in
completion?()
self?.processDissmissedEvent()
})
}
private func processDissmissedEvent() {
delegate?.didHidePermissions(permissions)
}
}
// MARK: - Table Data Source & Delegate
@available(iOSApplicationExtension, unavailable)
extension SPPermissionsListController {
public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return permissions.count
}
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let permission = permissions[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: SPPermissionsTableViewCell.id, for: indexPath) as! SPPermissionsTableViewCell
cell.defaultConfigure(for: permission)
dataSource?.configure(cell, for: permission)
cell.permissionButton.addTarget(self, action: #selector(self.process(button:)), for: .touchUpInside)
cell.updateInterface(animated: false)
return cell
}
public override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: SPPermissionsListHeaderView.id) as! SPPermissionsListHeaderView
view.titleLabel.text = headerText
return view
}
public override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: SPPermissionsListFooterCommentView.id) as! SPPermissionsListFooterCommentView
view.titleLabel.text = footerText
return view
}
}
// MARK: - UIAdaptivePresentationControllerDelegate
@available(iOSApplicationExtension, unavailable)
extension SPPermissionsListController: UIAdaptivePresentationControllerDelegate {
public func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
return allowSwipeDismiss
}
public func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
self.processDissmissedEvent()
}
}
#endif