Browse Source

VLCSettingsTableViewCell: Add custom tableViewCell for settings

Mike JS. Choi 7 years ago
parent
commit
bda39575e4

+ 34 - 6
Sources/VLCSettingsController.m

@@ -45,13 +45,16 @@
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_ABOUT", nil) style:UIBarButtonItemStylePlain target:self action:@selector(showAbout)];
     self.navigationItem.leftBarButtonItem.accessibilityIdentifier = VLCAccessibilityIdentifier.about;
     
+    self.neverShowPrivacySettings = YES;
+    self.tableView.estimatedRowHeight = 100;
+    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
+    self.tableView.rowHeight = UITableViewAutomaticDimension;
     [self themeDidChange];
 }
 
 - (void)themeDidChange
 {
-    self.view.backgroundColor = PresentationTheme.current.colors.settingsBackground;
-    self.tableView.separatorColor = PresentationTheme.current.colors.separatorColor;
+    self.view.backgroundColor = PresentationTheme.current.colors.background;
     [self.tableView reloadData];
     [self setNeedsStatusBarAppearanceUpdate];
 }
@@ -150,10 +153,12 @@
 
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
-    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
-    cell.backgroundColor = PresentationTheme.current.colors.settingsCellBackground;
-    cell.textLabel.textColor = PresentationTheme.current.colors.cellTextColor;
-    cell.detailTextLabel.textColor = PresentationTheme.current.colors.cellDetailTextColor;
+    IASKSpecifier *specifier = [self.settingsReader specifierForIndexPath:indexPath];
+    VLCSettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:specifier.type];
+    if (!cell) {
+        cell = [VLCSettingsTableViewCell cellWithIdentifier:specifier.type target:self];
+    }
+    [cell configureWithSpecifier:specifier value:[self.settingsStore objectForKey:specifier.key]];
     return cell;
 }
 
@@ -172,4 +177,27 @@
         [self.navigationController.presentedViewController dismissViewControllerAnimated:YES completion:nil];
     }
 }
+
+#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

+ 97 - 0
Sources/VLCSettingsTableViewCell.swift

@@ -0,0 +1,97 @@
+/*****************************************************************************
+ * 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 {
+
+    override func prepareForReuse() {
+        super.prepareForReuse()
+        setupCell()
+    }
+    
+    fileprivate func setupCell() {
+        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()
+        
+        switch identifier {
+        case kIASKPSToggleSwitchSpecifier:
+            let toggle = IASKSwitch(frame: .zero)
+            toggle.addTarget(target, action: #selector(target.toggledValue(_:)), for: .valueChanged)
+            cell.accessoryView = toggle
+            cell.selectionStyle = .none
+        case kIASKOpenURLSpecifier, kIASKPSMultiValueSpecifier:
+            cell.accessoryType = .disclosureIndicator
+            cell.selectionStyle = .default
+        default:
+            assertionFailure("\(identifier) has not been defined for VLCSettingsTableViewCell")
+        }
+        
+        return cell
+    }
+    
+    @objc func configure(specifier: IASKSpecifier, value: Any?) {
+        textLabel?.text = specifier.title()
+        
+        switch specifier.type() {
+        case kIASKPSToggleSwitchSpecifier:
+            configureToggle(specifier, value)
+        case kIASKPSMultiValueSpecifier:
+            configureMultiValue(specifier, value)
+        case kIASKOpenURLSpecifier:
+            configureOpenURL(specifier)
+        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 {
+            switch currentValue {
+            case specifier.trueValue() as? Bool:
+                state = true
+            case specifier.falseValue() as? Bool:
+                state = false
+            default:
+                state = currentValue
+            }
+        } else {
+            state = specifier.defaultBoolValue()
+        }
+        
+        if let toggle = accessoryView as? IASKSwitch {
+            toggle.isOn = state
+            toggle.key = specifier.key()
+        }
+    }
+    
+    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()
+    }
+}

+ 4 - 0
VLC.xcodeproj/project.pbxproj

@@ -261,6 +261,7 @@
 		CAA0B0F02072651A00B9274E /* XCUIElement+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAF76D9320709C9500E2AD7B /* XCUIElement+Helpers.swift */; };
 		CAA0B0F52072686E00B9274E /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = A79246C6170F11DF0036AAF2 /* Localizable.strings */; };
 		CAA0B0F720726A0E00B9274E /* TestHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAA0B0F620726A0E00B9274E /* TestHelper.swift */; };
+		CABCBAFF20EB15EA0040E2F5 /* VLCSettingsTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = CABCBAFE20EB15EA0040E2F5 /* VLCSettingsTableViewCell.swift */; };
 		CAC0AFED20CF8F6F00EDB035 /* VLCAccessibilityIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DD6516E208C89BC0052EE68 /* VLCAccessibilityIdentifier.swift */; };
 		CAD925792075536300F88496 /* Screenshot.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAD925782075536300F88496 /* Screenshot.swift */; };
 		CAD925812075613200F88496 /* SnapshotHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAD925802075613100F88496 /* SnapshotHelper.swift */; };
@@ -976,6 +977,7 @@
 		C6872E7B396534F3DAF4E48F /* Pods-VLC-iOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-VLC-iOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-VLC-iOS/Pods-VLC-iOS.release.xcconfig"; sourceTree = "<group>"; };
 		CA6FB8222074601900FC9BF2 /* VLC-iOSUITest-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "VLC-iOSUITest-Bridging-Header.h"; sourceTree = "<group>"; };
 		CAA0B0F620726A0E00B9274E /* TestHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestHelper.swift; sourceTree = "<group>"; };
+		CABCBAFE20EB15EA0040E2F5 /* VLCSettingsTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = VLCSettingsTableViewCell.swift; path = Sources/VLCSettingsTableViewCell.swift; sourceTree = "<group>"; };
 		CABCBB0020EB38580040E2F5 /* IASKSettingsTableViewController+Protected.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "IASKSettingsTableViewController+Protected.h"; path = "Sources/IASKSettingsTableViewController+Protected.h"; sourceTree = "<group>"; };
 		CAD925782075536300F88496 /* Screenshot.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Screenshot.swift; sourceTree = "<group>"; };
 		CAD925802075613100F88496 /* SnapshotHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SnapshotHelper.swift; path = fastlane/SnapshotHelper.swift; sourceTree = SOURCE_ROOT; };
@@ -1540,6 +1542,7 @@
 				7D3784A4183A98F5009EE944 /* VLCSettingsController.h */,
 				7D3784A5183A98F5009EE944 /* VLCSettingsController.m */,
 				CABCBB0020EB38580040E2F5 /* IASKSettingsTableViewController+Protected.h */,
+				CABCBAFE20EB15EA0040E2F5 /* VLCSettingsTableViewCell.swift */,
 				7D37849F183A98EB009EE944 /* VLCBugreporter.h */,
 				7D3784A0183A98EB009EE944 /* VLCBugreporter.m */,
 				7DC19AEB1868C91400810BF7 /* First Steps */,
@@ -3301,6 +3304,7 @@
 				7D18F0A21B34522000651A30 /* VLCActivityManager.m in Sources */,
 				DD3EFEEE1BDEBA3800B68579 /* VLCServerListViewController.m in Sources */,
 				7DB847D71A5871570002DC30 /* VLCOneDriveObject.m in Sources */,
+				CABCBAFF20EB15EA0040E2F5 /* VLCSettingsTableViewCell.swift in Sources */,
 				7D1052E91A4DCC1100295F08 /* VLCOneDriveTableViewController.m in Sources */,
 				DD3EFF591BDEBCE500B68579 /* VLCLocalNetworkServiceUPnP.m in Sources */,
 				7D30F3D7183AB2F100FFC021 /* VLCNetworkListCell.m in Sources */,