forked from videolan/vlc-ios
/
VLCSettingsTableViewCell.swift
92 lines (79 loc) · 3.73 KB
/
VLCSettingsTableViewCell.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
/*****************************************************************************
* VLCSettingsTableViewCell.swift
* VLC for iOS
*****************************************************************************
* Copyright (c) 2018 VideoLAN. All rights reserved.
* $Id$
*
* Authors: Mike JS. Choi <mkchoi212 # icloud.com>
*
* Refer to the COPYING file of the official project for license.
*****************************************************************************/
import UIKit
class VLCSettingsTableViewCell: UITableViewCell {
@objc fileprivate func themeDidChange() {
backgroundColor = PresentationTheme.current.colors.background
textLabel?.textColor = PresentationTheme.current.colors.cellTextColor
detailTextLabel?.textColor = PresentationTheme.current.colors.cellDetailTextColor
}
@objc init(reuseIdentifier: String, target: IASKAppSettingsViewController) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
themeDidChange()
switch reuseIdentifier {
case kIASKPSToggleSwitchSpecifier:
let toggle = IASKSwitch(frame: .zero)
toggle.addTarget(target, action: #selector(target.toggledValue(_:)), for: .valueChanged)
accessoryView = toggle
selectionStyle = .none
case kIASKOpenURLSpecifier, kIASKPSMultiValueSpecifier:
accessoryType = .disclosureIndicator
selectionStyle = .default
default:
assertionFailure("\(reuseIdentifier) has not been defined for VLCSettingsTableViewCell")
}
}
@available(*, unavailable, message: "use init(reuseIdentifier: String)")
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func configure(specifier: IASKSpecifier, settingsValue: Any?) {
textLabel?.text = specifier.title()
textLabel?.numberOfLines = 0
detailTextLabel?.text = specifier.subtitle()
switch specifier.type() {
case kIASKPSToggleSwitchSpecifier:
configureToggle(specifier, settingsValue)
case kIASKPSMultiValueSpecifier:
configureMultiValue(specifier, settingsValue)
case kIASKOpenURLSpecifier:
break
default:
assertionFailure("\(specifier.type()) has not been defined for VLCSettingsTableViewCell")
}
}
fileprivate func configureToggle(_ specifier: IASKSpecifier, _ settingsValue: Any?) {
assert(specifier.type() == kIASKPSToggleSwitchSpecifier, "configureToggle should only be called for kIASKPSToggleSwitchSpecifier")
assert(settingsValue is Bool?, "settingsValue should be Bool?")
assert(accessoryView is IASKSwitch, "the accessory should be a switch")
var state = specifier.defaultBoolValue()
if let currentValue = settingsValue as? Bool {
switch currentValue {
case specifier.trueValue() as? Bool:
state = true
case specifier.falseValue() as? Bool:
state = false
default:
state = currentValue
}
}
if let toggle = accessoryView as? IASKSwitch {
toggle.isOn = state
toggle.key = specifier.key()
}
}
fileprivate func configureMultiValue(_ specifier: IASKSpecifier, _ value: Any?) {
assert(specifier.type() == kIASKPSMultiValueSpecifier, "configureMultiValue should only be called for kIASKPSMultiValueSpecifier")
detailTextLabel?.text = specifier.title(forCurrentValue: value ?? specifier.defaultValue())
}
}