forked from C3-PRO/c3-pro-ios-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemPermissionTableViewController.swift
95 lines (73 loc) · 2.93 KB
/
SystemPermissionTableViewController.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
//
// SystemPermissionTableViewController.swift
// C3PRO
//
// Created by Pascal Pfiffner on 1/18/16.
// Copyright © 2016 Boston Children's Hospital. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import UIKit
/**
Table view controller displaying one cell per system service that access to should be granted. Each cell shows an active "Allow" button
or a disabled "Granted" button for the services you define, allowing users to grant access to select system services.
Assign an array of `SystemService` instances to your controller's `services` property, then present the view controller to the user.
*/
open class SystemPermissionTableViewController: UITableViewController {
lazy var permissionRequester = SystemServicePermissioner()
/// The services to request access to.
open var services: [SystemService]?
override public init(style: UITableViewStyle) {
super.init(style: style)
}
override public init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// MARK: - Data
func serviceAtIndexPath(_ indexPath: IndexPath) -> SystemService? {
if let services = services {
if indexPath.row < services.count {
return services[indexPath.row]
}
}
return nil
}
// MARK: - View Tasks
override open func viewDidLoad() {
super.viewDidLoad()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.delegate = self
tableView.dataSource = self
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 200.0
tableView.register(PermissionRequestTableViewCell.self, forCellReuseIdentifier: "MainCell")
}
// MARK: - Table View Data Source
override open func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return services?.count ?? 0
}
override open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainCell", for: indexPath) as! PermissionRequestTableViewCell
cell.selectionStyle = .none
if let service = serviceAtIndexPath(indexPath) {
cell.setup(for: service, permissioner: permissionRequester, viewController: self)
}
return cell
}
}