KeyboardPrologueView.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //
  2. // KeyboardPrologueView.swift
  3. // AiKeyboard
  4. //
  5. // Created by Destiny on 2025/4/28.
  6. //
  7. import UIKit
  8. import JXSegmentedView
  9. protocol KeyboardPrologueViewDelegate: NSObjectProtocol {
  10. func prologueCollectionViewDidSelectItem(name: String, complete: @escaping (([String]) -> ()))
  11. func prologueTableViewDidSelectItem(content: String)
  12. }
  13. class KeyboardPrologueView: KeyboardBaseView {
  14. struct UX {
  15. static let twoTitleBtnWidth = (KeyboardConst.kb_kScreenW - 20) / 4
  16. static let threeTitleBtnWidth = (KeyboardConst.kb_kScreenW - 20) / 3
  17. static let fourTitleBtnWidth = (KeyboardConst.kb_kScreenW - 20) / 4
  18. static let resultCellHeight = "单行文字".heightAccording(width: KeyboardConst.kb_kScreenW - 105, font: .systemFont(ofSize: 12), lineSpacing: 5) + 16
  19. }
  20. weak var prologueDelegate: KeyboardPrologueViewDelegate?
  21. // 是否在加载
  22. var isResultLoading: Bool = true
  23. var selectName: String?
  24. var resultList: [String]? {
  25. didSet {
  26. self.tableView.reloadData()
  27. }
  28. }
  29. // 开场白列表
  30. var prologueList: [PrologueModel]? {
  31. didSet {
  32. updateUI()
  33. }
  34. }
  35. // 主题
  36. var selectTopics: [PrologueTopicModel]? {
  37. didSet {
  38. self.collectionView.reloadData()
  39. }
  40. }
  41. let titleDataSource: JXSegmentedTitleDataSource = {
  42. let titleDataSource = JXSegmentedTitleDataSource()
  43. titleDataSource.titles = []
  44. titleDataSource.itemWidth = UX.fourTitleBtnWidth
  45. titleDataSource.titleNormalColor = UIColor.hexStringColor(hexString: "#77849D")
  46. titleDataSource.titleSelectedColor = .hexStringColor(hexString: "#000000", alpha: 0.8)
  47. titleDataSource.titleNormalFont = .systemFont(ofSize: 12)
  48. titleDataSource.titleSelectedFont = .boldSystemFont(ofSize: 12)
  49. titleDataSource.itemSpacing = 0
  50. return titleDataSource
  51. }()
  52. lazy var pagingTitleView: JXSegmentedView = {
  53. let totalItemWidth = KeyboardConst.kb_kScreenW - 20
  54. let indicator = JXSegmentedIndicatorBackgroundView()
  55. indicator.indicatorWidthIncrement = -4
  56. // indicator.indicatorWidth = totalItemWidth / 2
  57. indicator.indicatorHeight = 30
  58. indicator.indicatorColor = .hexStringColor(hexString: "#DDCFFD")
  59. indicator.indicatorCornerRadius = 15
  60. let titleView = JXSegmentedView(frame: CGRect(x: 0, y: 0, width: totalItemWidth, height: 34.0))
  61. titleView.backgroundColor = .white
  62. titleView.delegate = self
  63. titleView.dataSource = titleDataSource
  64. titleView.indicators = [indicator]
  65. // titleView.listContainer = containerView
  66. titleView.layer.cornerRadius = 17
  67. return titleView
  68. }()
  69. lazy var collectionView: UICollectionView = {
  70. let layout = UICollectionViewFlowLayout()
  71. layout.minimumLineSpacing = 0
  72. layout.scrollDirection = .vertical
  73. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  74. collectionView.backgroundColor = .clear
  75. collectionView.dataSource = self
  76. collectionView.delegate = self
  77. collectionView.showsVerticalScrollIndicator = false
  78. collectionView.bounces = false
  79. collectionView.register(KeyboardCharacterCell.self, forCellWithReuseIdentifier: KeyboardCharacterCell.reuseIdentifier())
  80. return collectionView
  81. }()
  82. lazy var resultView: UIView = {
  83. let view = UIView()
  84. view.isHidden = true
  85. return view
  86. }()
  87. lazy var backBtn: UIButton = {
  88. let btn = UIButton()
  89. btn.setImage(UIImage(named: "keyboard_back_btn"), for: .normal)
  90. btn.layer.shadowOffset = CGSize(width: 0, height: 0)
  91. btn.layer.shadowColor = UIColor.hexStringColor(hexString: "#000000", alpha: 0.12).cgColor
  92. btn.layer.shadowRadius = 6.1
  93. btn.addTarget(self, action: #selector(backBtnClickAction), for: .touchUpInside)
  94. return btn
  95. }()
  96. lazy var regenerateBtn: UIButton = {
  97. let btn = UIButton()
  98. btn.backgroundColor = .white
  99. btn.layer.cornerRadius = 12
  100. btn.layer.shadowOffset = CGSize(width: 0, height: 0)
  101. btn.layer.shadowColor = UIColor.hexStringColor(hexString: "#000000", alpha: 0.12).cgColor
  102. btn.layer.shadowRadius = 6.1
  103. btn.setTitle("重新生成", for: .normal)
  104. btn.setTitleColor(.hexStringColor(hexString: "#000000", alpha: 0.8), for: .normal)
  105. btn.titleLabel?.font = .systemFont(ofSize: 12)
  106. btn.addTarget(self, action: #selector(regenerateBtnClickAction), for: .touchUpInside)
  107. return btn
  108. }()
  109. lazy var tableView: UITableView = {
  110. let tableView = UITableView(frame: .zero, style: .grouped)
  111. tableView.backgroundColor = .clear
  112. tableView.separatorStyle = .none
  113. tableView.showsVerticalScrollIndicator = false
  114. tableView.delegate = self
  115. tableView.dataSource = self
  116. if #available(iOS 11, *) {
  117. tableView.estimatedRowHeight = 0
  118. tableView.estimatedSectionFooterHeight = 0
  119. tableView.estimatedSectionHeaderHeight = 0
  120. tableView.contentInsetAdjustmentBehavior = .never
  121. }
  122. tableView.isUserInteractionEnabled = true
  123. tableView.register(KeyboardTeachDialogueCell.self, forCellReuseIdentifier: KeyboardTeachDialogueCell.reuseIdentifier())
  124. return tableView
  125. }()
  126. override init(frame: CGRect) {
  127. super.init(frame: frame)
  128. initUI()
  129. }
  130. required init?(coder: NSCoder) {
  131. fatalError("init(coder:) has not been implemented")
  132. }
  133. }
  134. extension KeyboardPrologueView {
  135. // 返回按钮点击
  136. @objc func backBtnClickAction() {
  137. self.isResultLoading = true
  138. self.collectionView.isHidden = false
  139. self.resultView.isHidden = true
  140. self.dialogueClearBtnAction()
  141. }
  142. // 重新生成按钮点击
  143. @objc func regenerateBtnClickAction() {
  144. self.isResultLoading = true
  145. self.tableView.reloadData()
  146. if let selectName = self.selectName {
  147. prologueDelegate?.prologueCollectionViewDidSelectItem(name: selectName) { list in
  148. self.isResultLoading = false
  149. self.resultList = list
  150. }
  151. }
  152. }
  153. }
  154. extension KeyboardPrologueView: JXSegmentedViewDelegate {
  155. //返回列表的数量
  156. func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
  157. return titleDataSource.titles.count
  158. }
  159. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  160. if let prologueList = self.prologueList {
  161. let topics = prologueList[index].topics
  162. self.selectTopics = topics
  163. }
  164. }
  165. }
  166. extension KeyboardPrologueView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  167. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  168. return self.selectTopics?.count ?? 0
  169. }
  170. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  171. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KeyboardCharacterCell.reuseIdentifier(), for: indexPath) as! KeyboardCharacterCell
  172. if let selectTopics = self.selectTopics, selectTopics.count > 0 {
  173. let name = selectTopics[indexPath.row].name
  174. cell.config(content: name ?? "")
  175. }
  176. return cell
  177. }
  178. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  179. if let selectTopics = self.selectTopics, selectTopics.count > 0 {
  180. // 清空回答列表
  181. self.resultList?.removeAll()
  182. self.collectionView.isHidden = true
  183. self.resultView.isHidden = false
  184. self.tableView.reloadData()
  185. let name = selectTopics[indexPath.row].name
  186. prologueDelegate?.prologueCollectionViewDidSelectItem(name: name ?? "") { list in
  187. self.isResultLoading = false
  188. self.resultList = list
  189. }
  190. }
  191. }
  192. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  193. let width = (KeyboardConst.kb_kScreenW - 96) / 3
  194. return CGSize(width: width, height: 46)
  195. }
  196. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  197. return 6
  198. }
  199. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  200. return 6
  201. }
  202. }
  203. extension KeyboardPrologueView: UITableViewDelegate, UITableViewDataSource {
  204. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  205. return 1
  206. }
  207. func numberOfSections(in tableView: UITableView) -> Int {
  208. return isResultLoading ? 4 : (resultList?.count ?? 0) + 1
  209. }
  210. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  211. let cell = tableView.dequeueReusableCell(withIdentifier: KeyboardTeachDialogueCell.reuseIdentifier(), for: indexPath) as! KeyboardTeachDialogueCell
  212. cell.backgroundColor = .clear
  213. cell.selectionStyle = .none
  214. if indexPath.section == 0 {
  215. cell.isHidden = true
  216. } else {
  217. if let resultList = self.resultList, resultList.count > 0 && !isResultLoading {
  218. let content = resultList[indexPath.section - 1]
  219. cell.config(content: content)
  220. }
  221. cell.setLoading(play: isResultLoading)
  222. }
  223. return cell
  224. }
  225. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  226. if let resultList = self.resultList, resultList.count > 0 && !isResultLoading {
  227. let content = resultList[indexPath.section - 1]
  228. prologueDelegate?.prologueTableViewDidSelectItem(content: content)
  229. self.backBtnClickAction()
  230. }
  231. }
  232. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  233. if isResultLoading {
  234. return UX.resultCellHeight
  235. }
  236. if indexPath.section != 0 {
  237. if let resultList = self.resultList, resultList.count > 0 {
  238. let content = resultList[indexPath.section - 1]
  239. let height = content.heightAccording(width: KeyboardConst.kb_kScreenW - 105, font: .systemFont(ofSize: 12), lineSpacing: 5) + 16
  240. return height
  241. }
  242. }
  243. return UX.resultCellHeight
  244. }
  245. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  246. return 8
  247. }
  248. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  249. let view = UIView(frame: CGRect(x: 0, y: 0, width: KeyboardConst.kb_kScreenW - 84, height: 8))
  250. return view
  251. }
  252. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  253. return 0.001
  254. }
  255. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  256. return nil
  257. }
  258. }
  259. extension KeyboardPrologueView {
  260. func updateUI() {
  261. if let prologueList = self.prologueList {
  262. var titles = [String]()
  263. for prologue in prologueList {
  264. titles.append(prologue.title ?? "")
  265. }
  266. if prologueList.count == 2 {
  267. self.titleDataSource.itemWidth = UX.twoTitleBtnWidth
  268. } else if prologueList.count == 3 {
  269. self.titleDataSource.itemWidth = UX.threeTitleBtnWidth
  270. } else {
  271. self.titleDataSource.itemWidth = UX.fourTitleBtnWidth
  272. }
  273. self.titleDataSource.titles = titles
  274. self.pagingTitleView.dataSource = self.titleDataSource
  275. self.pagingTitleView.reloadData()
  276. self.selectTopics = prologueList.first?.topics
  277. }
  278. }
  279. func initUI() {
  280. self.dialogueView.removeFromSuperview()
  281. self.pasteBtn.removeFromSuperview()
  282. self.addSubview(pagingTitleView)
  283. pagingTitleView.snp.makeConstraints { make in
  284. make.left.equalTo(10)
  285. make.right.equalTo(-10)
  286. make.height.equalTo(34)
  287. make.top.equalTo(15)
  288. }
  289. self.addSubview(collectionView)
  290. collectionView.snp.makeConstraints { make in
  291. make.left.equalTo(10)
  292. make.right.equalTo(deleteBtn.snp.left).offset(-6)
  293. make.top.equalTo(pagingTitleView.snp.bottom).offset(14)
  294. make.bottom.equalTo(0)
  295. }
  296. deleteBtn.snp.remakeConstraints { make in
  297. make.size.equalTo(CGSize(width: 58, height: 46))
  298. make.right.equalTo(-10)
  299. make.top.equalTo(pagingTitleView.snp.bottom).offset(14)
  300. }
  301. self.addSubview(resultView)
  302. resultView.snp.makeConstraints { make in
  303. make.left.equalTo(10)
  304. make.right.equalTo(deleteBtn.snp.left).offset(-6)
  305. make.top.equalTo(pagingTitleView.snp.bottom).offset(12)
  306. make.bottom.equalTo(0)
  307. }
  308. resultView.addSubview(tableView)
  309. tableView.snp.makeConstraints { make in
  310. make.top.left.bottom.right.equalTo(0)
  311. }
  312. resultView.addSubview(backBtn)
  313. backBtn.snp.makeConstraints { make in
  314. make.size.equalTo(CGSize(width: 32, height: 32))
  315. make.left.equalTo(0)
  316. make.top.equalTo(0)
  317. }
  318. resultView.addSubview(regenerateBtn)
  319. regenerateBtn.snp.makeConstraints { make in
  320. make.size.equalTo(CGSize(width: 74, height: 32))
  321. make.top.right.equalTo(0)
  322. }
  323. }
  324. }