| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- //
- // LNSingleSelectionPanel.swift
- // Gami
- //
- // Created by OneeChan on 2026/1/20.
- //
- import Foundation
- import UIKit
- import SnapKit
- import Combine
- class LNSingleSelectionPanel: LNPopupView {
- private let titleLabel = UILabel()
- private let descLabel = UILabel()
- private let pickerView = UIPickerView()
- private let confirmButton = UIButton()
-
- private var selections: [String] = []
-
- var handler: ((Int) -> Void)?
-
- override init(frame: CGRect) {
- super.init(frame: frame)
-
- setupViews()
- }
-
-
- func setTitles(_ title: String, desc: String?) {
- titleLabel.text = title
-
- descLabel.text = desc
- descLabel.isHidden = desc?.isEmpty != false
- }
-
- func update(_ selections: [String], curSelect: String? = nil) {
- self.selections = selections
- pickerView.reloadAllComponents()
-
- if let curSelect,
- let index = selections.firstIndex(of: curSelect) {
- pickerView.selectRow(index, inComponent: 0, animated: false)
- }
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- }
- extension LNSingleSelectionPanel: UIPickerViewDataSource, UIPickerViewDelegate {
- func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
- selections.count
- }
-
- func numberOfComponents(in pickerView: UIPickerView) -> Int {
- 1
- }
-
- func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
- let label = UILabel()
- label.font = .systemFont(ofSize: 22)
- label.textColor = .text_5
- label.text = selections[row]
- label.textAlignment = .center
-
- return label
- }
-
- func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
- 52
- }
- }
- extension LNSingleSelectionPanel {
- private func setupViews() {
- let headerView = buildHeader()
- container.addSubview(headerView)
- headerView.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview()
- make.top.equalToSuperview()
- }
-
- pickerView.dataSource = self
- pickerView.delegate = self
- container.addSubview(pickerView)
- pickerView.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview()
- make.top.equalTo(headerView.snp.bottom)
- make.height.equalTo(240)
- }
-
- confirmButton.setTitle(.init(key: "A00223"), for: .normal)
- confirmButton.setTitleColor(.text_1, for: .normal)
- confirmButton.titleLabel?.font = .heading_h3
- confirmButton.layer.cornerRadius = 23.5
- confirmButton.clipsToBounds = true
- confirmButton.backgroundColor = .fill_4
- confirmButton.setBackgroundImage(.primary_8, for: .normal)
- confirmButton.addAction(UIAction(handler: { [weak self] _ in
- guard let self else { return }
- dismiss()
- handler?(pickerView.selectedRow(inComponent: 0))
- }), for: .touchUpInside)
- container.addSubview(confirmButton)
- confirmButton.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(16)
- make.top.equalTo(pickerView.snp.bottom).offset(10)
- make.bottom.equalToSuperview().offset(commonBottomInset)
- make.height.equalTo(47)
- }
- }
-
- private func buildHeader() -> UIView {
- let container = UIView()
- container.snp.makeConstraints { make in
- make.height.equalTo(56)
- }
-
- let stackView = UIStackView()
- stackView.axis = .vertical
- stackView.spacing = 3
- container.addSubview(stackView)
- stackView.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.horizontalEdges.equalToSuperview()
- }
-
- titleLabel.font = .heading_h3
- titleLabel.textColor = .text_5
- titleLabel.textAlignment = .center
- stackView.addArrangedSubview(titleLabel)
-
- descLabel.font = .body_s
- descLabel.textColor = .text_3
- descLabel.textAlignment = .center
- stackView.addArrangedSubview(descLabel)
-
- return container
- }
- }
|