KeyboardHelpView.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // KeyboardHelpView.swift
  3. // AiKeyboard
  4. //
  5. // Created by Destiny on 2025/4/23.
  6. //
  7. import UIKit
  8. protocol KeyboardHelpViewDelegate: NSObjectProtocol {
  9. func helpCollectionViewDidSelectItem(characterId: String, content: String, complete: @escaping ((Bool) -> ()))
  10. func addCharacterJump()
  11. }
  12. class KeyboardHelpView: KeyboardBaseView {
  13. struct UX {
  14. static let cellWidth = (KeyboardConst.kb_kScreenW - 96.0) / 3.0
  15. static let cellHeight = 46.0
  16. }
  17. weak var helpDelegate: KeyboardHelpViewDelegate?
  18. var characterList: [CharacterModel]? {
  19. didSet {
  20. self.collectionView.reloadData()
  21. }
  22. }
  23. lazy var collectionView: UICollectionView = {
  24. let layout = UICollectionViewFlowLayout()
  25. layout.minimumLineSpacing = 0
  26. layout.scrollDirection = .vertical
  27. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  28. collectionView.backgroundColor = .clear
  29. collectionView.dataSource = self
  30. collectionView.delegate = self
  31. collectionView.showsVerticalScrollIndicator = false
  32. collectionView.bounces = false
  33. collectionView.register(KeyboardCharacterCell.self, forCellWithReuseIdentifier: KeyboardCharacterCell.reuseIdentifier())
  34. return collectionView
  35. }()
  36. override init(frame: CGRect) {
  37. super.init(frame: frame)
  38. initUI()
  39. }
  40. required init?(coder: NSCoder) {
  41. fatalError("init(coder:) has not been implemented")
  42. }
  43. }
  44. extension KeyboardHelpView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  45. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  46. return (self.characterList?.count ?? 0) + 1
  47. }
  48. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  49. if indexPath.row == self.characterList?.count {
  50. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KeyboardCharacterCell.reuseIdentifier(), for: indexPath) as! KeyboardCharacterCell
  51. cell.titleLabel.isHidden = true
  52. cell.addBtn.isHidden = false
  53. return cell
  54. } else {
  55. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KeyboardCharacterCell.reuseIdentifier(), for: indexPath) as! KeyboardCharacterCell
  56. cell.titleLabel.isHidden = false
  57. cell.addBtn.isHidden = true
  58. if let model = self.characterList?[indexPath.row] {
  59. cell.config(model: model)
  60. }
  61. return cell
  62. }
  63. }
  64. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  65. if indexPath.row == self.characterList?.count {
  66. helpDelegate?.addCharacterJump()
  67. } else {
  68. if self.dialogueLabel.text?.count == 0 {
  69. self.toast(text: "请先粘贴文字")
  70. return
  71. }
  72. let cell = collectionView.cellForItem(at: indexPath) as! KeyboardCharacterCell
  73. cell.setAnimate(play: true)
  74. if let model = self.characterList?[indexPath.row], let characterId = model.id {
  75. helpDelegate?.helpCollectionViewDidSelectItem(characterId: characterId, content: self.dialogueLabel.text ?? "") { isSuccess in
  76. cell.setAnimate(play: false)
  77. if isSuccess {
  78. self.dialogueClearBtnAction()
  79. }
  80. }
  81. }
  82. }
  83. }
  84. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  85. return CGSize(width: UX.cellWidth, height: UX.cellHeight)
  86. }
  87. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  88. return 6
  89. }
  90. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  91. return 6
  92. }
  93. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  94. return CGSize(width: KeyboardConst.kb_kScreenW, height: 6)
  95. }
  96. }
  97. extension KeyboardHelpView {
  98. func initUI() {
  99. self.addSubview(self.collectionView)
  100. collectionView.snp.makeConstraints { make in
  101. make.left.equalTo(10)
  102. make.right.equalTo(deleteBtn.snp.left).offset(-6)
  103. make.top.equalTo(dialogueView.snp.bottom).offset(6)
  104. make.bottom.equalTo(0)
  105. }
  106. }
  107. }