LNRoomGiftListView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // LNRoomGiftListView.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/3/23.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. import Combine
  11. protocol LNRoomGiftListViewDelegate: AnyObject {
  12. func onRoomGiftListView(_ view: LNRoomGiftListView, didSelect index: Int)
  13. }
  14. class LNRoomGiftListView: UIView {
  15. private let collectionView: UICollectionView
  16. private var selectedIndex: Int?
  17. var selectedGift: LNGiftItemVO? {
  18. if let selectedIndex {
  19. giftList[selectedIndex]
  20. } else {
  21. nil
  22. }
  23. }
  24. private var roomSession: LNRoomViewModel?
  25. private var giftList: [LNGiftItemVO] = []
  26. weak var delegate: LNRoomGiftListViewDelegate?
  27. override init(frame: CGRect) {
  28. let layout = UICollectionViewFlowLayout()
  29. layout.scrollDirection = .vertical
  30. layout.minimumLineSpacing = 10
  31. layout.minimumInteritemSpacing = 0
  32. collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  33. super.init(frame: frame)
  34. setupViews()
  35. }
  36. func update(_ room: LNRoomViewModel?) {
  37. roomSession = room
  38. giftList = roomSession?.giftList ?? [] // 拷贝一份,避免房间数据更新后导致界面异常
  39. selectedIndex = nil
  40. collectionView.reloadData()
  41. }
  42. required init?(coder: NSCoder) {
  43. fatalError("init(coder:) has not been implemented")
  44. }
  45. }
  46. extension LNRoomGiftListView: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  47. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  48. giftList.count
  49. }
  50. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  51. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LNRoomGiftItemCell.className, for: indexPath) as! LNRoomGiftItemCell
  52. cell.update(giftList[indexPath.item])
  53. cell.isSelected = selectedIndex == indexPath.item
  54. return cell
  55. }
  56. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  57. if selectedIndex == indexPath.item { return }
  58. if let selectedIndex {
  59. collectionView.cellForItem(at: .init(row: selectedIndex, section: 0))?.isSelected = false
  60. }
  61. selectedIndex = indexPath.item
  62. collectionView.cellForItem(at: indexPath)?.isSelected = true
  63. (collectionView.cellForItem(at: indexPath) as? LNRoomGiftItemCell)?.showJumpAnimate()
  64. delegate?.onRoomGiftListView(self, didSelect: indexPath.item)
  65. }
  66. func collectionView(_ collectionView: UICollectionView,
  67. layout collectionViewLayout: UICollectionViewLayout,
  68. sizeForItemAt indexPath: IndexPath) -> CGSize
  69. {
  70. .init(width: 90, height: 96)
  71. }
  72. }
  73. private extension LNRoomGiftListView {
  74. func setupViews() {
  75. collectionView.delegate = self
  76. collectionView.dataSource = self
  77. collectionView.backgroundColor = .clear
  78. collectionView.allowsMultipleSelection = false
  79. collectionView.showsVerticalScrollIndicator = false
  80. collectionView.contentInset = .init(top: 0, left: 7.5, bottom: 32, right: 7.5)
  81. collectionView.register(LNRoomGiftItemCell.self, forCellWithReuseIdentifier: LNRoomGiftItemCell.className)
  82. addSubview(collectionView)
  83. collectionView.snp.makeConstraints { make in
  84. make.edges.equalToSuperview()
  85. }
  86. let bottomGradientView = UIView.gradientView([
  87. .init(hex: "#24213800"), .init(hex: "#242138")
  88. ], .verticalUTD)
  89. addSubview(bottomGradientView)
  90. bottomGradientView.snp.makeConstraints { make in
  91. make.horizontalEdges.equalToSuperview()
  92. make.bottom.equalToSuperview()
  93. make.height.equalTo(44)
  94. }
  95. }
  96. }