| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- //
- // LNOrderCustomView.swift
- // Lanu
- //
- // Created by OneeChan on 2025/11/27.
- //
- import Foundation
- import UIKit
- import SnapKit
- class LNOrderCustomView: UIView {
- private let showView = LNOrderQRCodeShowView()
-
- private let editView = UIView()
- private let priceLabel = UILabel()
-
- private let minusButton = UIButton()
- private let unitLabel = UILabel()
- private let countLabel = UILabel()
- private let addButton = UIButton()
-
- private let costLabel = UILabel()
- private let costUnitLabel = UILabel()
-
- private var curSkill: LNGameMateSkillVO?
-
- private var customCount = 1 {
- didSet {
- countLabel.text = "\(customCount)"
- if customCount <= 1 {
- minusButton.isEnabled = false
- } else {
- minusButton.isEnabled = true
- }
- updateEditCost()
- }
- }
-
- override init(frame: CGRect) {
- super.init(frame: frame)
-
- setupViews()
- }
-
- func update(_ skill: LNGameMateSkillVO) {
- guard curSkill?.id != skill.id else { return }
- curSkill = skill
-
- showView.isHidden = true
- editView.isHidden = false
-
- priceLabel.text = skill.price.toDisplay
- unitLabel.text = skill.unit
- customCount = 1
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- }
- extension LNOrderCustomView {
- private func updateEditCost() {
- guard let skill = curSkill else { return }
- let cost = skill.price * Double(customCount)
-
- costLabel.text = cost.toDisplay
- // costUnitLabel.text = if customCount <= 1 {
- // "/\(skill.unit)"
- // } else {
- // "/\(skill.unit)x\(customCount)"
- // }
- }
-
- private func setupViews() {
- showView.isHidden = true
- addSubview(showView)
- showView.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview()
- make.top.equalToSuperview()
- }
-
- let editView = buildEditView()
- addSubview(editView)
- editView.snp.makeConstraints { make in
- make.edges.equalToSuperview()
- }
- }
-
- private func buildEditView() -> UIView {
- editView.isHidden = false
-
- let container = UIView()
- container.backgroundColor = .fill
- container.layer.cornerRadius = 16
- editView.addSubview(container)
- container.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(16)
- make.top.equalToSuperview().offset(26)
- }
-
- let priceView = buildPriceView()
- container.addSubview(priceView)
- priceView.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview()
- make.top.equalToSuperview().offset(16)
- }
-
- let line = UIView()
- line.backgroundColor = .fill_4
- container.addSubview(line)
- line.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(16)
- make.top.equalTo(priceView.snp.bottom).offset(6)
- make.height.equalTo(0.5)
- }
-
- let unit = buildEditUnitView()
- container.addSubview(unit)
- unit.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview()
- make.top.equalTo(line.snp.bottom).offset(6)
- make.bottom.equalToSuperview().offset(-12)
- }
-
- let cost = buildCostView()
- editView.addSubview(cost)
- cost.snp.makeConstraints { make in
- make.top.equalTo(container.snp.bottom).offset(16)
- make.trailing.equalToSuperview().offset(-16)
- }
-
- let generateButton = UIButton()
- generateButton.setTitle(.init(key: "A00155"), for: .normal)
- generateButton.titleLabel?.font = .heading_h3
- generateButton.setBackgroundImage(.primary_8, for: .normal)
- generateButton.layer.cornerRadius = 23.5
- generateButton.backgroundColor = .fill_4
- generateButton.clipsToBounds = true
- generateButton.addAction(UIAction(handler: { [weak self] _ in
- guard let self else { return }
- guard let skill = self.curSkill else { return }
-
- self.showView.update(skill, customCount: customCount)
- self.showView.isHidden = false
- self.editView.isHidden = true
- }), for: .touchUpInside)
- editView.addSubview(generateButton)
- generateButton.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(16)
- make.bottom.equalToSuperview().offset(-10)
- make.height.equalTo(47)
- }
-
- return editView
- }
-
- private func buildPriceView() -> UIView {
- let container = UIView()
- container.snp.makeConstraints { make in
- make.height.equalTo(45)
- }
-
- let coin = UIImageView.coinImageView()
- container.addSubview(coin)
- coin.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.leading.equalToSuperview().offset(16)
- make.width.height.equalTo(23)
- }
-
- priceLabel.font = .heading_h1
- priceLabel.textColor = .text_5
- container.addSubview(priceLabel)
- priceLabel.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.leading.equalTo(coin.snp.trailing).offset(3)
- make.trailing.equalToSuperview().offset(-16)
- make.height.equalTo(22)
- }
-
- return container
- }
-
- private func buildEditUnitView() -> UIView {
- let container = UIView()
- container.snp.makeConstraints { make in
- make.height.equalTo(45)
- }
-
- addButton.setTitle("+", for: .normal)
- addButton.setTitleColor(.text_4, for: .normal)
- addButton.setTitleColor(.text_2, for: .disabled)
- addButton.backgroundColor = .primary_1
- addButton.layer.cornerRadius = 12
- addButton.addAction(UIAction(handler: { [weak self] _ in
- guard let self else { return }
- self.customCount += 1
- }), for: .touchUpInside)
- container.addSubview(addButton)
- addButton.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.trailing.equalToSuperview().offset(-12)
- make.width.height.equalTo(24)
- }
-
- countLabel.text = "\(customCount)"
- countLabel.font = .body_m
- countLabel.textColor = .text_5
- container.addSubview(countLabel)
- countLabel.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.trailing.equalTo(addButton.snp.leading).offset(-10)
- }
-
- minusButton.setTitle("-", for: .normal)
- minusButton.setTitleColor(.text_4, for: .normal)
- minusButton.setTitleColor(.text_2, for: .disabled)
- minusButton.backgroundColor = .primary_1
- minusButton.layer.cornerRadius = 12
- minusButton.addAction(UIAction(handler: { [weak self] _ in
- guard let self else { return }
- self.customCount -= 1
- }), for: .touchUpInside)
- container.addSubview(minusButton)
- minusButton.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.trailing.equalTo(countLabel.snp.leading).offset(-10)
- make.width.height.equalTo(24)
- }
-
- unitLabel.font = .body_m
- unitLabel.textColor = .text_5
- container.addSubview(unitLabel)
- unitLabel.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.leading.equalToSuperview().offset(16)
- }
-
- return container
- }
-
- private func buildCostView() -> UIView {
- let container = UIView()
-
- let coin = UIImageView.coinImageView()
- container.addSubview(coin)
- coin.snp.makeConstraints { make in
- make.leading.equalToSuperview()
- make.centerY.equalToSuperview()
- }
-
- costLabel.font = .heading_h2
- costLabel.textColor = .text_5
- container.addSubview(costLabel)
- costLabel.snp.makeConstraints { make in
- make.verticalEdges.equalToSuperview()
- make.leading.equalTo(coin.snp.trailing).offset(2)
- }
-
- costUnitLabel.font = .body_m
- costUnitLabel.textColor = .text_5
- container.addSubview(costUnitLabel)
- costUnitLabel.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.leading.equalTo(costLabel.snp.trailing)
- make.trailing.equalToSuperview()
- }
-
- return container
- }
- }
|