KeyboardPrologueView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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: 14), 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: 14)
  48. titleDataSource.titleSelectedFont = .boldSystemFont(ofSize: 14)
  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: 14)
  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.pagingTitleView.isHidden = false
  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.pagingTitleView.isHidden = true
  190. self.collectionView.isHidden = true
  191. self.resultView.isHidden = false
  192. self.tableView.reloadData()
  193. }
  194. if isLoaded {
  195. self.isResultLoading = false
  196. self.resultList = list
  197. }
  198. }
  199. }
  200. }
  201. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  202. let width = (KeyboardConst.kb_kScreenW - 96) / 3
  203. return CGSize(width: width, height: 46)
  204. }
  205. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  206. return 6
  207. }
  208. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  209. return 6
  210. }
  211. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  212. return CGSize(width: KeyboardConst.kb_kScreenW, height: 6)
  213. }
  214. }
  215. extension KeyboardPrologueView: UITableViewDelegate, UITableViewDataSource {
  216. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  217. return 1
  218. }
  219. func numberOfSections(in tableView: UITableView) -> Int {
  220. return isResultLoading ? 4 : (resultList?.count ?? 0) + 1
  221. }
  222. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  223. let cell = tableView.dequeueReusableCell(withIdentifier: KeyboardTeachDialogueCell.reuseIdentifier(), for: indexPath) as! KeyboardTeachDialogueCell
  224. cell.backgroundColor = .clear
  225. cell.selectionStyle = .none
  226. if indexPath.section == 0 {
  227. cell.isHidden = true
  228. } else {
  229. if let resultList = self.resultList, resultList.count > 0 && !isResultLoading {
  230. let content = resultList[indexPath.section - 1]
  231. cell.config(content: content)
  232. }
  233. cell.setLoading(play: isResultLoading)
  234. }
  235. return cell
  236. }
  237. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  238. if let resultList = self.resultList, resultList.count > 0 && !isResultLoading {
  239. let content = resultList[indexPath.section - 1]
  240. prologueDelegate?.prologueTableViewDidSelectItem(content: content)
  241. self.backBtnClickAction()
  242. }
  243. }
  244. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  245. if isResultLoading {
  246. return UX.resultCellHeight
  247. }
  248. if indexPath.section != 0 {
  249. if let resultList = self.resultList, resultList.count > 0 {
  250. let content = resultList[indexPath.section - 1]
  251. let height = content.heightAccording(width: KeyboardConst.kb_kScreenW - 105, font: .systemFont(ofSize: 14), lineSpacing: 5) + 16
  252. return height
  253. }
  254. }
  255. return UX.resultCellHeight
  256. }
  257. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  258. return 8
  259. }
  260. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  261. let view = UIView(frame: CGRect(x: 0, y: 0, width: KeyboardConst.kb_kScreenW - 84, height: 8))
  262. return view
  263. }
  264. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  265. return 0.001
  266. }
  267. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  268. return nil
  269. }
  270. }
  271. extension KeyboardPrologueView {
  272. func updateUI() {
  273. if let prologueList = self.prologueList {
  274. var titles = [String]()
  275. for prologue in prologueList {
  276. titles.append(prologue.title ?? "")
  277. }
  278. if prologueList.count == 2 {
  279. self.titleDataSource.itemWidth = UX.twoTitleBtnWidth
  280. } else if prologueList.count == 3 {
  281. self.titleDataSource.itemWidth = UX.threeTitleBtnWidth
  282. } else {
  283. self.titleDataSource.itemWidth = UX.fourTitleBtnWidth
  284. }
  285. self.titleDataSource.titles = titles
  286. self.pagingTitleView.dataSource = self.titleDataSource
  287. self.pagingTitleView.reloadData()
  288. self.selectTopics = prologueList.first?.topics
  289. }
  290. }
  291. func initUI() {
  292. self.dialogueView.removeFromSuperview()
  293. self.pasteBtn.removeFromSuperview()
  294. self.addSubview(pagingTitleView)
  295. pagingTitleView.snp.makeConstraints { make in
  296. make.left.equalTo(10)
  297. make.right.equalTo(-10)
  298. make.height.equalTo(34)
  299. make.top.equalTo(self.topView.snp.bottom)
  300. }
  301. self.addSubview(collectionView)
  302. collectionView.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(14)
  306. make.bottom.equalTo(0)
  307. }
  308. deleteBtn.snp.remakeConstraints { make in
  309. make.size.equalTo(CGSize(width: 58, height: 46))
  310. make.right.equalTo(-10)
  311. make.top.equalTo(pagingTitleView.snp.bottom).offset(14)
  312. }
  313. self.addSubview(resultView)
  314. resultView.snp.makeConstraints { make in
  315. make.left.equalTo(10)
  316. make.right.equalTo(deleteBtn.snp.left).offset(-6)
  317. make.top.equalTo(self.topView.snp.bottom)
  318. make.bottom.equalTo(0)
  319. }
  320. resultView.addSubview(tableView)
  321. tableView.snp.makeConstraints { make in
  322. make.top.left.bottom.right.equalTo(0)
  323. }
  324. resultView.addSubview(backBtn)
  325. backBtn.snp.makeConstraints { make in
  326. make.size.equalTo(CGSize(width: 32, height: 32))
  327. make.left.equalTo(0)
  328. make.top.equalTo(0)
  329. }
  330. resultView.addSubview(regenerateBtn)
  331. regenerateBtn.snp.makeConstraints { make in
  332. make.size.equalTo(CGSize(width: 74, height: 32))
  333. make.top.right.equalTo(0)
  334. }
  335. }
  336. }