|
@@ -24,15 +24,17 @@
|
|
|
import InputBarAccessoryView
|
|
|
import MessageKit
|
|
|
import UIKit
|
|
|
-import MapKit
|
|
|
-// MARK: - TSChatViewController
|
|
|
|
|
|
-/// A base class for the example controllers
|
|
|
class TSChatViewController: MessagesViewController, MessagesDataSource {
|
|
|
- // MARK: Internal
|
|
|
+
|
|
|
var viewModel:TSAIChatVM = TSAIChatVM()
|
|
|
|
|
|
|
|
|
+ public lazy var navBarContentView: UIView = creatNavBarContentView
|
|
|
+
|
|
|
+ public lazy var normalNavBarView: TSNormalNavigationBarView = creatNormalNavBarView
|
|
|
+
|
|
|
+
|
|
|
// MARK: - Public properties
|
|
|
lazy var messageList: [TSChatMessage] = []
|
|
|
|
|
@@ -55,84 +57,18 @@ class TSChatViewController: MessagesViewController, MessagesDataSource {
|
|
|
}()
|
|
|
|
|
|
|
|
|
- let inputBarBgView:UIView = {
|
|
|
- let inputBarBgView = UIView()
|
|
|
- inputBarBgView.addShadow(shadowColor: "#111111".uiColor.cgColor, shadowOffset: CGSize(width: 0, height: -10), shadowRadius: 10, shadowOpacity: 1.0)
|
|
|
- return inputBarBgView
|
|
|
- }()
|
|
|
- let inputBarTopView:UIView = UIView()
|
|
|
-
|
|
|
- //免费次数
|
|
|
- lazy var freeText: UILabel = {
|
|
|
- let textLabel = UILabel.createLabel(
|
|
|
- text: "Remaining \(kPurchaseDefault.freeNum(type: .aichat)) free times",
|
|
|
- font: .font(size: 12),
|
|
|
- textColor: "#E83E3E".uiColor,
|
|
|
- textAlignment: .center,
|
|
|
- numberOfLines: 0
|
|
|
- )
|
|
|
- textLabel.isHidden = false
|
|
|
- return textLabel
|
|
|
- }()
|
|
|
-
|
|
|
-
|
|
|
- lazy var upgradeVipBg: UIView = {
|
|
|
- let upgradeVipBg = UIView()
|
|
|
-
|
|
|
- let imageView = UIImageView.createImageView(imageName: "vip_upgrade_bg",contentMode: .scaleToFill)
|
|
|
- upgradeVipBg.addSubview(imageView)
|
|
|
- imageView.snp.makeConstraints { make in
|
|
|
- make.edges.equalToSuperview()
|
|
|
- }
|
|
|
-
|
|
|
- let label = UILabel.createLabel(
|
|
|
- text:"Free usage limit reached. Upgrade for unlimited chats.".localized,
|
|
|
- font: .font(size: 14,weight: .bold),
|
|
|
- textColor: "#111111".uiColor,
|
|
|
- numberOfLines: 0
|
|
|
- )
|
|
|
- upgradeVipBg.addSubview(label)
|
|
|
- label.snp.makeConstraints { make in
|
|
|
- make.leading.equalTo(16)
|
|
|
- make.top.equalTo(8)
|
|
|
- make.bottom.equalTo(-8)
|
|
|
- make.trailing.equalTo(-95)
|
|
|
- }
|
|
|
-
|
|
|
- let upgradeBtn = UIButton.createButton(
|
|
|
- title: "Upgrade".localized,
|
|
|
- backgroundColor: "#111111".uiColor,
|
|
|
- font:.font(size: 12,weight: .medium),
|
|
|
- titleColor:.white,
|
|
|
- corner: 14) { [weak self] in
|
|
|
- guard let self = self else { return }
|
|
|
- TSPurchaseVC.show(target: self) { [weak self] in
|
|
|
- guard let self = self else { return }
|
|
|
- updateVipView()
|
|
|
- }
|
|
|
- }
|
|
|
- upgradeVipBg.addSubview(upgradeBtn)
|
|
|
- upgradeBtn.snp.makeConstraints { make in
|
|
|
- make.trailing.equalTo(-12)
|
|
|
- make.centerY.equalToSuperview()
|
|
|
- make.width.equalTo(70)
|
|
|
- make.height.equalTo(28)
|
|
|
- }
|
|
|
- return upgradeVipBg
|
|
|
- }()
|
|
|
-
|
|
|
-
|
|
|
+ lazy var vipBtn: UIButton = creatVipBtn
|
|
|
+ lazy var navBarView: TSBaseNavContentBarView = creatNavBarView
|
|
|
|
|
|
- lazy var scrollToBottomButton: UIButton = {
|
|
|
- let backBottomBtn = UIButton.createButton(image: UIImage(named: "down_arrow_line")) { [weak self] in
|
|
|
- guard let self = self else { return }
|
|
|
- messagesCollectionView.scrollToLastItem(animated: false)
|
|
|
- }
|
|
|
- backBottomBtn.isHidden = true
|
|
|
- backBottomBtn.backgroundColor = .popupColor
|
|
|
- backBottomBtn.cornerRadius = 16.0
|
|
|
- return backBottomBtn
|
|
|
- }()
|
|
|
+ var deleteBlock:(()->Void)?
|
|
|
+
|
|
|
+ lazy var inputBarBgView:UIView = creatInputBarBgView
|
|
|
+ let inputBarTopView:UIView = UIView()
|
|
|
+
|
|
|
+ //免费次数label
|
|
|
+ lazy var freeText: UILabel = creatFreeText
|
|
|
+ lazy var upgradeVipBg: UIView = creatUpgradeVipBg
|
|
|
+ lazy var scrollToBottomButton: UIButton = creatScrollToBottomButton
|
|
|
|
|
|
|
|
|
|
|
@@ -140,19 +76,76 @@ class TSChatViewController: MessagesViewController, MessagesDataSource {
|
|
|
override func viewDidLoad() {
|
|
|
super.viewDidLoad()
|
|
|
navigationItem.title = "MessageKit"
|
|
|
-
|
|
|
+ edgesForExtendedLayout = [.top]
|
|
|
+
|
|
|
+ // 创建轻击手势识别器
|
|
|
+ let tapGesture = UITapGestureRecognizer(target: self, action: #selector(clickView))
|
|
|
+ // 设置轻击手势识别器取消默认的视图触摸响应,避免影响子视图交互
|
|
|
+ tapGesture.cancelsTouchesInView = false
|
|
|
+ // 将轻击手势识别器添加到视图上
|
|
|
+ messagesCollectionView.addGestureRecognizer(tapGesture)
|
|
|
+ configureNaviBarView()
|
|
|
configureMessageCollectionView()
|
|
|
configureMessageInputBar()
|
|
|
configureOtherUI()
|
|
|
loadFirstMessages()
|
|
|
|
|
|
-
|
|
|
+ vipBtn.isHidden = PurchaseManager.default.isVip
|
|
|
+ NotificationCenter.default.addObserver(self, selector: #selector(vipInfoChanged), name: .kPurchaseDidChanged, object: nil)
|
|
|
if viewModel.uiStyle == .chat {
|
|
|
// 注册通知监听,App死的时候,保存本次聊天记录到本地
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(saveChatList), name: .kApplicationWillTerminate, object: nil)
|
|
|
+ // 监听键盘事件
|
|
|
+ NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
|
|
|
+ NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc func vipInfoChanged() {
|
|
|
+ kExecuteOnMainThread {
|
|
|
+ self.vipBtn.isHidden = PurchaseManager.default.isVip
|
|
|
+ self.updateVipView()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc func clickView(){
|
|
|
+ view.endEditing(true)
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc func keyboardWillShow(_ notification: Notification) {
|
|
|
+
|
|
|
+ guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
|
|
|
+ let animationDuration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ let keyboardHeight = keyboardFrame.height
|
|
|
+ let contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight, right: 0)
|
|
|
+
|
|
|
+ UIView.animate(withDuration: animationDuration) {
|
|
|
+ self.messagesCollectionView.contentInset = contentInset
|
|
|
+ self.messagesCollectionView.scrollIndicatorInsets = contentInset
|
|
|
+ }
|
|
|
+
|
|
|
+ kDelayMainShort {
|
|
|
+ self.messagesCollectionView.scrollToLastItem(animated: false)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc func keyboardWillHide(_ notification: Notification) {
|
|
|
+ guard let animationDuration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ let contentInset = UIEdgeInsets.zero
|
|
|
+
|
|
|
+ UIView.animate(withDuration: animationDuration) {
|
|
|
+ self.messagesCollectionView.contentInset = contentInset
|
|
|
+ self.messagesCollectionView.scrollIndicatorInsets = contentInset
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@objc func saveChatList() {
|
|
|
messageList.remove(at: 0)
|
|
|
viewModel.updateMessages(msgModels: messageList)
|
|
@@ -179,8 +172,80 @@ class TSChatViewController: MessagesViewController, MessagesDataSource {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public func addNormalNavBarView(){
|
|
|
+ navBarContentView.addSubview(normalNavBarView)
|
|
|
+ normalNavBarView.snp.makeConstraints { make in
|
|
|
+ make.edges.equalToSuperview()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public func setTitleText(_ title: String) {
|
|
|
+ _ = normalNavBarView.setTitleName(NSLocalizedString(title, comment: ""))
|
|
|
+ }
|
|
|
+
|
|
|
+ public func setPageTitle(_ title: String) {
|
|
|
+ let pageTitle = title
|
|
|
+ let backTitle = " "
|
|
|
+ setTitleText(pageTitle)
|
|
|
+ _ = setNavigationItem(backTitle, imageName: "navi_back_white", direction: .left, action: #selector(navBarClickLeftAction))
|
|
|
+ }
|
|
|
+ @objc public func navBarClickLeftAction() {
|
|
|
+ debugPrint("navBarClickLeftAction -> \(type(of: self))")
|
|
|
+ pop()
|
|
|
+ }
|
|
|
+
|
|
|
+ public func pop() {
|
|
|
+ if navigationController == nil {
|
|
|
+ dismiss(animated: true, completion: nil)
|
|
|
+ } else if navigationController?.presentingViewController != nil, navigationController?.viewControllers.count == 1 {
|
|
|
+ navigationController?.dismiss(animated: true, completion: nil)
|
|
|
+ } else {
|
|
|
+ navigationController?.popViewController(animated: true)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public func setNavigationItem(_ name: String, imageName: String, direction: NSTextAlignment, action: Selector) -> UIButton {
|
|
|
+ if direction == .left {
|
|
|
+ return normalNavBarView.setLeftNavigationItem(name: name, imageName: imageName, target: self, action: action)
|
|
|
+ } else {
|
|
|
+ return normalNavBarView.setRightNavigationItem(name: name, imageName: imageName, target: self, action: action)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ @objc func clickDelete(){
|
|
|
+ showCustomAlert(message: "Are you sure to delete".localized, deleteHandler: { [weak self] in
|
|
|
+ guard let self = self else { return }
|
|
|
+ viewModel.dbAIChatList.delete()
|
|
|
+
|
|
|
+ deleteBlock?()
|
|
|
+ pop()
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ func configureNaviBarView() {
|
|
|
+ view.addSubview(navBarContentView)
|
|
|
+ navBarContentView.snp.makeConstraints { make in
|
|
|
+ make.leading.top.trailing.equalToSuperview()
|
|
|
+ make.height.equalTo(k_Nav_Height)
|
|
|
+ }
|
|
|
+
|
|
|
+ if viewModel.uiStyle == .history {
|
|
|
+ addNormalNavBarView()
|
|
|
+ setPageTitle("History".localized)
|
|
|
+ _ = setNavigationItem("", imageName: "delete_white", direction: .right, action: #selector(clickDelete))
|
|
|
+ }else{
|
|
|
+ navBarContentView.addSubview(navBarView)
|
|
|
+ navBarView.snp.makeConstraints { make in
|
|
|
+ make.edges.equalToSuperview()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
func configureMessageCollectionView() {
|
|
|
- view.backgroundColor = .clear
|
|
|
+ clearAndResetConstraints()
|
|
|
+ view.backgroundColor = .mainBg
|
|
|
//设置自定义FlowLayout,itemsize等,都在这里控制
|
|
|
let flowLayout = CustomMessagesFlowLayout()
|
|
|
flowLayout.sectionInset = UIEdgeInsets(top: 4, left: 0, bottom: 4, right: 0)
|
|
@@ -200,6 +265,25 @@ class TSChatViewController: MessagesViewController, MessagesDataSource {
|
|
|
messagesCollectionView.reloadData()
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ func clearAndResetConstraints() {
|
|
|
+ // 筛选出与 messagesCollectionView 相关的约束
|
|
|
+ let constraintsToRemove = view.constraints.filter { constraint in
|
|
|
+ return (constraint.firstItem as? UIView == messagesCollectionView) || (constraint.secondItem as? UIView == messagesCollectionView)
|
|
|
+ }
|
|
|
+ // 停用并移除这些约束
|
|
|
+ NSLayoutConstraint.deactivate(constraintsToRemove)
|
|
|
+ for constraint in constraintsToRemove {
|
|
|
+ view.removeConstraint(constraint)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ messagesCollectionView.snp.remakeConstraints { make in
|
|
|
+ make.leading.trailing.bottom.equalTo(0)
|
|
|
+ make.top.equalTo(k_Nav_Height)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
func configureMessageInputBar() {
|
|
|
|
|
|
inputBarBgView.addSubview(inputBarTopView)
|
|
@@ -499,6 +583,7 @@ extension TSChatViewController{
|
|
|
|
|
|
sendMessages(data)
|
|
|
messagesCollectionView.scrollToLastItem(animated: true)
|
|
|
+ view.endEditing(true)
|
|
|
}
|
|
|
|
|
|
|