LNRoomJoinMenuView.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // LNRoomJoinMenuView.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/3/9.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. private enum LNRoomJoinMenuState {
  11. case normal
  12. case appling
  13. case reviewing
  14. case onMic
  15. }
  16. class LNRoomJoinMenuView: UIView {
  17. private let joinView = UIStackView()
  18. private let bgImageView = UIImageView()
  19. private let seatIc = UIImageView()
  20. private let titleLabel = UILabel()
  21. private let redCountView = UIView()
  22. private let redCountLabel = UILabel()
  23. private var curState: LNRoomJoinMenuState = .normal {
  24. didSet {
  25. switch curState {
  26. case .normal: toBeOffMic()
  27. case .appling: toBePending()
  28. case .reviewing: toBeRequests()
  29. case .onMic: toBeOnMic()
  30. }
  31. }
  32. }
  33. private weak var roomSession: LNRoomViewModel?
  34. override init(frame: CGRect) {
  35. super.init(frame: frame)
  36. setupViews()
  37. toBeOffMic()
  38. LNEventDeliver.addObserver(self)
  39. }
  40. func update(_ room: LNRoomViewModel?) {
  41. roomSession = room
  42. onRoomSeatApplyChanged()
  43. }
  44. required init?(coder: NSCoder) {
  45. fatalError("init(coder:) has not been implemented")
  46. }
  47. }
  48. extension LNRoomJoinMenuView: LNRoomViewModelNotify {
  49. func onRoomSeatsChanged() {
  50. checkState()
  51. }
  52. func onRoomSeatApplyChanged() {
  53. checkState()
  54. }
  55. func onMySeatApplyChanged() {
  56. checkState()
  57. }
  58. private func checkState() {
  59. guard let roomSession else { return }
  60. redCountView.isHidden = true
  61. if roomSession.mySeatInfo == nil { // 还没上麦
  62. if roomSession.waitingForSeat != .none {
  63. curState = .appling
  64. redCountView.isHidden = roomSession.seatApplyCount == 0
  65. } else {
  66. curState = .normal
  67. }
  68. } else { // 在麦上
  69. if roomSession.mySeatInfo?.index == .host {
  70. curState = .reviewing
  71. redCountView.isHidden = roomSession.seatApplyCount == 0
  72. } else {
  73. curState = .onMic
  74. }
  75. }
  76. redCountLabel.text = "\(roomSession.seatApplyCount)"
  77. }
  78. }
  79. extension LNRoomJoinMenuView {
  80. private func setupViews() {
  81. LNRoomOrderGuideView.joinView = self
  82. backgroundColor = .fill.withAlphaComponent(0.15)
  83. layer.cornerRadius = 15
  84. onTap { [weak self] in
  85. guard let self else { return }
  86. guard let roomSession else { return }
  87. switch curState {
  88. case .normal:
  89. let panel = LNRoomApplySeatPanel()
  90. panel.update(roomSession)
  91. panel.popup(self)
  92. case .appling:
  93. let panel = LNRoomApplySeatListPanel()
  94. panel.update(roomSession)
  95. panel.popup(self)
  96. break
  97. case .reviewing:
  98. let panel = LNRoomManageSeatListPanel()
  99. panel.update(roomSession)
  100. panel.popup(self)
  101. break
  102. case .onMic:
  103. roomSession.leaveSeat { _ in }
  104. break
  105. }
  106. }
  107. snp.makeConstraints { make in
  108. make.width.greaterThanOrEqualTo(69)
  109. }
  110. bgImageView.image = .primary_8
  111. bgImageView.layer.cornerRadius = 15
  112. bgImageView.clipsToBounds = true
  113. addSubview(bgImageView)
  114. bgImageView.snp.makeConstraints { make in
  115. make.edges.equalToSuperview()
  116. }
  117. joinView.isUserInteractionEnabled = false
  118. joinView.spacing = 4
  119. addSubview(joinView)
  120. joinView.snp.makeConstraints { make in
  121. make.centerX.equalToSuperview()
  122. make.leading.greaterThanOrEqualToSuperview().offset(12)
  123. make.centerY.equalToSuperview()
  124. }
  125. seatIc.image = .icSeat
  126. joinView.addArrangedSubview(seatIc)
  127. seatIc.snp.makeConstraints { make in
  128. make.width.height.equalTo(16)
  129. }
  130. titleLabel.font = .heading_h5
  131. titleLabel.textColor = .text_1
  132. joinView.addArrangedSubview(titleLabel)
  133. redCountView.backgroundColor = .fill_6
  134. redCountView.layer.cornerRadius = 7
  135. addSubview(redCountView)
  136. redCountView.snp.makeConstraints { make in
  137. make.trailing.equalToSuperview()
  138. make.centerY.equalTo(snp.top)
  139. make.width.greaterThanOrEqualTo(15)
  140. }
  141. redCountLabel.font = .body_xs
  142. redCountLabel.textColor = .text_1
  143. redCountLabel.textAlignment = .center
  144. redCountView.addSubview(redCountLabel)
  145. redCountLabel.snp.makeConstraints { make in
  146. make.horizontalEdges.equalToSuperview().inset(4)
  147. make.verticalEdges.equalToSuperview()
  148. }
  149. }
  150. private func toBeOffMic() {
  151. bgImageView.isHidden = false
  152. seatIc.isHidden = false
  153. titleLabel.text = .init(key: "A00322")
  154. }
  155. private func toBePending() {
  156. bgImageView.isHidden = true
  157. seatIc.isHidden = true
  158. titleLabel.text = .init(key: "A00323")
  159. }
  160. private func toBeRequests() {
  161. bgImageView.isHidden = true
  162. seatIc.isHidden = true
  163. titleLabel.text = .init(key: "A00324")
  164. }
  165. private func toBeOnMic() {
  166. bgImageView.isHidden = true
  167. seatIc.isHidden = true
  168. titleLabel.text = .init(key: "A00386")
  169. }
  170. }