KeyboardTeachView.swift 13 KB

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