This repository was archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSettingsViewController.swift
More file actions
129 lines (111 loc) · 4.02 KB
/
SettingsViewController.swift
File metadata and controls
129 lines (111 loc) · 4.02 KB
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
import InterfaceBacked
import UIKit
protocol SettingsDelegate: class {
func doneTapped()
func clearOpenradarTapped()
func logoutTapped()
func frameworksTapped()
func feedbackTapped()
}
final class SettingsViewController: UITableViewController, StoryboardBacked {
// MARK: - Properties
let accountSection = 0
let appleRow = 0
let openradarRow = 1
let aboutSection = 1
let thirdPartyRow = 0
let feedbackRow = 1
weak var delegate: SettingsDelegate?
@IBOutlet weak var versionLabel: UILabel!
// MARK: - UIViewController Methods
override func viewDidLoad() {
super.viewDidLoad()
configureVersionLabel()
}
// MARK: - User Actions
@IBAction func doneTapped() {
delegate?.doneTapped()
}
// MARK: - UITableViewController Methods
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
switch indexPath.section {
case accountSection:
switch indexPath.row {
case appleRow:
if let (username, _) = Keychain.get(.radar) {
cell.detailTextLabel?.text = username
cell.detailTextLabel?.textColor = view.tintColor
} else {
cell.detailTextLabel?.text = Localizable.Settings.AppleRadar.placeholder.localized
cell.detailTextLabel?.textColor = UIColor.lightGray
}
case openradarRow:
if let (_, password) = Keychain.get(.openRadar) {
cell.detailTextLabel?.text = password
cell.textLabel?.textColor = UIColor.darkText
cell.detailTextLabel?.textColor = view.tintColor
} else {
cell.detailTextLabel?.text = Localizable.Settings.OpenRadar.placeholder.localized
cell.detailTextLabel?.textColor = UIColor.lightGray
cell.textLabel?.textColor = UIColor.lightGray
}
default: break
}
default: break
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.section {
case accountSection:
switch indexPath.row {
case appleRow:
showSheet(title: Localizable.Settings.AppleRadar.confirm.localized,
message: Localizable.Settings.AppleRadar.message.localized,
deleteTitle: Localizable.Settings.AppleRadar.logout.localized,
destructiveAction: {
self.delegate?.logoutTapped()
})
case openradarRow:
showSheet(title: Localizable.Settings.OpenRadar.confirm.localized,
message: Localizable.Settings.OpenRadar.message.localized,
deleteTitle: Localizable.Settings.OpenRadar.clear.localized,
destructiveAction: {
self.delegate?.clearOpenradarTapped()
})
default: break
}
case aboutSection:
switch indexPath.row {
case thirdPartyRow: delegate?.frameworksTapped()
case feedbackRow: delegate?.feedbackTapped()
default: break
}
default: break
}
}
// MARK: - Private
private func showSheet(title: String, message: String, deleteTitle: String, destructiveAction: @escaping () -> Void) {
let sheet = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
sheet.addAction(UIAlertAction(title: deleteTitle, style: .destructive, handler: { _ in
destructiveAction()
if let selected = self.tableView.indexPathForSelectedRow {
self.tableView.deselectRow(at: selected, animated: true)
}
}))
sheet.addAction(UIAlertAction(title: Localizable.Global.cancel.localized, style: .cancel, handler: { _ in
sheet.dismiss(animated: true)
if let selected = self.tableView.indexPathForSelectedRow {
self.tableView.deselectRow(at: selected, animated: true)
}
}))
present(sheet, animated: true, completion: nil)
if let popover = sheet.popoverPresentationController, let selected = tableView.indexPathForSelectedRow {
popover.sourceView = tableView
popover.sourceRect = tableView.rectForRow(at: selected)
}
}
private func configureVersionLabel() {
guard let info = Bundle.main.infoDictionary, let versionString = info["CFBundleShortVersionString"], let buildVersionString = info["CFBundleVersion"] else { return }
versionLabel.text = "Version \(versionString) (\(buildVersionString))"
}
}