146 lines
4.5 KiB
Swift
146 lines
4.5 KiB
Swift
//
|
|
// RadioSwatchGroupViewController.swift
|
|
// VDSSample
|
|
//
|
|
// Created by Matt Bruce on 8/30/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDS
|
|
import VDSColorTokens
|
|
import Combine
|
|
|
|
class RadioSwatchGroupViewController: ModelScrollViewController<DefaultRadioSwatchGroupModel> {
|
|
|
|
lazy var surfacePickerSelectorView = {
|
|
SurfacePickerSelectorView(picker: self.picker)
|
|
}()
|
|
|
|
var disabledSwitch = UISwitch()
|
|
var strikeThroughSwitch = UISwitch()
|
|
var radioSwatchGroup = RadioSwatchGroup()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
addContentTopView(view: radioSwatchGroup)
|
|
setupForm()
|
|
setupPicker()
|
|
setupModel()
|
|
}
|
|
|
|
func setupForm() {
|
|
addFormRow(label: "Disabled", view: disabledSwitch)
|
|
addFormRow(label: "Surface", view: surfacePickerSelectorView)
|
|
addFormRow(label: "Strikethrough", view: strikeThroughSwitch)
|
|
|
|
disabledSwitch
|
|
.publisher(for: .valueChanged)
|
|
.sink { [weak self] sender in
|
|
self?.radioSwatchGroup.disabled = sender.isOn
|
|
}.store(in: &subscribers)
|
|
|
|
strikeThroughSwitch
|
|
.publisher(for: .valueChanged)
|
|
.sink { [weak self] sender in
|
|
let selectors = self?.model.selectors.compactMap { existing in
|
|
if existing.id == self?.model.selectors.first?.id {
|
|
return existing.copyWith {
|
|
$0.strikethrough = sender.isOn
|
|
}
|
|
} else {
|
|
return existing
|
|
}
|
|
}
|
|
if let selectors {
|
|
self?.model.selectors = selectors
|
|
}
|
|
}.store(in: &subscribers)
|
|
}
|
|
|
|
func setupModel(){
|
|
var defaultModel = DefaultRadioSwatchGroupModel()
|
|
|
|
var model1 = DefaultRadioSwatchModel()
|
|
model1.fillImage = UIImage(named: "imageSwatch")
|
|
model1.text = "Image"
|
|
model1.inputId = "radioSwatch1"
|
|
|
|
var model2 = DefaultRadioSwatchModel()
|
|
model2.primaryColor = .red
|
|
model2.secondaryColor = .blue
|
|
model2.text = "Red/Blue"
|
|
model2.inputId = "radioSwatch2"
|
|
|
|
var model3 = DefaultRadioSwatchModel()
|
|
model3.primaryColor = .green
|
|
model3.text = "Green"
|
|
model3.inputId = "radioSwatch3"
|
|
|
|
var model4 = DefaultRadioSwatchModel()
|
|
model4.primaryColor = .orange
|
|
model4.text = "Orange"
|
|
model4.inputId = "radioSwatch4"
|
|
|
|
var model5 = DefaultRadioSwatchModel()
|
|
model5.primaryColor = .brown
|
|
model5.text = "Brown"
|
|
model5.inputId = "radioSwatch5"
|
|
|
|
var model6 = DefaultRadioSwatchModel()
|
|
model6.primaryColor = .yellow
|
|
model6.text = "Yellow"
|
|
model6.inputId = "radioSwatch6"
|
|
|
|
var model7 = DefaultRadioSwatchModel()
|
|
model7.primaryColor = .purple
|
|
model7.text = "Puple"
|
|
model7.inputId = "radioSwatch7"
|
|
|
|
var model8 = DefaultRadioSwatchModel()
|
|
model8.primaryColor = .systemPink
|
|
model8.text = "Pink"
|
|
model8.inputId = "radioSwatch8"
|
|
|
|
|
|
defaultModel.selectors = [model1, model2, model3, model4, model5, model6, model7, model8]
|
|
set(with: defaultModel)
|
|
|
|
//update the model
|
|
radioSwatchGroup
|
|
.handlerPublisher()
|
|
.sink { [weak self] updatedModel in
|
|
self?.model = updatedModel
|
|
self?.disabledSwitch.isOn = updatedModel.disabled
|
|
}
|
|
.store(in: &subscribers)
|
|
|
|
radioSwatchGroup
|
|
.publisher(for: .valueChanged)
|
|
.sink { group in
|
|
print("Selected: \(group.selectedModelHandler?.text ?? "none")")
|
|
}.store(in: &subscribers)
|
|
|
|
//set UI values
|
|
surfacePickerSelectorView.text = model.surface.rawValue
|
|
disabledSwitch.isOn = model.disabled
|
|
}
|
|
|
|
override func updateView(viewModel: ModelType) {
|
|
print("\(Self.self) updateView(viewModel)")
|
|
disabledSwitch.isOn = viewModel.disabled
|
|
radioSwatchGroup.set(with: viewModel)
|
|
}
|
|
|
|
//Picker
|
|
func setupPicker(){
|
|
|
|
surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
self?.radioSwatchGroup.surface = item
|
|
self?.contentTopView.backgroundColor = item.color
|
|
self?.surfacePickerSelectorView.text = item.rawValue
|
|
}
|
|
|
|
}
|
|
}
|