KeyboardTeachView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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: 12), 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: 12)
  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. self.dialogueClearBtnAction()
  105. }
  106. // 重新生成按钮点击
  107. @objc func regenerateBtnClickAction() {
  108. self.isResultLoading = true
  109. self.tableView.reloadData()
  110. if let selectCharacter = self.selectCharacter, let characterId = selectCharacter.id {
  111. teachDelegate?.teachCollectionViewDidSelectItem(characterId: characterId, content: self.dialogueLabel.text ?? "") { list, isLogin, isLoaded in
  112. if isLoaded {
  113. self.isResultLoading = false
  114. self.resultList = list
  115. }
  116. }
  117. }
  118. }
  119. }
  120. extension KeyboardTeachView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  121. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  122. return (self.characterList?.count ?? 0) + 1
  123. }
  124. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  125. if indexPath.row == self.characterList?.count {
  126. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KeyboardCharacterCell.reuseIdentifier(), for: indexPath) as! KeyboardCharacterCell
  127. cell.titleLabel.isHidden = true
  128. cell.addBtn.isHidden = false
  129. return cell
  130. } else {
  131. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KeyboardCharacterCell.reuseIdentifier(), for: indexPath) as! KeyboardCharacterCell
  132. if let model = self.characterList?[indexPath.row] {
  133. cell.config(model: model)
  134. }
  135. return cell
  136. }
  137. }
  138. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  139. if indexPath.row == self.characterList?.count {
  140. teachDelegate?.addCharacterJump()
  141. } else {
  142. if self.dialogueLabel.text?.count == 0 {
  143. self.toast(text: "请先粘贴文字")
  144. return
  145. }
  146. if let model = self.characterList?[indexPath.row], let characterId = model.id {
  147. self.selectCharacter = model
  148. teachDelegate?.teachCollectionViewDidSelectItem(characterId: characterId, content: self.dialogueLabel.text ?? "") { list, isLogin, isLoaded in
  149. if isLogin {
  150. // 清空回答列表
  151. self.resultList?.removeAll()
  152. self.collectionView.isHidden = true
  153. self.resultView.isHidden = false
  154. self.tableView.reloadData()
  155. }
  156. if isLoaded {
  157. self.isResultLoading = false
  158. self.resultList = list
  159. }
  160. }
  161. }
  162. }
  163. }
  164. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  165. return CGSize(width: UX.cellWidth, height: UX.cellHeight)
  166. }
  167. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  168. return 6
  169. }
  170. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  171. return 6
  172. }
  173. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  174. return CGSize(width: KeyboardConst.kb_kScreenW, height: 6)
  175. }
  176. }
  177. extension KeyboardTeachView: UITableViewDelegate, UITableViewDataSource {
  178. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  179. return 1
  180. }
  181. func numberOfSections(in tableView: UITableView) -> Int {
  182. return isResultLoading ? 4 : (resultList?.count ?? 0) + 1
  183. }
  184. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  185. let cell = tableView.dequeueReusableCell(withIdentifier: KeyboardTeachDialogueCell.reuseIdentifier(), for: indexPath) as! KeyboardTeachDialogueCell
  186. cell.backgroundColor = .clear
  187. cell.selectionStyle = .none
  188. if indexPath.section == 0 {
  189. cell.isHidden = true
  190. } else {
  191. if let resultList = self.resultList, resultList.count > 0 && !isResultLoading {
  192. let content = resultList[indexPath.section - 1]
  193. cell.config(content: content)
  194. }
  195. cell.setLoading(play: isResultLoading)
  196. }
  197. return cell
  198. }
  199. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  200. if let resultList = self.resultList, resultList.count > 0 && !isResultLoading {
  201. let content = resultList[indexPath.section - 1]
  202. teachDelegate?.teachTableViewDidSelectItem(content: content)
  203. self.backBtnClickAction()
  204. }
  205. }
  206. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  207. if isResultLoading {
  208. return UX.resultCellHeight
  209. }
  210. if indexPath.section != 0 {
  211. if let resultList = self.resultList, resultList.count > 0 {
  212. let content = resultList[indexPath.section - 1]
  213. let height = content.heightAccording(width: KeyboardConst.kb_kScreenW - 105, font: .systemFont(ofSize: 12), lineSpacing: 5) + 16
  214. return height
  215. }
  216. }
  217. return UX.resultCellHeight
  218. }
  219. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  220. return 8
  221. }
  222. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  223. let view = UIView(frame: CGRect(x: 0, y: 0, width: KeyboardConst.kb_kScreenW - 84, height: 8))
  224. return view
  225. }
  226. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  227. return 0.001
  228. }
  229. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  230. return nil
  231. }
  232. }
  233. extension KeyboardTeachView {
  234. func setupUI() {
  235. let attr = NSMutableAttributedString()
  236. let copyIcon = NSTextAttachment(image: UIImage(named: "keyboard_copy_icon")!)
  237. copyIcon.bounds = CGRect(x: -4, y: -4, width: 18, height: 18)
  238. let copyAttr = NSAttributedString(attachment: copyIcon)
  239. attr.append(copyAttr)
  240. let titleText = NSMutableAttributedString(string: "输入/粘贴你想说的话,润色升华")
  241. titleText.addAttributes([.font: UIFont.systemFont(ofSize: 14, weight: .medium), .foregroundColor: UIColor.hexStringColor(hexString: "#996DFF")], range: NSRange(location: 0, length: titleText.length))
  242. attr.append(titleText)
  243. self.dialogueTitleLabel.attributedText = attr
  244. self.addSubview(self.collectionView)
  245. collectionView.snp.makeConstraints { make in
  246. make.left.equalTo(10)
  247. make.right.equalTo(deleteBtn.snp.left).offset(-6)
  248. make.top.equalTo(dialogueView.snp.bottom).offset(6)
  249. make.bottom.equalTo(0)
  250. }
  251. self.addSubview(resultView)
  252. resultView.snp.makeConstraints { make in
  253. make.left.equalTo(10)
  254. make.right.equalTo(deleteBtn.snp.left).offset(-6)
  255. make.top.equalTo(dialogueView.snp.bottom).offset(12)
  256. make.bottom.equalTo(0)
  257. }
  258. resultView.addSubview(tableView)
  259. tableView.snp.makeConstraints { make in
  260. make.top.left.bottom.right.equalTo(0)
  261. }
  262. resultView.addSubview(backBtn)
  263. backBtn.snp.makeConstraints { make in
  264. make.size.equalTo(CGSize(width: 32, height: 32))
  265. make.left.equalTo(0)
  266. make.top.equalTo(0)
  267. }
  268. resultView.addSubview(regenerateBtn)
  269. regenerateBtn.snp.makeConstraints { make in
  270. make.size.equalTo(CGSize(width: 74, height: 32))
  271. make.top.right.equalTo(0)
  272. }
  273. }
  274. }