KeyboardHelpView.swift 4.4 KB

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