KeyboardPrologueView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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], Bool, Bool) -> ()))
  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, isLogin, isLoaded in
  148. if isLoaded {
  149. self.isResultLoading = false
  150. self.resultList = list
  151. }
  152. }
  153. }
  154. }
  155. }
  156. extension KeyboardPrologueView: JXSegmentedViewDelegate {
  157. //返回列表的数量
  158. func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
  159. return titleDataSource.titles.count
  160. }
  161. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  162. if let prologueList = self.prologueList {
  163. let topics = prologueList[index].topics
  164. self.selectTopics = topics
  165. }
  166. }
  167. }
  168. extension KeyboardPrologueView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  169. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  170. return self.selectTopics?.count ?? 0
  171. }
  172. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  173. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KeyboardCharacterCell.reuseIdentifier(), for: indexPath) as! KeyboardCharacterCell
  174. if let selectTopics = self.selectTopics, selectTopics.count > 0 {
  175. let name = selectTopics[indexPath.row].name
  176. cell.config(content: name ?? "")
  177. }
  178. return cell
  179. }
  180. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  181. self.selectName = ""
  182. if let selectTopics = self.selectTopics, selectTopics.count > 0 {
  183. let name = selectTopics[indexPath.row].name
  184. self.selectName = name
  185. prologueDelegate?.prologueCollectionViewDidSelectItem(name: name ?? "") { list, isLogin, isLoaded in
  186. if isLogin {
  187. // 清空回答列表
  188. self.resultList?.removeAll()
  189. self.collectionView.isHidden = true
  190. self.resultView.isHidden = false
  191. self.tableView.reloadData()
  192. }
  193. if isLoaded {
  194. self.isResultLoading = false
  195. self.resultList = list
  196. }
  197. }
  198. }
  199. }
  200. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  201. let width = (KeyboardConst.kb_kScreenW - 96) / 3
  202. return CGSize(width: width, height: 46)
  203. }
  204. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  205. return 6
  206. }
  207. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  208. return 6
  209. }
  210. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  211. return CGSize(width: KeyboardConst.kb_kScreenW, height: 6)
  212. }
  213. }
  214. extension KeyboardPrologueView: UITableViewDelegate, UITableViewDataSource {
  215. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  216. return 1
  217. }
  218. func numberOfSections(in tableView: UITableView) -> Int {
  219. return isResultLoading ? 4 : (resultList?.count ?? 0) + 1
  220. }
  221. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  222. let cell = tableView.dequeueReusableCell(withIdentifier: KeyboardTeachDialogueCell.reuseIdentifier(), for: indexPath) as! KeyboardTeachDialogueCell
  223. cell.backgroundColor = .clear
  224. cell.selectionStyle = .none
  225. if indexPath.section == 0 {
  226. cell.isHidden = true
  227. } else {
  228. if let resultList = self.resultList, resultList.count > 0 && !isResultLoading {
  229. let content = resultList[indexPath.section - 1]
  230. cell.config(content: content)
  231. }
  232. cell.setLoading(play: isResultLoading)
  233. }
  234. return cell
  235. }
  236. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  237. if let resultList = self.resultList, resultList.count > 0 && !isResultLoading {
  238. let content = resultList[indexPath.section - 1]
  239. prologueDelegate?.prologueTableViewDidSelectItem(content: content)
  240. self.backBtnClickAction()
  241. }
  242. }
  243. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  244. if isResultLoading {
  245. return UX.resultCellHeight
  246. }
  247. if indexPath.section != 0 {
  248. if let resultList = self.resultList, resultList.count > 0 {
  249. let content = resultList[indexPath.section - 1]
  250. let height = content.heightAccording(width: KeyboardConst.kb_kScreenW - 105, font: .systemFont(ofSize: 12), lineSpacing: 5) + 16
  251. return height
  252. }
  253. }
  254. return UX.resultCellHeight
  255. }
  256. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  257. return 8
  258. }
  259. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  260. let view = UIView(frame: CGRect(x: 0, y: 0, width: KeyboardConst.kb_kScreenW - 84, height: 8))
  261. return view
  262. }
  263. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  264. return 0.001
  265. }
  266. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  267. return nil
  268. }
  269. }
  270. extension KeyboardPrologueView {
  271. func updateUI() {
  272. if let prologueList = self.prologueList {
  273. var titles = [String]()
  274. for prologue in prologueList {
  275. titles.append(prologue.title ?? "")
  276. }
  277. if prologueList.count == 2 {
  278. self.titleDataSource.itemWidth = UX.twoTitleBtnWidth
  279. } else if prologueList.count == 3 {
  280. self.titleDataSource.itemWidth = UX.threeTitleBtnWidth
  281. } else {
  282. self.titleDataSource.itemWidth = UX.fourTitleBtnWidth
  283. }
  284. self.titleDataSource.titles = titles
  285. self.pagingTitleView.dataSource = self.titleDataSource
  286. self.pagingTitleView.reloadData()
  287. self.selectTopics = prologueList.first?.topics
  288. }
  289. }
  290. func initUI() {
  291. self.dialogueView.removeFromSuperview()
  292. self.pasteBtn.removeFromSuperview()
  293. self.addSubview(pagingTitleView)
  294. pagingTitleView.snp.makeConstraints { make in
  295. make.left.equalTo(10)
  296. make.right.equalTo(-10)
  297. make.height.equalTo(34)
  298. make.top.equalTo(self.topView.snp.bottom)
  299. }
  300. self.addSubview(collectionView)
  301. collectionView.snp.makeConstraints { make in
  302. make.left.equalTo(10)
  303. make.right.equalTo(deleteBtn.snp.left).offset(-6)
  304. make.top.equalTo(pagingTitleView.snp.bottom).offset(14)
  305. make.bottom.equalTo(0)
  306. }
  307. deleteBtn.snp.remakeConstraints { make in
  308. make.size.equalTo(CGSize(width: 58, height: 46))
  309. make.right.equalTo(-10)
  310. make.top.equalTo(pagingTitleView.snp.bottom).offset(14)
  311. }
  312. self.addSubview(resultView)
  313. resultView.snp.makeConstraints { make in
  314. make.left.equalTo(10)
  315. make.right.equalTo(deleteBtn.snp.left).offset(-6)
  316. make.top.equalTo(pagingTitleView.snp.bottom).offset(12)
  317. make.bottom.equalTo(0)
  318. }
  319. resultView.addSubview(tableView)
  320. tableView.snp.makeConstraints { make in
  321. make.top.left.bottom.right.equalTo(0)
  322. }
  323. resultView.addSubview(backBtn)
  324. backBtn.snp.makeConstraints { make in
  325. make.size.equalTo(CGSize(width: 32, height: 32))
  326. make.left.equalTo(0)
  327. make.top.equalTo(0)
  328. }
  329. resultView.addSubview(regenerateBtn)
  330. regenerateBtn.snp.makeConstraints { make in
  331. make.size.equalTo(CGSize(width: 74, height: 32))
  332. make.top.right.equalTo(0)
  333. }
  334. }
  335. }