LNCreateOrderFromSkillListPanel.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //
  2. // LNCreateOrderFromSkillListPanel.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/2.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNCreateOrderFromSkillListPanel: LNPopupView {
  11. private let skillListView = UIStackView()
  12. private let costLabel = UILabel()
  13. private let minusButton = UIButton()
  14. private let countLabel = UILabel()
  15. private let addButton = UIButton()
  16. private var curCount = 1 {
  17. didSet {
  18. countLabel.text = "\(curCount)"
  19. if curCount <= 1 {
  20. minusButton.isEnabled = false
  21. } else {
  22. minusButton.isEnabled = true
  23. }
  24. updatePrice()
  25. }
  26. }
  27. private var skillItemViews: [LNProfileOrderSkillItemView] = []
  28. private var curSelected: LNGameMateSkillVO? {
  29. didSet {
  30. skillItemViews.forEach {
  31. $0.isSelected = $0.cur?.id == curSelected?.id
  32. }
  33. updatePrice()
  34. }
  35. }
  36. override init(frame: CGRect) {
  37. super.init(frame: frame)
  38. setupViews()
  39. }
  40. func update(_ skills: [LNGameMateSkillVO], selected: LNGameMateSkillVO?) {
  41. guard !skills.isEmpty else { return }
  42. skillListView.arrangedSubviews.forEach {
  43. skillListView.removeArrangedSubview($0)
  44. $0.removeFromSuperview()
  45. }
  46. skills.forEach { skill in
  47. let view = LNProfileOrderSkillItemView()
  48. view.update(skill)
  49. view.onTap { [weak self] in
  50. guard let self else { return }
  51. self.curSelected = skill
  52. }
  53. skillListView.addArrangedSubview(view)
  54. skillItemViews.append(view)
  55. }
  56. curSelected = selected ?? skills[0]
  57. curCount = 1
  58. }
  59. required init?(coder: NSCoder) {
  60. fatalError("init(coder:) has not been implemented")
  61. }
  62. }
  63. extension LNCreateOrderFromSkillListPanel {
  64. private func updatePrice() {
  65. guard let skill = curSelected else { return }
  66. let cost = skill.price * Double(curCount)
  67. if let discount = LNOrderManager.shared.discountFor(skill.price) {
  68. costLabel.text = (cost - (1 - discount) * skill.price).toDisplay
  69. } else {
  70. costLabel.text = cost.toDisplay
  71. }
  72. }
  73. private func setupViews() {
  74. let titleLabel = UILabel()
  75. titleLabel.font = .heading_h3
  76. titleLabel.textColor = .text_5
  77. titleLabel.textAlignment = .center
  78. titleLabel.text = .init(key: "A00291")
  79. container.addSubview(titleLabel)
  80. titleLabel.snp.makeConstraints { make in
  81. make.horizontalEdges.equalToSuperview().inset(16)
  82. make.top.equalToSuperview().offset(28)
  83. }
  84. skillListView.axis = .vertical
  85. skillListView.spacing = 8
  86. container.addSubview(skillListView)
  87. skillListView.snp.makeConstraints { make in
  88. make.horizontalEdges.equalToSuperview().inset(16)
  89. make.top.equalTo(titleLabel.snp.bottom).offset(20)
  90. }
  91. let priceView = buildPriceView()
  92. container.addSubview(priceView)
  93. priceView.snp.makeConstraints { make in
  94. make.horizontalEdges.equalToSuperview()
  95. make.top.equalTo(skillListView.snp.bottom).offset(4)
  96. }
  97. let orderView = buildOrderButton()
  98. container.addSubview(orderView)
  99. orderView.snp.makeConstraints { make in
  100. make.horizontalEdges.equalToSuperview()
  101. make.top.equalTo(priceView.snp.bottom).offset(17)
  102. make.bottom.equalTo(commonBottomInset)
  103. }
  104. }
  105. private func buildPriceView() -> UIView {
  106. let container = UIView()
  107. container.snp.makeConstraints { make in
  108. make.height.equalTo(54)
  109. }
  110. let label = UILabel()
  111. label.text = .init(key: "A00122")
  112. label.font = .heading_h3
  113. label.textColor = .text_5
  114. container.addSubview(label)
  115. label.snp.makeConstraints { make in
  116. make.centerY.equalToSuperview()
  117. make.leading.equalToSuperview().offset(20)
  118. }
  119. addButton.setTitle("+", for: .normal)
  120. addButton.setTitleColor(.text_4, for: .normal)
  121. addButton.setTitleColor(.text_2, for: .disabled)
  122. addButton.backgroundColor = .primary_1
  123. addButton.layer.cornerRadius = 12
  124. addButton.addAction(UIAction(handler: { [weak self] _ in
  125. guard let self else { return }
  126. self.curCount += 1
  127. }), for: .touchUpInside)
  128. container.addSubview(addButton)
  129. addButton.snp.makeConstraints { make in
  130. make.centerY.equalToSuperview()
  131. make.trailing.equalToSuperview().offset(-20)
  132. make.width.height.equalTo(24)
  133. }
  134. countLabel.font = .body_m
  135. countLabel.textColor = .text_5
  136. container.addSubview(countLabel)
  137. countLabel.snp.makeConstraints { make in
  138. make.centerY.equalToSuperview()
  139. make.trailing.equalTo(addButton.snp.leading).offset(-10)
  140. }
  141. minusButton.setTitle("-", for: .normal)
  142. minusButton.setTitleColor(.text_4, for: .normal)
  143. minusButton.setTitleColor(.text_2, for: .disabled)
  144. minusButton.backgroundColor = .primary_1
  145. minusButton.layer.cornerRadius = 12
  146. minusButton.addAction(UIAction(handler: { [weak self] _ in
  147. guard let self else { return }
  148. self.curCount -= 1
  149. }), for: .touchUpInside)
  150. container.addSubview(minusButton)
  151. minusButton.snp.makeConstraints { make in
  152. make.centerY.equalToSuperview()
  153. make.trailing.equalTo(countLabel.snp.leading).offset(-10)
  154. make.width.height.equalTo(24)
  155. }
  156. return container
  157. }
  158. private func buildOrderButton() -> UIView {
  159. let container = UIView()
  160. let buttonView = UIView()
  161. buttonView.backgroundColor = .init(hex: "#1789FF14")
  162. buttonView.layer.cornerRadius = 23.5
  163. container.addSubview(buttonView)
  164. buttonView.snp.makeConstraints { make in
  165. make.horizontalEdges.equalToSuperview().inset(16)
  166. make.verticalEdges.equalToSuperview()
  167. make.height.equalTo(47)
  168. }
  169. let orderButton = UIButton()
  170. orderButton.setBackgroundImage(.icSkillOrderLong, for: .normal)
  171. orderButton.setTitle(.init(key: "A00041"), for: .normal)
  172. orderButton.setTitleColor(.text_1, for: .normal)
  173. orderButton.titleLabel?.font = .heading_h3
  174. orderButton.addAction(UIAction(handler: { [weak self] _ in
  175. guard let self else { return }
  176. guard let curSelected else { return }
  177. dismiss()
  178. showLoading()
  179. LNGameMateManager.shared.getSkillDetail(skillId: curSelected.id) { [weak self] detail in
  180. dismissLoading()
  181. guard let self else { return }
  182. guard let detail else { return }
  183. pushToCreateOrder(detail, count: curCount)
  184. }
  185. }), for: .touchUpInside)
  186. buttonView.addSubview(orderButton)
  187. orderButton.snp.makeConstraints { make in
  188. make.trailing.equalToSuperview()
  189. make.verticalEdges.equalToSuperview()
  190. }
  191. let descView = UIView()
  192. buttonView.addSubview(descView)
  193. descView.snp.makeConstraints { make in
  194. make.leading.verticalEdges.equalToSuperview()
  195. make.trailing.equalTo(orderButton.snp.leading)
  196. }
  197. let priceView = UIView()
  198. descView.addSubview(priceView)
  199. priceView.snp.makeConstraints { make in
  200. make.center.equalToSuperview()
  201. }
  202. let coin = UIImageView.coinImageView()
  203. priceView.addSubview(coin)
  204. coin.snp.makeConstraints { make in
  205. make.leading.centerY.equalToSuperview()
  206. make.width.height.equalTo(20)
  207. }
  208. costLabel.font = .heading_h2
  209. costLabel.textColor = .text_5
  210. priceView.addSubview(costLabel)
  211. costLabel.snp.makeConstraints { make in
  212. make.verticalEdges.trailing.equalToSuperview()
  213. make.leading.equalTo(coin.snp.trailing).offset(4)
  214. }
  215. return container
  216. }
  217. }
  218. private class LNProfileOrderSkillItemView: UIView {
  219. private let icon = UIImageView()
  220. private let nameLabel = UILabel()
  221. private let priceLabel = UILabel()
  222. private let discountView = LNNewbieDiscountView()
  223. var cur: LNGameMateSkillVO?
  224. var isSelected: Bool = false {
  225. didSet {
  226. if isSelected {
  227. layer.borderColor = UIColor.init(hex: "#1789FF").cgColor
  228. backgroundColor = .fill_5
  229. } else {
  230. layer.borderColor = UIColor.fill_4.cgColor
  231. backgroundColor = .clear
  232. }
  233. }
  234. }
  235. func update(_ skill: LNGameMateSkillVO) {
  236. icon.sd_setImage(with: URL(string: skill.icon))
  237. nameLabel.text = skill.name
  238. let text: String = .init(key: "A00040", skill.price.toDisplay, skill.unit)
  239. let attrStr = NSMutableAttributedString(string: text)
  240. let range = (text as NSString).range(of: "\(skill.price.toDisplay)")
  241. attrStr.addAttribute(.font, value: UIFont.heading_h4, range: range)
  242. priceLabel.attributedText = attrStr
  243. if let discount = LNOrderManager.shared.discountFor(skill.price) {
  244. discountView.isHidden = false
  245. discountView.update(1 - discount)
  246. } else {
  247. discountView.isHidden = true
  248. }
  249. cur = skill
  250. }
  251. override init(frame: CGRect) {
  252. super.init(frame: frame)
  253. layer.cornerRadius = 12
  254. layer.borderWidth = 1
  255. layer.borderColor = UIColor.fill_4.cgColor
  256. snp.makeConstraints { make in
  257. make.height.equalTo(52)
  258. }
  259. addSubview(icon)
  260. icon.snp.makeConstraints { make in
  261. make.leading.equalToSuperview().offset(14)
  262. make.centerY.equalToSuperview()
  263. make.width.height.equalTo(32)
  264. }
  265. priceLabel.font = .body_s
  266. priceLabel.textColor = .text_4
  267. priceLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
  268. priceLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
  269. addSubview(priceLabel)
  270. priceLabel.snp.makeConstraints { make in
  271. make.centerY.equalToSuperview()
  272. make.trailing.equalToSuperview().offset(-15)
  273. }
  274. discountView.discountOnly = true
  275. addSubview(discountView)
  276. discountView.snp.makeConstraints { make in
  277. make.centerY.equalToSuperview()
  278. make.trailing.equalTo(priceLabel.snp.leading).offset(-8)
  279. }
  280. nameLabel.font = .heading_h4
  281. nameLabel.textColor = .text_5
  282. nameLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
  283. nameLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  284. addSubview(nameLabel)
  285. nameLabel.snp.makeConstraints { make in
  286. make.leading.equalTo(icon.snp.trailing).offset(10)
  287. make.centerY.equalToSuperview()
  288. make.trailing.lessThanOrEqualTo(discountView.snp.leading).offset(-8)
  289. }
  290. }
  291. required init?(coder: NSCoder) {
  292. fatalError("init(coder:) has not been implemented")
  293. }
  294. }
  295. #if DEBUG
  296. import SwiftUI
  297. struct LNProfileOrderPanelPreview: UIViewRepresentable {
  298. func makeUIView(context: Context) -> some UIView {
  299. let container = UIView()
  300. container.backgroundColor = .lightGray
  301. let view = LNCreateOrderFromSkillListPanel()
  302. view.popup(container)
  303. return container
  304. }
  305. func updateUIView(_ uiView: UIViewType, context: Context) { }
  306. }
  307. #Preview(body: {
  308. LNProfileOrderPanelPreview()
  309. })
  310. #endif // DEBUG