| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- //
- // KeyboardPrologueView.swift
- // AiKeyboard
- //
- // Created by Destiny on 2025/4/28.
- //
- import UIKit
- import JXSegmentedView
- protocol KeyboardPrologueViewDelegate: NSObjectProtocol {
-
- func prologueCollectionViewDidSelectItem(name: String, complete: @escaping (([String], Bool, Bool) -> ()))
-
- func prologueTableViewDidSelectItem(content: String)
- }
- class KeyboardPrologueView: KeyboardBaseView {
-
- struct UX {
- static let twoTitleBtnWidth = (KeyboardConst.kb_kScreenW - 20) / 4
- static let threeTitleBtnWidth = (KeyboardConst.kb_kScreenW - 20) / 3
- static let fourTitleBtnWidth = (KeyboardConst.kb_kScreenW - 20) / 4
- static let resultCellHeight = "单行文字".heightAccording(width: KeyboardConst.kb_kScreenW - 105, font: .systemFont(ofSize: 14), lineSpacing: 5) + 16
- }
-
- weak var prologueDelegate: KeyboardPrologueViewDelegate?
-
- // 是否在加载
- var isResultLoading: Bool = true
-
- var selectName: String?
-
- var resultList: [String]? {
- didSet {
- self.tableView.reloadData()
- }
- }
-
- // 开场白列表
- var prologueList: [PrologueModel]? {
- didSet {
- updateUI()
- }
- }
-
- // 主题
- var selectTopics: [PrologueTopicModel]? {
- didSet {
- self.collectionView.reloadData()
- }
- }
-
- let titleDataSource: JXSegmentedTitleDataSource = {
-
- let titleDataSource = JXSegmentedTitleDataSource()
- titleDataSource.titles = []
- titleDataSource.itemWidth = UX.fourTitleBtnWidth
- titleDataSource.titleNormalColor = UIColor.hexStringColor(hexString: "#77849D")
- titleDataSource.titleSelectedColor = .hexStringColor(hexString: "#000000", alpha: 0.8)
- titleDataSource.titleNormalFont = .systemFont(ofSize: 14)
- titleDataSource.titleSelectedFont = .boldSystemFont(ofSize: 14)
- titleDataSource.itemSpacing = 0
-
- return titleDataSource
- }()
-
- lazy var pagingTitleView: JXSegmentedView = {
-
- let totalItemWidth = KeyboardConst.kb_kScreenW - 20
-
- let indicator = JXSegmentedIndicatorBackgroundView()
- indicator.indicatorWidthIncrement = -4
- // indicator.indicatorWidth = totalItemWidth / 2
- indicator.indicatorHeight = 30
- indicator.indicatorColor = .hexStringColor(hexString: "#DDCFFD")
- indicator.indicatorCornerRadius = 15
-
- let titleView = JXSegmentedView(frame: CGRect(x: 0, y: 0, width: totalItemWidth, height: 34.0))
- titleView.backgroundColor = .white
- titleView.delegate = self
- titleView.dataSource = titleDataSource
- titleView.indicators = [indicator]
- // titleView.listContainer = containerView
- titleView.layer.cornerRadius = 17
- return titleView
- }()
-
- lazy var collectionView: UICollectionView = {
-
- let layout = UICollectionViewFlowLayout()
- layout.minimumLineSpacing = 0
- layout.scrollDirection = .vertical
-
- let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
- collectionView.backgroundColor = .clear
-
- collectionView.dataSource = self
- collectionView.delegate = self
-
- collectionView.showsVerticalScrollIndicator = false
- collectionView.bounces = false
-
- collectionView.register(KeyboardCharacterCell.self, forCellWithReuseIdentifier: KeyboardCharacterCell.reuseIdentifier())
-
- return collectionView
- }()
-
- lazy var resultView: UIView = {
-
- let view = UIView()
- view.isHidden = true
- return view
- }()
-
- lazy var backBtn: UIButton = {
-
- let btn = UIButton()
- btn.setImage(UIImage(named: "keyboard_back_btn"), for: .normal)
- btn.layer.shadowOffset = CGSize(width: 0, height: 0)
- btn.layer.shadowColor = UIColor.hexStringColor(hexString: "#000000", alpha: 0.12).cgColor
- btn.layer.shadowRadius = 6.1
- btn.addTarget(self, action: #selector(backBtnClickAction), for: .touchUpInside)
- return btn
- }()
-
- lazy var regenerateBtn: UIButton = {
-
- let btn = UIButton()
- btn.backgroundColor = .white
- btn.layer.cornerRadius = 12
- btn.layer.shadowOffset = CGSize(width: 0, height: 0)
- btn.layer.shadowColor = UIColor.hexStringColor(hexString: "#000000", alpha: 0.12).cgColor
- btn.layer.shadowRadius = 6.1
- btn.setTitle("重新生成", for: .normal)
- btn.setTitleColor(.hexStringColor(hexString: "#000000", alpha: 0.8), for: .normal)
- btn.titleLabel?.font = .systemFont(ofSize: 14)
- btn.addTarget(self, action: #selector(regenerateBtnClickAction), for: .touchUpInside)
- return btn
- }()
-
- lazy var tableView: UITableView = {
-
- let tableView = UITableView(frame: .zero, style: .grouped)
- tableView.backgroundColor = .clear
- tableView.separatorStyle = .none
- tableView.showsVerticalScrollIndicator = false
- tableView.delegate = self
- tableView.dataSource = self
-
- if #available(iOS 11, *) {
- tableView.estimatedRowHeight = 0
- tableView.estimatedSectionFooterHeight = 0
- tableView.estimatedSectionHeaderHeight = 0
- tableView.contentInsetAdjustmentBehavior = .never
- }
-
- tableView.isUserInteractionEnabled = true
- tableView.register(KeyboardTeachDialogueCell.self, forCellReuseIdentifier: KeyboardTeachDialogueCell.reuseIdentifier())
-
- return tableView
- }()
-
- override init(frame: CGRect) {
- super.init(frame: frame)
- initUI()
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- }
- extension KeyboardPrologueView {
-
- // 返回按钮点击
- @objc func backBtnClickAction() {
-
- self.isResultLoading = true
- self.collectionView.isHidden = false
- self.resultView.isHidden = true
- self.pagingTitleView.isHidden = false
- }
-
- // 重新生成按钮点击
- @objc func regenerateBtnClickAction() {
-
- self.isResultLoading = true
- self.tableView.reloadData()
-
- if let selectName = self.selectName {
-
- prologueDelegate?.prologueCollectionViewDidSelectItem(name: selectName) { list, isLogin, isLoaded in
- if isLoaded {
- self.isResultLoading = false
- self.resultList = list
- }
- }
- }
- }
- }
- extension KeyboardPrologueView: JXSegmentedViewDelegate {
-
- //返回列表的数量
- func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
- return titleDataSource.titles.count
- }
-
- func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
-
- if let prologueList = self.prologueList {
- let topics = prologueList[index].topics
- self.selectTopics = topics
- }
- }
- }
- extension KeyboardPrologueView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
-
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return self.selectTopics?.count ?? 0
- }
-
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
-
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KeyboardCharacterCell.reuseIdentifier(), for: indexPath) as! KeyboardCharacterCell
- if let selectTopics = self.selectTopics, selectTopics.count > 0 {
- let name = selectTopics[indexPath.row].name
- cell.config(content: name ?? "")
- }
- return cell
- }
-
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
-
- self.selectName = ""
-
- if let selectTopics = self.selectTopics, selectTopics.count > 0 {
-
- let name = selectTopics[indexPath.row].name
- self.selectName = name
- prologueDelegate?.prologueCollectionViewDidSelectItem(name: name ?? "") { list, isLogin, isLoaded in
-
- if isLogin {
- // 清空回答列表
- self.resultList?.removeAll()
- self.pagingTitleView.isHidden = true
- self.collectionView.isHidden = true
- self.resultView.isHidden = false
- self.tableView.reloadData()
- }
-
- if isLoaded {
-
- self.isResultLoading = false
- self.resultList = list
- }
- }
- }
- }
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
-
- let width = (KeyboardConst.kb_kScreenW - 96) / 3
- return CGSize(width: width, height: 46)
- }
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
- return 6
- }
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
- return 6
- }
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
- return CGSize(width: KeyboardConst.kb_kScreenW, height: 6)
- }
- }
- extension KeyboardPrologueView: UITableViewDelegate, UITableViewDataSource {
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return 1
- }
-
- func numberOfSections(in tableView: UITableView) -> Int {
-
- return isResultLoading ? 4 : (resultList?.count ?? 0) + 1
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
-
- let cell = tableView.dequeueReusableCell(withIdentifier: KeyboardTeachDialogueCell.reuseIdentifier(), for: indexPath) as! KeyboardTeachDialogueCell
- cell.backgroundColor = .clear
- cell.selectionStyle = .none
- if indexPath.section == 0 {
- cell.isHidden = true
- } else {
- if let resultList = self.resultList, resultList.count > 0 && !isResultLoading {
- let content = resultList[indexPath.section - 1]
- cell.config(content: content)
- }
-
- cell.setLoading(play: isResultLoading)
- }
- return cell
- }
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
-
- if let resultList = self.resultList, resultList.count > 0 && !isResultLoading {
-
- let content = resultList[indexPath.section - 1]
- prologueDelegate?.prologueTableViewDidSelectItem(content: content)
- self.backBtnClickAction()
- }
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
-
- if isResultLoading {
- return UX.resultCellHeight
- }
-
- if indexPath.section != 0 {
- if let resultList = self.resultList, resultList.count > 0 {
- let content = resultList[indexPath.section - 1]
- let height = content.heightAccording(width: KeyboardConst.kb_kScreenW - 105, font: .systemFont(ofSize: 14), lineSpacing: 5) + 16
- return height
- }
- }
-
- return UX.resultCellHeight
- }
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- return 8
- }
-
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- let view = UIView(frame: CGRect(x: 0, y: 0, width: KeyboardConst.kb_kScreenW - 84, height: 8))
- return view
- }
-
- func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
- return 0.001
- }
-
- func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
- return nil
- }
-
- }
- extension KeyboardPrologueView {
-
- func updateUI() {
-
- if let prologueList = self.prologueList {
-
- var titles = [String]()
- for prologue in prologueList {
- titles.append(prologue.title ?? "")
- }
-
- if prologueList.count == 2 {
- self.titleDataSource.itemWidth = UX.twoTitleBtnWidth
- } else if prologueList.count == 3 {
- self.titleDataSource.itemWidth = UX.threeTitleBtnWidth
- } else {
- self.titleDataSource.itemWidth = UX.fourTitleBtnWidth
- }
-
- self.titleDataSource.titles = titles
- self.pagingTitleView.dataSource = self.titleDataSource
- self.pagingTitleView.reloadData()
-
- self.selectTopics = prologueList.first?.topics
- }
- }
-
- func initUI() {
-
- self.dialogueView.removeFromSuperview()
- self.pasteBtn.removeFromSuperview()
-
- self.addSubview(pagingTitleView)
- pagingTitleView.snp.makeConstraints { make in
- make.left.equalTo(10)
- make.right.equalTo(-10)
- make.height.equalTo(34)
- make.top.equalTo(self.topView.snp.bottom)
- }
-
- self.addSubview(collectionView)
- collectionView.snp.makeConstraints { make in
- make.left.equalTo(10)
- make.right.equalTo(deleteBtn.snp.left).offset(-6)
- make.top.equalTo(pagingTitleView.snp.bottom).offset(14)
- make.bottom.equalTo(0)
- }
-
- deleteBtn.snp.remakeConstraints { make in
- make.size.equalTo(CGSize(width: 58, height: 46))
- make.right.equalTo(-10)
- make.top.equalTo(pagingTitleView.snp.bottom).offset(14)
- }
-
- self.addSubview(resultView)
- resultView.snp.makeConstraints { make in
- make.left.equalTo(10)
- make.right.equalTo(deleteBtn.snp.left).offset(-6)
- make.top.equalTo(self.topView.snp.bottom)
- make.bottom.equalTo(0)
- }
-
- resultView.addSubview(tableView)
- tableView.snp.makeConstraints { make in
- make.top.left.bottom.right.equalTo(0)
- }
-
- resultView.addSubview(backBtn)
- backBtn.snp.makeConstraints { make in
- make.size.equalTo(CGSize(width: 32, height: 32))
- make.left.equalTo(0)
- make.top.equalTo(0)
- }
-
- resultView.addSubview(regenerateBtn)
- regenerateBtn.snp.makeConstraints { make in
- make.size.equalTo(CGSize(width: 74, height: 32))
- make.top.right.equalTo(0)
- }
- }
- }
|