/
SettingsViewController.swift
96 lines (71 loc) · 3 KB
/
SettingsViewController.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
//
// SettingsViewController.swift
// rechord
//
// Created by Julian Tigler on 11/22/19.
// Copyright © 2019 High5! Apps. All rights reserved.
//
import UIKit
class SettingsViewController: UITableViewController {
// MARK: Properties
var chordSettings = [String: Bool]()
var chordTypes: [Chord.Type]!
override func viewDidLoad() {
super.viewDidLoad()
setNavigationTitle("Settings", kern: 1.5)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
chordTypes = appDelegate.chordTypes
}
private func setNavigationTitle(_ title: String, kern: CGFloat) {
let titleLabel = UILabel()
let attributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 24, weight: .thin),
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.kern: kern] as [NSAttributedString.Key : Any]
let attributedTitle = NSAttributedString(string: title, attributes: attributes)
titleLabel.attributedText = attributedTitle
titleLabel.sizeToFit()
navigationItem.titleView = titleLabel
}
// MARK: Navigation
@IBAction func done(_ sender: UIBarButtonItem) {
self.dismiss(animated: true)
}
// MARK: Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return chordTypes.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "ChordCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
let chordName = chordTypes[indexPath.row].friendlyName
cell.textLabel?.text = chordName
let isEnabled = UserDefaults.standard.bool(forKey: chordName)
let accessoryType: UITableViewCell.AccessoryType = isEnabled ? .checkmark : .none
cell.accessoryType = accessoryType
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = self.tableView.cellForRow(at: indexPath)!
let isEnabled = cell.accessoryType == .checkmark
// Require at least 1 chord type to be enabled
if isEnabled && getEnabledCount() == 1 {
return
}
cell.accessoryType = isEnabled ? .none : .checkmark
UserDefaults.standard.set(!isEnabled, forKey: cell.textLabel!.text!)
}
private func getEnabledCount() -> Int {
var enabledCount = 0
let defaults = UserDefaults.standard
for chordType in chordTypes {
if defaults.bool(forKey: chordType.friendlyName) {
enabledCount += 1
}
}
return enabledCount
}
}