slight refactor

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-10-09 13:11:06 -05:00
parent 2e55d2d231
commit 48c9c087f8
2 changed files with 26 additions and 32 deletions

View File

@ -16,12 +16,9 @@ public class ColorPickerView: UIStackView, UIColorPickerViewControllerDelegate {
} }
} }
private var colorPicker: UIColorPickerViewController = { public var onColorSelected: ((UIColor) -> Void)?
let picker = UIColorPickerViewController()
return picker private var selectedColorView: UIView = {
}()
var selectedColorView: UIView = {
let view = UIView() let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false view.translatesAutoresizingMaskIntoConstraints = false
view.widthAnchor.constraint(equalToConstant: 20).isActive = true view.widthAnchor.constraint(equalToConstant: 20).isActive = true
@ -29,27 +26,18 @@ public class ColorPickerView: UIStackView, UIColorPickerViewControllerDelegate {
return view return view
}() }()
var button = Button().with { private var button = Button().with {
$0.size = .small $0.size = .small
$0.use = .secondary $0.use = .secondary
$0.text = "Select" $0.text = "Select"
} }
/// Callback once the user picks a color from the picker.
public var onColorSelected: ((UIColor) -> Void)?
public init(color: UIColor? = nil) { public init(color: UIColor? = nil) {
super.init(frame: .zero) super.init(frame: .zero)
if let color { self.selectedColor = color
selectedColor = color
}
colorPicker.delegate = self
setup() setup()
button.onClick = { _ in
UIApplication.topViewController()?.present(self.colorPicker, animated: true)
}
} }
required init(coder: NSCoder) { required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
@ -58,7 +46,7 @@ public class ColorPickerView: UIStackView, UIColorPickerViewControllerDelegate {
distribution = .fillEqually distribution = .fillEqually
alignment = .fill alignment = .fill
spacing = 10 spacing = 10
let indicatorWrapper = View() let indicatorWrapper = View()
indicatorWrapper.addSubview(selectedColorView) indicatorWrapper.addSubview(selectedColorView)
indicatorWrapper.height(32) indicatorWrapper.height(32)
@ -66,9 +54,8 @@ public class ColorPickerView: UIStackView, UIColorPickerViewControllerDelegate {
indicatorWrapper.pinLeading() indicatorWrapper.pinLeading()
indicatorWrapper.pinBottom() indicatorWrapper.pinBottom()
indicatorWrapper.pinTrailingGreaterThanOrEqualTo(anchor: indicatorWrapper.trailingAnchor) indicatorWrapper.pinTrailingGreaterThanOrEqualTo(anchor: indicatorWrapper.trailingAnchor)
addArrangedSubview(indicatorWrapper) addArrangedSubview(indicatorWrapper)
let buttonWrapper = View() let buttonWrapper = View()
buttonWrapper.addSubview(button) buttonWrapper.addSubview(button)
buttonWrapper.height(32) buttonWrapper.height(32)
@ -77,16 +64,23 @@ public class ColorPickerView: UIStackView, UIColorPickerViewControllerDelegate {
button.pinBottom() button.pinBottom()
button.pinLeadingGreaterThanOrEqualTo(anchor: buttonWrapper.leadingAnchor) button.pinLeadingGreaterThanOrEqualTo(anchor: buttonWrapper.leadingAnchor)
addArrangedSubview(buttonWrapper) addArrangedSubview(buttonWrapper)
button.onClick = { [weak self] _ in
guard let self = self else { return }
let colorPicker = UIColorPickerViewController()
colorPicker.title = "Select a Color"
colorPicker.delegate = self
if let selectedColor = self.selectedColor {
colorPicker.selectedColor = selectedColor
}
UIApplication.topViewController()?.present(colorPicker, animated: true)
}
} }
// MARK: - UIColorPickerViewControllerDelegate // MARK: - UIColorPickerViewControllerDelegate
public func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) { public func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
selectedColor = viewController.selectedColor selectedColor = viewController.selectedColor
onColorSelected?(viewController.selectedColor) onColorSelected?(viewController.selectedColor)
} }
public func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
UIApplication.topViewController()?.dismiss(animated: true)
}
} }

View File

@ -85,19 +85,19 @@ public class TokenColorPickerSection<EnumType: RawRepresentable & CaseIterable>:
let isToken = item.rawValue == "token" let isToken = item.rawValue == "token"
let isCustom = item.rawValue == "custom" let isCustom = item.rawValue == "custom"
if isToken { if isToken {
self.onTokenSelected?(self.tokenColorView.selectedItem) onTokenSelected?(self.tokenColorView.selectedItem)
} else if let selectedItem = self.customColorView.selectedColor, isCustom { } else if let selectedItem = self.customColorView.selectedColor, isCustom {
self.onColorSelected?(selectedItem) onColorSelected?(selectedItem)
} else { } else {
self.onSelected?(item) onSelected?(item)
} }
self.tokenColorRow?.isHidden = !isToken tokenColorRow?.isHidden = !isToken
self.customColorRow?.isHidden = !isCustom customColorRow?.isHidden = !isCustom
} }
tokenColorView.onPickerDidSelect = { [weak self] item in tokenColorView.onPickerDidSelect = { [weak self] item in
guard let self else { return } guard let self else { return }
self.onTokenSelected?(item) onTokenSelected?(item)
} }
} }