LNSkillCreateViewController.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // LNSkillCreateViewController.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/1/25.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. import Combine
  11. extension UIView {
  12. func pushToSkillCreate(skill: LNGameCategoryItemVO, info: LNCreateSkillInputFieldsVO) {
  13. let vc = LNSkillCreateViewController(skill: skill, info: info)
  14. navigationController?.pushViewController(vc, animated: true)
  15. }
  16. }
  17. class LNSkillCreateViewController: LNViewController {
  18. private let skill: LNGameCategoryItemVO
  19. private let scrollView = UIScrollView()
  20. private let info: LNCreateSkillInputFieldsVO
  21. private let editPanel = LNSkillFieldsEditView()
  22. private let confirmButton = UIButton()
  23. init(skill: LNGameCategoryItemVO, info: LNCreateSkillInputFieldsVO) {
  24. self.skill = skill
  25. self.info = info
  26. super.init(nibName: nil, bundle: nil)
  27. }
  28. required init?(coder: NSCoder) {
  29. fatalError("init(coder:) has not been implemented")
  30. }
  31. override func viewDidLoad() {
  32. super.viewDidLoad()
  33. setupViews()
  34. checkConfirmButton()
  35. }
  36. }
  37. extension LNSkillCreateViewController {
  38. private func commit() {
  39. guard editPanel.checkAvailable else {
  40. return
  41. }
  42. showLoading()
  43. DispatchQueue.global().async { [weak self] in
  44. guard let self else {
  45. dismissLoading()
  46. return
  47. }
  48. let voices = info.fields.filter({ $0.type == .voice })
  49. let group = DispatchGroup()
  50. var uploadDone = true
  51. for voice in voices {
  52. if let path = voice.value as? String,
  53. !path.starts(with: "http") {
  54. group.enter()
  55. LNFileUploader.shared.startUpload(type: .voice, fileURL: URL(fileURLWithPath: path), progressHandler: nil)
  56. { url, err in
  57. if let err {
  58. showToast(err)
  59. uploadDone = false
  60. } else if let url {
  61. voice.value = url
  62. } else {
  63. uploadDone = false
  64. }
  65. group.leave()
  66. }
  67. }
  68. }
  69. group.wait()
  70. if !uploadDone {
  71. dismissLoading()
  72. return
  73. }
  74. let input = LNCreateSkillFieldsInfo()
  75. input.bizCategoryCode = skill.code
  76. for item in info.fields {
  77. let config = LNCreateSkillFieldInfo()
  78. config.fieldCode = item.fieldCode
  79. config.value = item.value
  80. if item.type == .voice {
  81. config.duration = item.duration
  82. }
  83. input.fields.append(config)
  84. }
  85. LNGameMateManager.shared.createSkill(info: input) { [weak self] success in
  86. dismissLoading()
  87. guard let self else { return }
  88. guard success else { return }
  89. view.pushToSkillReview(cls: LNSkillCreateViewController.self)
  90. }
  91. }
  92. }
  93. }
  94. extension LNSkillCreateViewController: LNSkillFieldBaseEditViewDelegate {
  95. func onSkillFieldBaseEditViewInputChanged(view: LNSkillFieldBaseEditView) {
  96. checkConfirmButton()
  97. }
  98. }
  99. extension LNSkillCreateViewController {
  100. private func checkConfirmButton() {
  101. let allInput = editPanel.hasAllInput
  102. if allInput != confirmButton.isEnabled {
  103. confirmButton.isEnabled = allInput
  104. confirmButton.setBackgroundImage(allInput ? .primary_8 : nil, for: .normal)
  105. }
  106. }
  107. private func setupViews() {
  108. title = .init(key: "B00093")
  109. view.backgroundColor = .primary_1
  110. view.onTap { [weak self] in
  111. guard let self else { return }
  112. view.endEditing(true)
  113. }
  114. scrollView.showsHorizontalScrollIndicator = false
  115. scrollView.showsVerticalScrollIndicator = false
  116. scrollView.contentInset = .init(top: 12, left: 0, bottom: -view.commonBottomInset + 32, right: 0)
  117. scrollView.adjustKeyoard()
  118. view.addSubview(scrollView)
  119. scrollView.snp.makeConstraints { make in
  120. make.horizontalEdges.equalToSuperview().inset(16)
  121. make.top.equalToSuperview()
  122. make.bottom.equalToSuperview()
  123. }
  124. scrollView.addSubview(editPanel)
  125. editPanel.snp.makeConstraints { make in
  126. make.edges.equalToSuperview()
  127. make.width.equalToSuperview()
  128. }
  129. editPanel.delegate = self
  130. editPanel.update(skill.name, fields: info.fields)
  131. let bottomMenu = UIView()
  132. view.addSubview(bottomMenu)
  133. bottomMenu.snp.makeConstraints { make in
  134. make.horizontalEdges.equalToSuperview()
  135. make.bottom.equalToSuperview()
  136. }
  137. let bottomGradient = CAGradientLayer()
  138. bottomGradient.colors = [
  139. UIColor.white.withAlphaComponent(0).cgColor,
  140. UIColor.white.cgColor,
  141. UIColor.white.cgColor
  142. ]
  143. bottomGradient.locations = [0, 0.5, 1]
  144. bottomGradient.startPoint = .init(x: 0, y: 0)
  145. bottomGradient.endPoint = .init(x: 0, y: 1)
  146. bottomMenu.layer.addSublayer(bottomGradient)
  147. bottomMenu.publisher(for: \.bounds).removeDuplicates().sink { [weak bottomGradient] newValue in
  148. guard let bottomGradient else { return }
  149. bottomGradient.frame = newValue
  150. }.store(in: &bag)
  151. confirmButton.setTitle(.init(key: "A00002"), for: .normal)
  152. confirmButton.setTitleColor(.text_1, for: .normal)
  153. confirmButton.titleLabel?.font = .heading_h3
  154. confirmButton.layer.cornerRadius = 23.5
  155. confirmButton.backgroundColor = .fill_4
  156. confirmButton.clipsToBounds = true
  157. confirmButton.isEnabled = false
  158. confirmButton.addAction(UIAction(handler: { [weak self] _ in
  159. guard let self else { return }
  160. commit()
  161. }), for: .touchUpInside)
  162. bottomMenu.addSubview(confirmButton)
  163. confirmButton.snp.makeConstraints { make in
  164. make.horizontalEdges.equalToSuperview().inset(16)
  165. make.bottom.equalToSuperview().offset(view.commonBottomInset)
  166. make.top.equalToSuperview()
  167. make.height.equalTo(47)
  168. }
  169. }
  170. }