KeyboardTeachView.swift 12 KB

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