|
| 1 | +// |
| 2 | +// SettingsCells.swift |
| 3 | +// APIExample |
| 4 | +// |
| 5 | +// Created by ZQZ on 2020/11/28. |
| 6 | +// Copyright © 2020 Agora Corp. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +class SettingsBaseCell : UITableViewCell |
| 12 | +{ |
| 13 | + var configs:SettingsBaseParam? |
| 14 | + weak var delegate:SettingsViewControllerDelegate? |
| 15 | + func configure(configs:SettingsBaseParam){ |
| 16 | + self.configs = configs |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +class SettingsBaseParam: NSObject |
| 21 | +{ |
| 22 | + var key:String |
| 23 | + var label:String |
| 24 | + var type:String |
| 25 | + |
| 26 | + init(key:String, label:String, type:String) { |
| 27 | + self.key = key |
| 28 | + self.label = label |
| 29 | + self.type = type |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +class SettingsSliderCell : SettingsBaseCell |
| 34 | +{ |
| 35 | + @IBOutlet weak var settingLabel: UILabel! |
| 36 | + @IBOutlet weak var settingSlider: UISlider! |
| 37 | + @IBOutlet weak var settingValue: UILabel! |
| 38 | + |
| 39 | + @IBAction func onSliderValueChanged(sender:UISlider){ |
| 40 | + let val = (sender.value*100).rounded()/100 |
| 41 | + settingValue.text = "\(val)" |
| 42 | + guard let configs = self.configs as? SettingsSliderParam else {return} |
| 43 | + delegate?.didChangeValue(type: "SettingsSliderCell", key: configs.key, value: val) |
| 44 | + } |
| 45 | + |
| 46 | + override func configure(configs: SettingsBaseParam) { |
| 47 | + super.configure(configs: configs) |
| 48 | + |
| 49 | + guard let param = configs as? SettingsSliderParam else {return} |
| 50 | + settingLabel.text = param.label |
| 51 | + settingSlider.value = param.value |
| 52 | + settingSlider.minimumValue = param.minimumValue |
| 53 | + settingSlider.maximumValue = param.maximumValue |
| 54 | + settingValue.text = "\(settingSlider.value)" |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +class SettingsSliderParam: SettingsBaseParam { |
| 59 | + var value:Float |
| 60 | + var minimumValue:Float |
| 61 | + var maximumValue:Float |
| 62 | + init(key:String, label:String, value:Float, minimumValue:Float, maximumValue:Float) { |
| 63 | + self.value = value |
| 64 | + self.minimumValue = minimumValue |
| 65 | + self.maximumValue = maximumValue |
| 66 | + super.init(key: key, label: label, type: "SliderCell") |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +class SettingsLabelCell : SettingsBaseCell |
| 72 | +{ |
| 73 | + @IBOutlet weak var settingLabel: UILabel! |
| 74 | + @IBOutlet weak var settingValue: UILabel! |
| 75 | + |
| 76 | + override func configure(configs: SettingsBaseParam) { |
| 77 | + super.configure(configs: configs) |
| 78 | + |
| 79 | + guard let param = configs as? SettingsLabelParam else {return} |
| 80 | + settingLabel.text = param.label |
| 81 | + settingValue.text = param.value |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +class SettingsLabelParam: SettingsBaseParam { |
| 86 | + var value:String |
| 87 | + init(key:String, label:String, value:String) { |
| 88 | + self.value = value |
| 89 | + super.init(key: key, label: label, type: "LabelCell") |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +class SettingsSelectCell : SettingsBaseCell |
| 94 | +{ |
| 95 | + @IBOutlet weak var settingLabel: UILabel! |
| 96 | + @IBOutlet weak var settingBtn: UIButton! |
| 97 | + |
| 98 | + override func configure(configs: SettingsBaseParam) { |
| 99 | + super.configure(configs: configs) |
| 100 | + |
| 101 | + guard let param = configs as? SettingsSelectParam else {return} |
| 102 | + settingLabel.text = param.label |
| 103 | + settingBtn.setTitle(param.value, for: .normal) |
| 104 | + } |
| 105 | + |
| 106 | + func getSelectAction(_ option:SettingItemOption) -> UIAlertAction{ |
| 107 | + return UIAlertAction(title: "\(option.label)", style: .default, handler: {[unowned self] action in |
| 108 | + guard let param = self.configs as? SettingsSelectParam else {return} |
| 109 | + self.settingBtn.setTitle(option.label, for: .normal) |
| 110 | + param.settingItem.selected = option.idx |
| 111 | + self.delegate?.didChangeValue(type: "SettingsSelectCell", key: param.key, value: param.settingItem) |
| 112 | + }) |
| 113 | + } |
| 114 | + |
| 115 | + @IBAction func onSelect(_ sender:UIButton) { |
| 116 | + let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) |
| 117 | + guard let param = configs as? SettingsSelectParam else {return} |
| 118 | + for option in param.settingItem.options { |
| 119 | + alert.addAction(getSelectAction(option)) |
| 120 | + } |
| 121 | + alert.addCancelAction() |
| 122 | + param.context?.present(alert, animated: true, completion: nil) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +class SettingsSelectParam: SettingsBaseParam { |
| 127 | + var value:String |
| 128 | + var settingItem:SettingItem |
| 129 | + weak var context:UIViewController?; |
| 130 | + init(key:String, label:String, settingItem:SettingItem, context:UIViewController) { |
| 131 | + self.settingItem = settingItem |
| 132 | + self.context = context |
| 133 | + self.value = settingItem.selectedOption().label |
| 134 | + super.init(key: key, label: label, type: "SelectCell") |
| 135 | + } |
| 136 | +} |
0 commit comments