LNOrderCustomView.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // LNOrderCustomView.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/11/27.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNOrderCustomView: UIView {
  11. private let showView = LNOrderQRCodeShowView()
  12. private let editView = UIView()
  13. private let priceLabel = UILabel()
  14. private let minusButton = UIButton()
  15. private let unitLabel = UILabel()
  16. private let countLabel = UILabel()
  17. private let addButton = UIButton()
  18. private let costLabel = UILabel()
  19. private let costUnitLabel = UILabel()
  20. private var curSkill: LNGameMateSkillVO?
  21. private var customCount = 1 {
  22. didSet {
  23. countLabel.text = "\(customCount)"
  24. if customCount <= 1 {
  25. minusButton.isEnabled = false
  26. } else {
  27. minusButton.isEnabled = true
  28. }
  29. updateEditCost()
  30. }
  31. }
  32. override init(frame: CGRect) {
  33. super.init(frame: frame)
  34. setupViews()
  35. }
  36. func update(_ skill: LNGameMateSkillVO) {
  37. guard curSkill?.id != skill.id else { return }
  38. curSkill = skill
  39. showView.isHidden = true
  40. editView.isHidden = false
  41. priceLabel.text = skill.price.toDisplay
  42. unitLabel.text = skill.unit
  43. customCount = 1
  44. }
  45. required init?(coder: NSCoder) {
  46. fatalError("init(coder:) has not been implemented")
  47. }
  48. }
  49. extension LNOrderCustomView {
  50. private func updateEditCost() {
  51. guard let skill = curSkill else { return }
  52. let cost = skill.price * Double(customCount)
  53. costLabel.text = cost.toDisplay
  54. // costUnitLabel.text = if customCount <= 1 {
  55. // "/\(skill.unit)"
  56. // } else {
  57. // "/\(skill.unit)x\(customCount)"
  58. // }
  59. }
  60. private func setupViews() {
  61. showView.isHidden = true
  62. addSubview(showView)
  63. showView.snp.makeConstraints { make in
  64. make.horizontalEdges.equalToSuperview()
  65. make.top.equalToSuperview()
  66. }
  67. let editView = buildEditView()
  68. addSubview(editView)
  69. editView.snp.makeConstraints { make in
  70. make.edges.equalToSuperview()
  71. }
  72. }
  73. private func buildEditView() -> UIView {
  74. editView.isHidden = false
  75. let container = UIView()
  76. container.backgroundColor = .fill
  77. container.layer.cornerRadius = 16
  78. editView.addSubview(container)
  79. container.snp.makeConstraints { make in
  80. make.horizontalEdges.equalToSuperview().inset(16)
  81. make.top.equalToSuperview().offset(26)
  82. }
  83. let priceView = buildPriceView()
  84. container.addSubview(priceView)
  85. priceView.snp.makeConstraints { make in
  86. make.horizontalEdges.equalToSuperview()
  87. make.top.equalToSuperview().offset(16)
  88. }
  89. let line = UIView()
  90. line.backgroundColor = .fill_4
  91. container.addSubview(line)
  92. line.snp.makeConstraints { make in
  93. make.horizontalEdges.equalToSuperview().inset(16)
  94. make.top.equalTo(priceView.snp.bottom).offset(6)
  95. make.height.equalTo(0.5)
  96. }
  97. let unit = buildEditUnitView()
  98. container.addSubview(unit)
  99. unit.snp.makeConstraints { make in
  100. make.horizontalEdges.equalToSuperview()
  101. make.top.equalTo(line.snp.bottom).offset(6)
  102. make.bottom.equalToSuperview().offset(-12)
  103. }
  104. let cost = buildCostView()
  105. editView.addSubview(cost)
  106. cost.snp.makeConstraints { make in
  107. make.top.equalTo(container.snp.bottom).offset(16)
  108. make.trailing.equalToSuperview().offset(-16)
  109. }
  110. let generateButton = UIButton()
  111. generateButton.setTitle(.init(key: "A00155"), for: .normal)
  112. generateButton.titleLabel?.font = .heading_h3
  113. generateButton.setBackgroundImage(.primary_8, for: .normal)
  114. generateButton.layer.cornerRadius = 23.5
  115. generateButton.backgroundColor = .fill_4
  116. generateButton.clipsToBounds = true
  117. generateButton.addAction(UIAction(handler: { [weak self] _ in
  118. guard let self else { return }
  119. guard let skill = self.curSkill else { return }
  120. self.showView.update(skill, customCount: customCount)
  121. self.showView.isHidden = false
  122. self.editView.isHidden = true
  123. }), for: .touchUpInside)
  124. editView.addSubview(generateButton)
  125. generateButton.snp.makeConstraints { make in
  126. make.horizontalEdges.equalToSuperview().inset(16)
  127. make.bottom.equalToSuperview().offset(-10)
  128. make.height.equalTo(47)
  129. }
  130. return editView
  131. }
  132. private func buildPriceView() -> UIView {
  133. let container = UIView()
  134. container.snp.makeConstraints { make in
  135. make.height.equalTo(45)
  136. }
  137. let coin = UIImageView.coinImageView()
  138. container.addSubview(coin)
  139. coin.snp.makeConstraints { make in
  140. make.centerY.equalToSuperview()
  141. make.leading.equalToSuperview().offset(16)
  142. make.width.height.equalTo(23)
  143. }
  144. priceLabel.font = .heading_h1
  145. priceLabel.textColor = .text_5
  146. container.addSubview(priceLabel)
  147. priceLabel.snp.makeConstraints { make in
  148. make.centerY.equalToSuperview()
  149. make.leading.equalTo(coin.snp.trailing).offset(3)
  150. make.trailing.equalToSuperview().offset(-16)
  151. make.height.equalTo(22)
  152. }
  153. return container
  154. }
  155. private func buildEditUnitView() -> UIView {
  156. let container = UIView()
  157. container.snp.makeConstraints { make in
  158. make.height.equalTo(45)
  159. }
  160. addButton.setTitle("+", for: .normal)
  161. addButton.setTitleColor(.text_4, for: .normal)
  162. addButton.setTitleColor(.text_2, for: .disabled)
  163. addButton.backgroundColor = .primary_1
  164. addButton.layer.cornerRadius = 12
  165. addButton.addAction(UIAction(handler: { [weak self] _ in
  166. guard let self else { return }
  167. self.customCount += 1
  168. }), for: .touchUpInside)
  169. container.addSubview(addButton)
  170. addButton.snp.makeConstraints { make in
  171. make.centerY.equalToSuperview()
  172. make.trailing.equalToSuperview().offset(-12)
  173. make.width.height.equalTo(24)
  174. }
  175. countLabel.text = "\(customCount)"
  176. countLabel.font = .body_m
  177. countLabel.textColor = .text_5
  178. container.addSubview(countLabel)
  179. countLabel.snp.makeConstraints { make in
  180. make.centerY.equalToSuperview()
  181. make.trailing.equalTo(addButton.snp.leading).offset(-10)
  182. }
  183. minusButton.setTitle("-", for: .normal)
  184. minusButton.setTitleColor(.text_4, for: .normal)
  185. minusButton.setTitleColor(.text_2, for: .disabled)
  186. minusButton.backgroundColor = .primary_1
  187. minusButton.layer.cornerRadius = 12
  188. minusButton.addAction(UIAction(handler: { [weak self] _ in
  189. guard let self else { return }
  190. self.customCount -= 1
  191. }), for: .touchUpInside)
  192. container.addSubview(minusButton)
  193. minusButton.snp.makeConstraints { make in
  194. make.centerY.equalToSuperview()
  195. make.trailing.equalTo(countLabel.snp.leading).offset(-10)
  196. make.width.height.equalTo(24)
  197. }
  198. unitLabel.font = .body_m
  199. unitLabel.textColor = .text_5
  200. container.addSubview(unitLabel)
  201. unitLabel.snp.makeConstraints { make in
  202. make.centerY.equalToSuperview()
  203. make.leading.equalToSuperview().offset(16)
  204. }
  205. return container
  206. }
  207. private func buildCostView() -> UIView {
  208. let container = UIView()
  209. let coin = UIImageView.coinImageView()
  210. container.addSubview(coin)
  211. coin.snp.makeConstraints { make in
  212. make.leading.equalToSuperview()
  213. make.centerY.equalToSuperview()
  214. }
  215. costLabel.font = .heading_h2
  216. costLabel.textColor = .text_5
  217. container.addSubview(costLabel)
  218. costLabel.snp.makeConstraints { make in
  219. make.verticalEdges.equalToSuperview()
  220. make.leading.equalTo(coin.snp.trailing).offset(2)
  221. }
  222. costUnitLabel.font = .body_m
  223. costUnitLabel.textColor = .text_5
  224. container.addSubview(costUnitLabel)
  225. costUnitLabel.snp.makeConstraints { make in
  226. make.centerY.equalToSuperview()
  227. make.leading.equalTo(costLabel.snp.trailing)
  228. make.trailing.equalToSuperview()
  229. }
  230. return container
  231. }
  232. }