136 lines
3.5 KiB
Swift
136 lines
3.5 KiB
Swift
//
|
|
// ChatVC.swift
|
|
// yinmeng-ios
|
|
//
|
|
// Created by MaiMang on 2024/2/25.
|
|
// 回话详情
|
|
|
|
import UIKit
|
|
import NIMSDK
|
|
class ChatVC: BaseViewController {
|
|
|
|
// 键盘收起/弹出 滚动的时候收起输入栏
|
|
private var isBecome: Bool = false
|
|
/// 是否发送完成
|
|
private var isSended: Bool = true
|
|
/// 输入栏默认的高度
|
|
private let ToolBarLastH: CGFloat = 52
|
|
|
|
public init(session: NIMSession) {
|
|
vm = ChatViewModel(session: session)
|
|
super.init(nibName: nil, bundle: nil)
|
|
// vm.delegate = self
|
|
// NIMSDK.shared().mediaManager.add(self)
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
var vm:ChatViewModel
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
view.backgroundColor = ThemeColor(hexStr: "#F6F6F6")
|
|
let height = ScreenHeight - (ToolBarLastH + SafeAraeBottomHeight + NavHeight)
|
|
chatTableView.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: height)
|
|
view.addSubview(chatTableView)
|
|
view.addSubview(keyboardView)
|
|
}
|
|
|
|
///注册cell
|
|
private func registerChatCell() {
|
|
chatTableView.register(cellType: ChatTextCell.self)
|
|
}
|
|
|
|
private lazy var chatTableView: UITableView = {
|
|
let tableView = UITableView(frame: .zero, style: .plain)
|
|
tableView.delegate = self
|
|
tableView.dataSource = self
|
|
tableView.tableFooterView = UIView()
|
|
tableView.separatorStyle = .none
|
|
tableView.backgroundColor = .clear
|
|
if #available(iOS 11.0, *) {
|
|
tableView.contentInsetAdjustmentBehavior = .never
|
|
}
|
|
return tableView
|
|
}()
|
|
|
|
private lazy var keyboardView: ChatKeyboardView = {
|
|
let toolBarY = ScreenHeight - NavHeight - ToolBarLastH - SafeAraeBottomHeight
|
|
let view = ChatKeyboardView(frame: CGRect(x: 0, y: toolBarY, width: ScreenWidth, height: ToolBarLastH))
|
|
view.delegate = self
|
|
return view
|
|
}()
|
|
|
|
}
|
|
|
|
// MARK: - ChatKeyboardViewDelegate
|
|
extension ChatVC: ChatKeyboardViewDelegate {
|
|
|
|
func keyboard(_ keyboard: ChatKeyboardView, DidFinish content: String) {
|
|
///发送消息
|
|
vm.sendTextMessage(text: content) { error in
|
|
|
|
}
|
|
}
|
|
|
|
func keyboard(_ keyboard: ChatKeyboardView, DidBecome isBecome: Bool) {
|
|
self.isSended = true
|
|
self.isBecome = isBecome
|
|
}
|
|
|
|
func keyboard(_ keyboard: ChatKeyboardView, DidMoreMenu type: ChatMoreMenuType) {
|
|
|
|
//TODO: 点击更多
|
|
}
|
|
|
|
func keyboard(_ keyboard: ChatKeyboardView, DidObserver offsetY: CGFloat) {
|
|
|
|
}
|
|
}
|
|
|
|
extension ChatVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
|
|
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
return vm.messageObjects.count
|
|
}
|
|
|
|
public func tableView(_ tableView: UITableView,
|
|
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
let model = vm.messageObjects[safe: indexPath.row]
|
|
if model?.type == .text {
|
|
let cell = tableView.dequeueReusableCell(for: indexPath, cellType: ChatTextCell.self)
|
|
cell.model = model
|
|
}
|
|
return UITableViewCell()
|
|
}
|
|
|
|
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
}
|
|
|
|
public func tableView(_ tableView: UITableView,
|
|
heightForRowAt indexPath: IndexPath) -> CGFloat {
|
|
let m = vm.messageObjects[safe:indexPath.row]
|
|
return CGFloat(m?.height ?? 0)
|
|
}
|
|
|
|
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
|
|
if isSended {
|
|
isSended = false
|
|
}
|
|
}
|
|
|
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
|
if isSended {
|
|
return
|
|
}
|
|
|
|
if chatTableView.y <= 0 && isBecome {
|
|
DispatchQueue.main.async {
|
|
NotificationCenter.default.post(name: .kChatTextKeyboardNeedHide, object: nil)
|
|
}
|
|
}
|
|
}
|
|
}
|