Commit 5b3dfaf9 authored by Carola Nitz's avatar Carola Nitz

VLCSettingsTableViewCell: more defensive code and syntactic sugar

parent 63f8a9fb
B/* A single strings file, whose title is specified in your preferences schema. The strings files provide the localized content to display to the user for each of your preferences. */
......
......@@ -55,7 +55,6 @@
- (void)themeDidChange
{
self.view.backgroundColor = PresentationTheme.current.colors.background;
[self.tableView reloadData];
[self setNeedsStatusBarAppearanceUpdate];
}
......@@ -156,9 +155,9 @@
IASKSpecifier *specifier = [self.settingsReader specifierForIndexPath:indexPath];
VLCSettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:specifier.type];
if (!cell) {
cell = [VLCSettingsTableViewCell cellWithIdentifier:specifier.type target:self];
cell = [[VLCSettingsTableViewCell alloc] initWithReuseIdentifier:specifier.type target:self];
}
[cell configureWithSpecifier:specifier value:[self.settingsStore objectForKey:specifier.key]];
[cell configureWithSpecifier:specifier settingsValue:[self.settingsStore objectForKey:specifier.key]];
return cell;
}
......@@ -178,26 +177,4 @@
}
}
#pragma mark - InAppSettings customization
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
IASKSpecifier *specifier = [self.settingsReader specifierForIndexPath:indexPath];
if ([specifier.type isEqual: kIASKPSToggleSwitchSpecifier]) {
VLCSettingsTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
IASKSwitch *toggle = (IASKSwitch *)cell.accessoryView;
[toggle setOn:!toggle.isOn animated:YES];
[self toggledValue:toggle];
} else {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
}
}
@end
......@@ -13,58 +13,60 @@
import UIKit
class VLCSettingsTableViewCell: UITableViewCell {
override func prepareForReuse() {
super.prepareForReuse()
setupCell()
}
fileprivate func setupCell() {
@objc fileprivate func themeDidChange() {
backgroundColor = PresentationTheme.current.colors.background
textLabel?.textColor = PresentationTheme.current.colors.cellTextColor
detailTextLabel?.textColor = PresentationTheme.current.colors.cellDetailTextColor
}
@objc static func cell(identifier: String, target: IASKAppSettingsViewController) -> VLCSettingsTableViewCell? {
let cell = VLCSettingsTableViewCell(style: .subtitle, reuseIdentifier: identifier)
cell.setupCell()
@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 identifier {
switch reuseIdentifier {
case kIASKPSToggleSwitchSpecifier:
let toggle = IASKSwitch(frame: .zero)
toggle.addTarget(target, action: #selector(target.toggledValue(_:)), for: .valueChanged)
cell.accessoryView = toggle
cell.selectionStyle = .none
accessoryView = toggle
selectionStyle = .none
case kIASKOpenURLSpecifier, kIASKPSMultiValueSpecifier:
cell.accessoryType = .disclosureIndicator
cell.selectionStyle = .default
accessoryType = .disclosureIndicator
selectionStyle = .default
default:
assertionFailure("\(identifier) has not been defined for VLCSettingsTableViewCell")
assertionFailure("\(reuseIdentifier) has not been defined for VLCSettingsTableViewCell")
}
return cell
}
@objc func configure(specifier: IASKSpecifier, value: Any?) {
@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()
detailTextLabel?.text = specifier.subtitle()
switch specifier.type() {
case kIASKPSToggleSwitchSpecifier:
configureToggle(specifier, value)
configureToggle(specifier, settingsValue)
case kIASKPSMultiValueSpecifier:
configureMultiValue(specifier, value)
configureMultiValue(specifier, settingsValue)
case kIASKOpenURLSpecifier:
configureOpenURL(specifier)
break
default:
assertionFailure("\(specifier.type()) has not been defined for VLCSettingsTableViewCell")
}
}
fileprivate func configureToggle(_ specifier: IASKSpecifier, _ value: Any?) {
detailTextLabel?.text = specifier.subtitle()
var state: Bool
if let currentValue = value as? Bool {
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
......@@ -73,8 +75,6 @@ class VLCSettingsTableViewCell: UITableViewCell {
default:
state = currentValue
}
} else {
state = specifier.defaultBoolValue()
}
if let toggle = accessoryView as? IASKSwitch {
......@@ -84,14 +84,8 @@ class VLCSettingsTableViewCell: UITableViewCell {
}
fileprivate func configureMultiValue(_ specifier: IASKSpecifier, _ value: Any?) {
if let currentValue = value {
detailTextLabel?.text = specifier.title(forCurrentValue: currentValue)
} else {
detailTextLabel?.text = specifier.title(forCurrentValue: specifier.defaultValue())
}
}
fileprivate func configureOpenURL(_ specifier: IASKSpecifier) {
detailTextLabel?.text = specifier.subtitle()
assert(specifier.type() == kIASKPSMultiValueSpecifier, "configureMultiValue should only be called for kIASKPSMultiValueSpecifier")
detailTextLabel?.text = specifier.title(forCurrentValue: value ?? specifier.defaultValue())
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment