LNRoomMessageView.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // LNRoomPublicBoardView.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/3/9.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. import Combine
  11. class LNRoomMessageView: UIView {
  12. private let tableView = UITableView()
  13. private let maxMessageCount = 300
  14. private weak var roomSession: LNRoomViewModel?
  15. private var items: [LNRoomMessageItem] = []
  16. override init(frame: CGRect) {
  17. super.init(frame: frame)
  18. setupViews()
  19. LNEventDeliver.addObserver(self)
  20. }
  21. func update(_ room: LNRoomViewModel?) {
  22. roomSession = room
  23. }
  24. required init?(coder: NSCoder) {
  25. fatalError("init(coder:) has not been implemented")
  26. }
  27. }
  28. extension LNRoomMessageView: LNRoomViewModelNotify {
  29. func onRoomMessageChanged(messages: [LNRoomMessageItem]) {
  30. items.append(contentsOf: messages)
  31. if items.count > maxMessageCount {
  32. items.removeFirst(Int(Double(maxMessageCount) * 0.3))
  33. }
  34. tableView.reloadData()
  35. tableView.scrollToBottom()
  36. }
  37. }
  38. extension LNRoomMessageView: UITableViewDataSource {
  39. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  40. items.count
  41. }
  42. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  43. let message = items[indexPath.row]
  44. if let userMessage = message as? LNRoomChatMessageItem {
  45. let cell = tableView.dequeueReusableCell(withIdentifier: LNRoomChatMessageCell.className, for: indexPath) as! LNRoomChatMessageCell
  46. cell.update(userMessage)
  47. return cell
  48. } else if let welcomeMessage = message as? LNRoomWelcomeMessageItem {
  49. let cell = tableView.dequeueReusableCell(withIdentifier: LNRoomWelcomeMessageCell.className, for: indexPath) as! LNRoomWelcomeMessageCell
  50. cell.update(welcomeMessage)
  51. return cell
  52. } else if let giftMessage = message as? LNRoomGiftMessageItem {
  53. let cell = tableView.dequeueReusableCell(withIdentifier: LNRoomGiftMessageCell.className, for: indexPath) as! LNRoomGiftMessageCell
  54. cell.update(giftMessage)
  55. return cell
  56. }
  57. return tableView.dequeueReusableCell(withIdentifier: LNRoomUnknownMessageCell.className, for: indexPath)
  58. }
  59. }
  60. extension LNRoomMessageView {
  61. private func setupViews() {
  62. tableView.dataSource = self
  63. tableView.allowsSelection = false
  64. tableView.separatorStyle = .none
  65. tableView.backgroundColor = .clear
  66. tableView.alwaysBounceVertical = true
  67. tableView.showsVerticalScrollIndicator = false
  68. tableView.contentInsetAdjustmentBehavior = .never
  69. tableView.contentInset = .init(top: 0, left: 0, bottom: 16, right: 0)
  70. tableView.register(LNRoomChatMessageCell.self, forCellReuseIdentifier: LNRoomChatMessageCell.className)
  71. tableView.register(LNRoomGiftMessageCell.self, forCellReuseIdentifier: LNRoomGiftMessageCell.className)
  72. tableView.register(LNRoomSystemMessageCell.self, forCellReuseIdentifier: LNRoomSystemMessageCell.className)
  73. tableView.register(LNRoomWelcomeMessageCell.self, forCellReuseIdentifier: LNRoomWelcomeMessageCell.className)
  74. tableView.register(LNRoomUnknownMessageCell.self, forCellReuseIdentifier: LNRoomUnknownMessageCell.className)
  75. addSubview(tableView)
  76. tableView.snp.makeConstraints { make in
  77. make.edges.equalToSuperview()
  78. }
  79. let bottomGradientView = UIView.gradientView([
  80. .init(hex: "#010B2300"), .init(hex: "#010B23")
  81. ], .verticalUTD)
  82. addSubview(bottomGradientView)
  83. bottomGradientView.snp.makeConstraints { make in
  84. make.horizontalEdges.equalToSuperview()
  85. make.bottom.equalToSuperview()
  86. make.height.equalTo(44)
  87. }
  88. }
  89. }