LNSingleSelectionPanel.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // LNSingleSelectionPanel.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/1/20.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. import Combine
  11. class LNSingleSelectionPanel: LNPopupView {
  12. private let titleLabel = UILabel()
  13. private let descLabel = UILabel()
  14. private let pickerView = UIPickerView()
  15. private let confirmButton = UIButton()
  16. private var selections: [String] = []
  17. var handler: ((Int) -> Void)?
  18. override init(frame: CGRect) {
  19. super.init(frame: frame)
  20. setupViews()
  21. }
  22. func setTitles(_ title: String, desc: String?) {
  23. titleLabel.text = title
  24. descLabel.text = desc
  25. descLabel.isHidden = desc?.isEmpty != false
  26. }
  27. func update(_ selections: [String], curSelect: String? = nil) {
  28. self.selections = selections
  29. pickerView.reloadAllComponents()
  30. if let curSelect,
  31. let index = selections.firstIndex(of: curSelect) {
  32. pickerView.selectRow(index, inComponent: 0, animated: false)
  33. }
  34. }
  35. required init?(coder: NSCoder) {
  36. fatalError("init(coder:) has not been implemented")
  37. }
  38. }
  39. extension LNSingleSelectionPanel: UIPickerViewDataSource, UIPickerViewDelegate {
  40. func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  41. selections.count
  42. }
  43. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  44. 1
  45. }
  46. func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
  47. let label = UILabel()
  48. label.font = .systemFont(ofSize: 22)
  49. label.textColor = .text_5
  50. label.text = selections[row]
  51. label.textAlignment = .center
  52. return label
  53. }
  54. func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
  55. 52
  56. }
  57. }
  58. extension LNSingleSelectionPanel {
  59. private func setupViews() {
  60. let headerView = buildHeader()
  61. container.addSubview(headerView)
  62. headerView.snp.makeConstraints { make in
  63. make.horizontalEdges.equalToSuperview()
  64. make.top.equalToSuperview()
  65. }
  66. pickerView.dataSource = self
  67. pickerView.delegate = self
  68. container.addSubview(pickerView)
  69. pickerView.snp.makeConstraints { make in
  70. make.horizontalEdges.equalToSuperview()
  71. make.top.equalTo(headerView.snp.bottom)
  72. make.height.equalTo(240)
  73. }
  74. confirmButton.setTitle(.init(key: "A00223"), for: .normal)
  75. confirmButton.setTitleColor(.text_1, for: .normal)
  76. confirmButton.titleLabel?.font = .heading_h3
  77. confirmButton.layer.cornerRadius = 23.5
  78. confirmButton.clipsToBounds = true
  79. confirmButton.backgroundColor = .fill_4
  80. confirmButton.setBackgroundImage(.primary_8, for: .normal)
  81. confirmButton.addAction(UIAction(handler: { [weak self] _ in
  82. guard let self else { return }
  83. dismiss()
  84. handler?(pickerView.selectedRow(inComponent: 0))
  85. }), for: .touchUpInside)
  86. container.addSubview(confirmButton)
  87. confirmButton.snp.makeConstraints { make in
  88. make.horizontalEdges.equalToSuperview().inset(16)
  89. make.top.equalTo(pickerView.snp.bottom).offset(10)
  90. make.bottom.equalToSuperview().offset(commonBottomInset)
  91. make.height.equalTo(47)
  92. }
  93. }
  94. private func buildHeader() -> UIView {
  95. let container = UIView()
  96. container.snp.makeConstraints { make in
  97. make.height.equalTo(56)
  98. }
  99. let stackView = UIStackView()
  100. stackView.axis = .vertical
  101. stackView.spacing = 3
  102. container.addSubview(stackView)
  103. stackView.snp.makeConstraints { make in
  104. make.centerY.equalToSuperview()
  105. make.horizontalEdges.equalToSuperview()
  106. }
  107. titleLabel.font = .heading_h3
  108. titleLabel.textColor = .text_5
  109. titleLabel.textAlignment = .center
  110. stackView.addArrangedSubview(titleLabel)
  111. descLabel.font = .body_s
  112. descLabel.textColor = .text_3
  113. descLabel.textAlignment = .center
  114. stackView.addArrangedSubview(descLabel)
  115. return container
  116. }
  117. }