LNIMChatOrderMessageCell.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // LNIMChatOrderMessageCell.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/26.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNIMChatOrderMessageCell: UITableViewCell {
  11. private let statusIc = UIImageView()
  12. private let statusLabel = UILabel()
  13. private let tipsLabel = UILabel()
  14. private let gameIc = UIImageView()
  15. private let billInfoLabel = UILabel()
  16. private let menuButton = UIButton()
  17. private var curItem: LNIMMessageData?
  18. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  19. super.init(style: style, reuseIdentifier: reuseIdentifier)
  20. setupViews()
  21. }
  22. func update(_ data: LNIMMessageData, viewModel: LNIMChatViewModel) {
  23. guard let order: LNIMOrderMessage = data.decodeCustomMessage() else { return }
  24. let isCreator = order.buyerUserNo.isMyUid
  25. tipsLabel.isHidden = false
  26. menuButton.isHidden = true
  27. gameIc.sd_setImage(with: URL(string: order.categoryIcon))
  28. billInfoLabel.text = "\(order.bizCategoryName)/\(order.unit) x \(order.purchaseQty)"
  29. curItem = data
  30. let ui = order.statusUI
  31. statusLabel.text = ui.title
  32. statusIc.image = ui.icon
  33. if order.refundApply {
  34. if isCreator {
  35. tipsLabel.text = .init(key: "A00050")
  36. } else {
  37. tipsLabel.text = .init(key: "A00052")
  38. }
  39. return
  40. }
  41. switch order.status {
  42. case .created, .waitingForAccept:
  43. if isCreator {
  44. tipsLabel.text = .init(key: "A00054")
  45. } else {
  46. tipsLabel.text = .init(key: "A00056", order.customerRemark)
  47. }
  48. case .completed:
  49. if isCreator {
  50. tipsLabel.text = .init(key: "A00058")
  51. } else {
  52. tipsLabel.text = .init(key: "A00059")
  53. }
  54. case .refunded:
  55. tipsLabel.text = .init(key: "A00061")
  56. case .accepted:
  57. if isCreator {
  58. tipsLabel.text = .init(key: "A00063")
  59. } else {
  60. tipsLabel.isHidden = true
  61. menuButton.isHidden = false
  62. menuButton.setTitle(.init(key: "A00064"), for: .normal)
  63. }
  64. case .rejected:
  65. if isCreator {
  66. tipsLabel.text = .init(key: "A00066")
  67. } else {
  68. tipsLabel.text = .init(key: "A00067")
  69. }
  70. case .servicing:
  71. tipsLabel.text = .init(key: "A00075")
  72. if !isCreator {
  73. menuButton.isHidden = false
  74. menuButton.setTitle(.init(key: "A00069"), for: .normal)
  75. }
  76. case .serviceDone:
  77. if isCreator {
  78. tipsLabel.text = .init(key: "A00058")
  79. menuButton.isHidden = false
  80. menuButton.setTitle(.init(key: "A00069"), for: .normal)
  81. } else {
  82. tipsLabel.text = .init(key: "A00074")
  83. }
  84. case .cancelled:
  85. if isCreator {
  86. tipsLabel.text = .init(key: "A00072")
  87. } else {
  88. tipsLabel.text = .init(key: "A00073")
  89. }
  90. }
  91. }
  92. required init?(coder: NSCoder) {
  93. fatalError("init(coder:) has not been implemented")
  94. }
  95. }
  96. extension LNIMChatOrderMessageCell {
  97. private func setupViews() {
  98. backgroundColor = .clear
  99. let container = UIView()
  100. container.backgroundColor = .fill
  101. container.layer.cornerRadius = 12
  102. contentView.addSubview(container)
  103. container.snp.makeConstraints { make in
  104. make.horizontalEdges.equalToSuperview().inset(16)
  105. make.top.equalToSuperview()
  106. make.bottom.equalToSuperview().offset(-8)
  107. }
  108. let mainStackView = UIStackView()
  109. mainStackView.axis = .vertical
  110. mainStackView.spacing = 12
  111. container.addSubview(mainStackView)
  112. mainStackView.snp.makeConstraints { make in
  113. make.horizontalEdges.equalToSuperview().inset(12)
  114. make.verticalEdges.equalToSuperview().inset(12)
  115. }
  116. let infoStackView = UIStackView()
  117. infoStackView.axis = .vertical
  118. infoStackView.spacing = 6
  119. infoStackView.onTap { [weak self] in
  120. guard let self else { return }
  121. guard let order: LNIMOrderMessage = curItem?.decodeCustomMessage() else { return }
  122. if order.buyerUserNo.isMyUid {
  123. pushToOrderList()
  124. } else {
  125. pushToOrderRecord()
  126. }
  127. }
  128. mainStackView.addArrangedSubview(infoStackView)
  129. infoStackView.addArrangedSubview(buildStatusView())
  130. infoStackView.addArrangedSubview(buildTipsView())
  131. infoStackView.addArrangedSubview(buildLine())
  132. infoStackView.addArrangedSubview(buildBillInfoView())
  133. mainStackView.addArrangedSubview(buildMenu())
  134. }
  135. private func buildStatusView() -> UIView {
  136. let container = UIView()
  137. container.snp.makeConstraints { make in
  138. make.height.equalTo(24)
  139. }
  140. container.addSubview(statusIc)
  141. statusIc.snp.makeConstraints { make in
  142. make.centerY.equalToSuperview()
  143. make.leading.equalToSuperview()
  144. make.width.height.equalTo(24)
  145. }
  146. statusLabel.font = .heading_h4
  147. statusLabel.textColor = .text_5
  148. container.addSubview(statusLabel)
  149. statusLabel.snp.makeConstraints { make in
  150. make.leading.equalTo(statusIc.snp.trailing).offset(6)
  151. make.centerY.equalToSuperview()
  152. }
  153. let arrow = UIImageView.arrowImageView(size: 16)
  154. arrow.tintColor = .text_4
  155. container.addSubview(arrow)
  156. arrow.snp.makeConstraints { make in
  157. make.centerY.equalToSuperview()
  158. make.trailing.equalToSuperview()
  159. }
  160. return container
  161. }
  162. private func buildTipsView() -> UIView {
  163. tipsLabel.font = .body_xs
  164. tipsLabel.textColor = .text_3
  165. tipsLabel.numberOfLines = 0
  166. return tipsLabel
  167. }
  168. private func buildLine() -> UIView {
  169. let line = UIView()
  170. line.backgroundColor = .fill_2
  171. line.snp.makeConstraints { make in
  172. make.height.equalTo(1)
  173. }
  174. return line
  175. }
  176. private func buildBillInfoView() -> UIView {
  177. let container = UIView()
  178. container.snp.makeConstraints { make in
  179. make.height.equalTo(20)
  180. }
  181. let backgroundIc = UIImageView()
  182. backgroundIc.layer.cornerRadius = 10
  183. backgroundIc.image = .primary_7
  184. backgroundIc.clipsToBounds = true
  185. container.addSubview(backgroundIc)
  186. backgroundIc.snp.makeConstraints { make in
  187. make.leading.equalToSuperview()
  188. make.centerY.equalToSuperview()
  189. make.width.height.equalTo(20)
  190. }
  191. gameIc.backgroundColor = .fill
  192. gameIc.layer.cornerRadius = 9
  193. gameIc.clipsToBounds = true
  194. backgroundIc.addSubview(gameIc)
  195. gameIc.snp.makeConstraints { make in
  196. make.center.equalToSuperview()
  197. make.width.height.equalTo(18)
  198. }
  199. billInfoLabel.font = .body_s
  200. billInfoLabel.textColor = .text_4
  201. container.addSubview(billInfoLabel)
  202. billInfoLabel.snp.makeConstraints { make in
  203. make.leading.equalTo(backgroundIc.snp.trailing).offset(6)
  204. make.centerY.equalToSuperview()
  205. }
  206. return container
  207. }
  208. private func buildMenu() -> UIView {
  209. menuButton.layer.cornerRadius = 20
  210. menuButton.setBackgroundImage(.primary_8, for: .normal)
  211. menuButton.clipsToBounds = true
  212. menuButton.setTitleColor(.text_1, for: .normal)
  213. menuButton.titleLabel?.font = .heading_h3
  214. menuButton.addAction(UIAction(handler: { [weak self] _ in
  215. guard let self else { return }
  216. guard let order: LNIMOrderMessage = curItem?.decodeCustomMessage() else { return }
  217. let isCreator = order.buyerUserNo.isMyUid
  218. switch order.status {
  219. case .accepted:
  220. if !isCreator {
  221. LNOrderManager.shared.startOrderService(orderId: order.orderId) { success in }
  222. }
  223. case .servicing:
  224. if !isCreator {
  225. LNOrderManager.shared.finishOrderService(orderId: order.orderId) { success in }
  226. }
  227. case .serviceDone:
  228. if isCreator {
  229. LNCommonAlertView.showFinishOrderAlert(orderId: order.orderId)
  230. }
  231. default: break
  232. }
  233. }), for: .touchUpInside)
  234. menuButton.snp.makeConstraints { make in
  235. make.height.equalTo(40)
  236. }
  237. return menuButton
  238. }
  239. }