Commit 561d4e73 authored by Steven杜宇's avatar Steven杜宇

// AI对话

parent 00202cfe
......@@ -570,6 +570,7 @@
04943BEA2CF1B44100BF2255 /* YHAIViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04943BE92CF1B44100BF2255 /* YHAIViewController.swift */; };
04943BED2CF46B6600BF2255 /* YHAIRequestManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04943BEC2CF46B6600BF2255 /* YHAIRequestManager.swift */; };
04943BF42CF4954800BF2255 /* YHAIMessageCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04943BF32CF4954800BF2255 /* YHAIMessageCell.swift */; };
04943BF62CF49E3E00BF2255 /* YHAITextInputView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04943BF52CF49E3E00BF2255 /* YHAITextInputView.swift */; };
049537852BA9B28500AF7E59 /* YHCertificateTemplateCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 049537842BA9B28500AF7E59 /* YHCertificateTemplateCell.swift */; };
049663BA2BF6105C003C8295 /* YHRefreshHeaderAnimator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 049663B92BF6105C003C8295 /* YHRefreshHeaderAnimator.swift */; };
049663C02BF729F6003C8295 /* galaxy_refresher.gif in Resources */ = {isa = PBXBuildFile; fileRef = 049663BF2BF729F5003C8295 /* galaxy_refresher.gif */; };
......@@ -1610,6 +1611,7 @@
04943BE92CF1B44100BF2255 /* YHAIViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHAIViewController.swift; sourceTree = "<group>"; };
04943BEC2CF46B6600BF2255 /* YHAIRequestManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHAIRequestManager.swift; sourceTree = "<group>"; };
04943BF32CF4954800BF2255 /* YHAIMessageCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHAIMessageCell.swift; sourceTree = "<group>"; };
04943BF52CF49E3E00BF2255 /* YHAITextInputView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHAITextInputView.swift; sourceTree = "<group>"; };
049537842BA9B28500AF7E59 /* YHCertificateTemplateCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHCertificateTemplateCell.swift; sourceTree = "<group>"; };
049663B92BF6105C003C8295 /* YHRefreshHeaderAnimator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHRefreshHeaderAnimator.swift; sourceTree = "<group>"; };
049663BF2BF729F5003C8295 /* galaxy_refresher.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = galaxy_refresher.gif; sourceTree = "<group>"; };
......@@ -3941,6 +3943,7 @@
isa = PBXGroup;
children = (
04943BF32CF4954800BF2255 /* YHAIMessageCell.swift */,
04943BF52CF49E3E00BF2255 /* YHAITextInputView.swift */,
);
path = V;
sourceTree = "<group>";
......@@ -6392,6 +6395,7 @@
04A671622BA058DE00C1FB91 /* YHCertificateEditSheetView.swift in Sources */,
04F4A52C2CB8F8190026D469 /* YHResignAppointedScheduleRiskTipsView.swift in Sources */,
04AD317D2C295AE800DF5655 /* YHUserInfoSettingViewModel.swift in Sources */,
04943BF62CF49E3E00BF2255 /* YHAITextInputView.swift in Sources */,
045A08C22C37EB8B00BCBABA /* YHMyFileListNoneCell.swift in Sources */,
04F2435F2C9D1C0600DF2C74 /* YHHKRecordsStayingPersonnelCell.swift in Sources */,
A5EB6C732C69FB9A0081FB6C /* YHOffivialApprovalSuccessCheckView.swift in Sources */,
......
......@@ -55,6 +55,7 @@ class YHAutoTextView: UITextView, UITextViewDelegate {
// 更新自身的高度
invalidateIntrinsicContentSize()
placeholderLabel.isHidden = !textView.text.isEmpty
text = textView.text
}
override var intrinsicContentSize: CGSize {
......
......@@ -32,24 +32,40 @@ class YHAIViewController: YHBaseViewController {
return tableView
}()
lazy var bottomInputView: YHAITextInputView = {
let v = YHAITextInputView(frame: .zero)
v.sendBlock = {
[weak self] text in
guard let self = self else { return }
let question = YHAIResponse.createQuestion(text)
messages.append(question)
self.scrollToBottom()
YHAIRequestManager.shared.requestAI(query: text) {
[weak self] res in
guard let self = self else { return }
self.handleMessage(res)
}
}
return v
}()
override func viewDidLoad() {
super.viewDidLoad()
gk_navTitle = "AI对话"
self.view.addSubview(self.tableView)
self.view.addSubview(self.bottomInputView)
bottomInputView.snp.makeConstraints { make in
make.left.right.bottom.equalToSuperview()
}
tableView.snp.makeConstraints { make in
make.left.right.bottom.equalTo(0)
make.left.right.equalTo(0)
make.top.equalTo(k_Height_NavigationtBarAndStatuBar)
make.bottom.equalTo(bottomInputView.snp.top).offset(-10)
}
requestData()
}
bottomInputView.addKeyBoardNotify()
func requestData() {
YHAIRequestManager.shared.requestAI(query: "今天深圳天气怎么样") {
[weak self] res in
guard let self = self else { return }
self.handleMessage(res)
}
}
func handleMessage(_ res: YHAIResponse) {
......@@ -59,7 +75,6 @@ class YHAIViewController: YHBaseViewController {
find = true
if msg.getType() == .text {
msg.body.contentText += res.body.contentText
}
}
}
......@@ -67,8 +82,14 @@ class YHAIViewController: YHBaseViewController {
if !find {
messages.append(res)
}
self.tableView.reloadData()
self.scrollToBottom()
}
func scrollToBottom() {
self.tableView.reloadData {
let indexPath = IndexPath(row: self.messages.count-1, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
}
}
......
......@@ -15,6 +15,25 @@ class YHAIMessageCell: UITableViewCell {
var message: YHAIResponse = YHAIResponse() {
didSet {
messageLabel.text = message.body.contentText
if message.isSelf {
whiteContentView.snp.remakeConstraints { make in
make.left.greaterThanOrEqualTo(20)
make.right.equalTo(-20)
make.top.equalTo(20)
make.bottom.equalTo(-20)
}
} else {
whiteContentView.snp.remakeConstraints { make in
make.left.equalTo(20)
make.right.lessThanOrEqualTo(-20)
make.top.equalTo(20)
make.bottom.equalTo(-20)
}
}
self.setNeedsLayout()
self.layoutIfNeeded()
}
}
......@@ -26,8 +45,6 @@ class YHAIMessageCell: UITableViewCell {
return v
}()
var
lazy var messageLabel:UILabel = {
let lable = UILabel()
lable.textColor = UIColor.mainTextColor
......@@ -48,6 +65,7 @@ class YHAIMessageCell: UITableViewCell {
func setupUI() {
selectionStyle = .none
contentView.backgroundColor = .clear
backgroundColor = .clear
contentView.addSubview(whiteContentView)
......
//
// YHAITextInputView.swift
// galaxy
//
// Created by Dufet on 2024/11/25.
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//
import UIKit
import IQKeyboardManagerSwift
class YHAITextInputView: UIView {
var sendBlock: ((String)->())?
static let height = 72.0
lazy var contentView: UIView = {
let v = UIView()
v.backgroundColor = .white
v.layer.cornerRadius = 12.0
v.layer.borderWidth = 2.0
v.layer.borderColor = UIColor.brandMainColor.cgColor
v.clipsToBounds = true
return v
}()
lazy var whiteView: UIView = {
let v = UIView()
v.backgroundColor = .white
return v
}()
lazy var sendBtn: UIButton = {
let btn = UIButton(type: .custom)
btn.backgroundColor = .red
btn.addTarget(self, action: #selector(didSendButtonClicked), for: .touchUpInside)
return btn
}()
@objc func didSendButtonClicked() {
sendBlock?(textView.text)
textView.text = ""
}
var textView: YHAutoTextView = {
let v = YHAutoTextView(frame: .zero)
return v
}()
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) {
super.init(frame: frame)
createUI()
}
deinit {
self.removeNotify()
}
func addKeyBoardNotify() {
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
func removeNotify() {
NotificationCenter.default.removeObserver(self)
}
func createUI() {
self.addSubview(whiteView)
whiteView.addSubview(contentView)
contentView.addSubview(sendBtn)
contentView.addSubview(textView)
whiteView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
contentView.snp.makeConstraints { make in
make.left.equalTo(20)
make.top.equalTo(10)
make.bottom.equalTo(-20)
make.right.equalTo(-20)
}
sendBtn.snp.makeConstraints { make in
make.width.equalTo(50)
make.height.equalTo(50)
make.right.equalTo(-16)
make.bottom.equalTo(-13)
}
textView.snp.makeConstraints { make in
make.left.equalTo(16)
make.top.equalTo(16)
make.bottom.equalTo(-16)
make.right.equalTo(sendBtn.snp.left).offset(-10)
}
}
@objc func handleKeyboardNotification(_ notification: Notification) {
if notification.userInfo != nil {
guard let keyboardFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue else {return }
let isKeyboardShow = notification.name == UIResponder.keyboardWillShowNotification
let bottomMargin = (isKeyboardShow ? -keyboardFrame.height : 0)
if !isKeyboardShow {
IQKeyboardManager.shared.enable = true
}
self.snp.updateConstraints { make in
make.bottom.equalToSuperview().offset(bottomMargin)
}
self.setNeedsLayout()
self.layoutIfNeeded()
if !isKeyboardShow {
// self.dismiss()
}
}
}
}
......@@ -22,6 +22,7 @@ class YHAIResponse: CustomStringConvertible {
var event: String = ""
var data: [String : Any] = [:]
var uuid: String = ""
var isSelf: Bool = false
var body = YHAIResponseBody()
......@@ -35,6 +36,21 @@ class YHAIResponse: CustomStringConvertible {
return false
}
func setFinished() {
event = "conversation.message.completed"
}
static func createQuestion(_ text: String) -> YHAIResponse {
let question = YHAIResponse()
question.isSelf = true
let body = YHAIResponseBody()
body.contentType = 1
body.contentText = text
question.setFinished()
question.body = body
return question
}
func getType() -> YHAIResponseType {
if let type = data["contentType"] as? Int, type == 1 {
return .text
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment