KeyboardTeachView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //
  2. // KeyboardTeachView.swift
  3. // AiKeyboard
  4. //
  5. // Created by Destiny on 2025/4/24.
  6. //
  7. import UIKit
  8. protocol KeyboardTeachViewDelegate: NSObjectProtocol {
  9. func teachCollectionViewDidSelectItem(characterId: String, content: String, complete: @escaping (([String]) -> ()))
  10. func teachTableViewDidSelectItem(content: String)
  11. }
  12. class KeyboardTeachView: KeyboardBaseView {
  13. struct UX {
  14. static let cellWidth = (KeyboardConst.kb_kScreenW - 96.0) / 3.0
  15. static let cellHeight = 46.0
  16. static let resultCellHeight = "单行文字".heightAccording(width: KeyboardConst.kb_kScreenW - 105, font: .systemFont(ofSize: 12), lineSpacing: 5) + 16
  17. }
  18. weak var teachDelegate: KeyboardTeachViewDelegate?
  19. // 是否在加载
  20. var isResultLoading: Bool = true
  21. var selectCharacter: CharacterModel?
  22. var characterList: [CharacterModel]? {
  23. didSet {
  24. self.collectionView.reloadData()
  25. }
  26. }
  27. var resultList: [String]? {
  28. didSet {
  29. self.tableView.reloadData()
  30. }
  31. }
  32. lazy var collectionView: UICollectionView = {
  33. let layout = UICollectionViewFlowLayout()
  34. layout.minimumLineSpacing = 0
  35. layout.scrollDirection = .vertical
  36. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  37. collectionView.backgroundColor = .clear
  38. collectionView.dataSource = self
  39. collectionView.delegate = self
  40. collectionView.showsVerticalScrollIndicator = false
  41. collectionView.bounces = false
  42. collectionView.register(KeyboardCharacterCell.self, forCellWithReuseIdentifier: KeyboardCharacterCell.reuseIdentifier())
  43. return collectionView
  44. }()
  45. lazy var resultView: UIView = {
  46. let view = UIView()
  47. view.isHidden = true
  48. return view
  49. }()
  50. lazy var backBtn: UIButton = {
  51. let btn = UIButton()
  52. btn.setImage(UIImage(named: "keyboard_back_btn"), for: .normal)
  53. btn.layer.shadowOffset = CGSize(width: 0, height: 0)
  54. btn.layer.shadowColor = UIColor.hexStringColor(hexString: "#000000", alpha: 0.12).cgColor
  55. btn.layer.shadowRadius = 6.1
  56. btn.addTarget(self, action: #selector(backBtnClickAction), for: .touchUpInside)
  57. return btn
  58. }()
  59. lazy var regenerateBtn: UIButton = {
  60. let btn = UIButton()
  61. btn.backgroundColor = .white
  62. btn.layer.cornerRadius = 12
  63. btn.layer.shadowOffset = CGSize(width: 0, height: 0)
  64. btn.layer.shadowColor = UIColor.hexStringColor(hexString: "#000000", alpha: 0.12).cgColor
  65. btn.layer.shadowRadius = 6.1
  66. btn.setTitle("重新生成", for: .normal)
  67. btn.setTitleColor(.hexStringColor(hexString: "#000000", alpha: 0.8), for: .normal)
  68. btn.titleLabel?.font = .systemFont(ofSize: 12)
  69. btn.addTarget(self, action: #selector(regenerateBtnClickAction), for: .touchUpInside)
  70. return btn
  71. }()
  72. lazy var tableView: UITableView = {
  73. let tableView = UITableView(frame: .zero, style: .grouped)
  74. tableView.backgroundColor = .clear
  75. tableView.separatorStyle = .none
  76. tableView.showsVerticalScrollIndicator = false
  77. tableView.delegate = self
  78. tableView.dataSource = self
  79. if #available(iOS 11, *) {
  80. tableView.estimatedRowHeight = 0
  81. tableView.estimatedSectionFooterHeight = 0
  82. tableView.estimatedSectionHeaderHeight = 0
  83. tableView.contentInsetAdjustmentBehavior = .never
  84. }
  85. tableView.isUserInteractionEnabled = true
  86. tableView.register(KeyboardTeachDialogueCell.self, forCellReuseIdentifier: KeyboardTeachDialogueCell.reuseIdentifier())
  87. return tableView
  88. }()
  89. override init(frame: CGRect) {
  90. super.init(frame: frame)
  91. setupUI()
  92. }
  93. required init?(coder: NSCoder) {
  94. fatalError("init(coder:) has not been implemented")
  95. }
  96. }
  97. extension KeyboardTeachView {
  98. // 返回按钮点击
  99. @objc func backBtnClickAction() {
  100. self.isResultLoading = true
  101. self.collectionView.isHidden = false
  102. self.resultView.isHidden = true
  103. self.dialogueClearBtnAction()
  104. }
  105. // 重新生成按钮点击
  106. @objc func regenerateBtnClickAction() {
  107. self.isResultLoading = true
  108. self.tableView.reloadData()
  109. if let selectCharacter = self.selectCharacter, let characterId = selectCharacter.id {
  110. teachDelegate?.teachCollectionViewDidSelectItem(characterId: characterId, content: self.dialogueLabel.text ?? "") { list in
  111. self.isResultLoading = false
  112. self.resultList = list
  113. }
  114. }
  115. }
  116. }
  117. extension KeyboardTeachView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  118. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  119. return (self.characterList?.count ?? 0) + 1
  120. }
  121. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  122. if indexPath.row == self.characterList?.count {
  123. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KeyboardCharacterCell.reuseIdentifier(), for: indexPath) as! KeyboardCharacterCell
  124. cell.titleLabel.isHidden = true
  125. cell.addBtn.isHidden = false
  126. return cell
  127. } else {
  128. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KeyboardCharacterCell.reuseIdentifier(), for: indexPath) as! KeyboardCharacterCell
  129. if let model = self.characterList?[indexPath.row] {
  130. cell.config(model: model)
  131. }
  132. return cell
  133. }
  134. }
  135. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  136. if self.dialogueLabel.text?.count == 0 {
  137. self.toast(text: "请先粘贴文字")
  138. return
  139. }
  140. if let model = self.characterList?[indexPath.row], let characterId = model.id {
  141. // 清空回答列表
  142. self.resultList?.removeAll()
  143. self.collectionView.isHidden = true
  144. self.resultView.isHidden = false
  145. self.tableView.reloadData()
  146. self.selectCharacter = model
  147. teachDelegate?.teachCollectionViewDidSelectItem(characterId: characterId, content: self.dialogueLabel.text ?? "") { list in
  148. self.isResultLoading = false
  149. self.resultList = list
  150. }
  151. }
  152. }
  153. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  154. return CGSize(width: UX.cellWidth, height: UX.cellHeight)
  155. }
  156. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  157. return 6
  158. }
  159. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  160. return 6
  161. }
  162. }
  163. extension KeyboardTeachView: UITableViewDelegate, UITableViewDataSource {
  164. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  165. return 1
  166. }
  167. func numberOfSections(in tableView: UITableView) -> Int {
  168. return isResultLoading ? 4 : (resultList?.count ?? 0) + 1
  169. }
  170. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  171. let cell = tableView.dequeueReusableCell(withIdentifier: KeyboardTeachDialogueCell.reuseIdentifier(), for: indexPath) as! KeyboardTeachDialogueCell
  172. cell.backgroundColor = .clear
  173. cell.selectionStyle = .none
  174. if indexPath.section == 0 {
  175. cell.isHidden = true
  176. } else {
  177. if let resultList = self.resultList, resultList.count > 0 && !isResultLoading {
  178. let content = resultList[indexPath.section - 1]
  179. cell.config(content: content)
  180. }
  181. cell.setLoading(play: isResultLoading)
  182. }
  183. return cell
  184. }
  185. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  186. if let resultList = self.resultList, resultList.count > 0 && !isResultLoading {
  187. let content = resultList[indexPath.section - 1]
  188. teachDelegate?.teachTableViewDidSelectItem(content: content)
  189. self.backBtnClickAction()
  190. }
  191. }
  192. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  193. if isResultLoading {
  194. return UX.resultCellHeight
  195. }
  196. if indexPath.section != 0 {
  197. if let resultList = self.resultList, resultList.count > 0 {
  198. let content = resultList[indexPath.section - 1]
  199. let height = content.heightAccording(width: KeyboardConst.kb_kScreenW - 105, font: .systemFont(ofSize: 12), lineSpacing: 5) + 16
  200. return height
  201. }
  202. }
  203. return UX.resultCellHeight
  204. }
  205. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  206. return 8
  207. }
  208. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  209. let view = UIView(frame: CGRect(x: 0, y: 0, width: KeyboardConst.kb_kScreenW - 84, height: 8))
  210. return view
  211. }
  212. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  213. return 0.001
  214. }
  215. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  216. return nil
  217. }
  218. }
  219. extension KeyboardTeachView {
  220. func setupUI() {
  221. let attr = NSMutableAttributedString()
  222. let copyIcon = NSTextAttachment(image: UIImage(named: "keyboard_copy_icon")!)
  223. copyIcon.bounds = CGRect(x: -4, y: -4, width: 18, height: 18)
  224. let copyAttr = NSAttributedString(attachment: copyIcon)
  225. attr.append(copyAttr)
  226. let titleText = NSMutableAttributedString(string: "输入/粘贴你想说的话,润色升华")
  227. titleText.addAttributes([.font: UIFont.systemFont(ofSize: 14, weight: .medium), .foregroundColor: UIColor.hexStringColor(hexString: "#996DFF")], range: NSRange(location: 0, length: titleText.length))
  228. attr.append(titleText)
  229. self.dialogueTitleLabel.attributedText = attr
  230. self.addSubview(self.collectionView)
  231. collectionView.snp.makeConstraints { make in
  232. make.left.equalTo(10)
  233. make.right.equalTo(deleteBtn.snp.left).offset(-6)
  234. make.top.equalTo(dialogueView.snp.bottom).offset(6)
  235. make.bottom.equalTo(self.sendBtn.snp.bottom).offset(3)
  236. }
  237. self.addSubview(resultView)
  238. resultView.snp.makeConstraints { make in
  239. make.left.equalTo(10)
  240. make.right.equalTo(deleteBtn.snp.left).offset(-6)
  241. make.top.equalTo(dialogueView.snp.bottom).offset(12)
  242. make.bottom.equalTo(0)
  243. }
  244. resultView.addSubview(tableView)
  245. tableView.snp.makeConstraints { make in
  246. make.top.left.bottom.right.equalTo(0)
  247. }
  248. resultView.addSubview(backBtn)
  249. backBtn.snp.makeConstraints { make in
  250. make.size.equalTo(CGSize(width: 32, height: 32))
  251. make.left.equalTo(0)
  252. make.top.equalTo(0)
  253. }
  254. resultView.addSubview(regenerateBtn)
  255. regenerateBtn.snp.makeConstraints { make in
  256. make.size.equalTo(CGSize(width: 74, height: 32))
  257. make.top.right.equalTo(0)
  258. }
  259. }
  260. }