// // ChatConversationTableViewSwift.swift // linphone // // Created by BenoƮt Martins on 20/02/2023. // import UIKit import Foundation import linphonesw class ChatConversationTableViewSwift: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { let controlsView = ControlsView(showVideo: true, controlsViewModel: ChatConversationTableViewModel.sharedModel) static let compositeDescription = UICompositeViewDescription(ChatConversationTableViewSwift.self, statusBar: StatusBarView.self, tabBar: nil, sideMenu: SideMenuView.self, fullscreen: false, isLeftFragment: false,fragmentWith: nil) static func compositeViewDescription() -> UICompositeViewDescription! { return compositeDescription } func compositeViewDescription() -> UICompositeViewDescription! { return type(of: self).compositeDescription } var collectionView: UICollectionView = { let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout()) return collectionView }() var basic :Bool = false override func viewDidLoad() { super.viewDidLoad() self.initView() UIDeviceBridge.displayModeSwitched.readCurrentAndObserve { _ in self.collectionView.backgroundColor = VoipTheme.backgroundWhiteBlack.get() } NotificationCenter.default.addObserver(self, selector: #selector(self.rotated), name: UIDevice.orientationDidChangeNotification, object: nil) ChatConversationTableViewModel.sharedModel.nbEventDisplayed.observe { index in self.collectionView.reloadData() } collectionView.isUserInteractionEnabled = true } deinit { NotificationCenter.default.removeObserver(self) } @objc func rotated() { collectionView.reloadData() } func initView(){ basic = isBasicChatRoom(ChatConversationTableViewModel.sharedModel.chatRoom?.getCobject) view.addSubview(collectionView) collectionView.backgroundColor = VoipTheme.backgroundWhiteBlack.get() collectionView.contentInsetAdjustmentBehavior = .always collectionView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0) collectionView.translatesAutoresizingMaskIntoConstraints = false collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true collectionView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true collectionView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true collectionView.dataSource = self collectionView.delegate = self collectionView.register(MultilineMessageCell.self, forCellWithReuseIdentifier: MultilineMessageCell.reuseId) (collectionView.collectionViewLayout as! UICollectionViewFlowLayout).estimatedItemSize = UICollectionViewFlowLayout.automaticSize (collectionView.collectionViewLayout as! UICollectionViewFlowLayout).minimumLineSpacing = 2 collectionView.transform = CGAffineTransform(scaleX: 1, y: -1) } override func viewWillAppear(_ animated: Bool) { //ChatConversationTableViewModel.sharedModel.updateData() collectionView.reloadData() } override func viewDidAppear(_ animated: Bool) { self.collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: false) } // MARK: - UICollectionViewDataSource - func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MultilineMessageCell.reuseId, for: indexPath) as! MultilineMessageCell if let message = ChatConversationTableViewModel.sharedModel.getMessage(index: indexPath.row){ cell.configure(message: message, isBasic: basic) } cell.contentView.transform = CGAffineTransform(scaleX: 1, y: -1) return cell } func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MultilineMessageCell.reuseId, for: indexPath) as! MultilineMessageCell if cell.isPlayingVoiceRecording { AudioPlayer.stopSharedPlayer() } } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return ChatConversationTableViewModel.sharedModel.getNBMessages() } // MARK: - UICollectionViewDelegateFlowLayout - func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let referenceHeight: CGFloat = 100 let referenceWidth: CGFloat = 100 return CGSize(width: referenceWidth, height: referenceHeight) } func isBasicChatRoom(_ room: OpaquePointer?) -> Bool { if room == nil { return true } let charRoomBasic = ChatRoom.getSwiftObject(cObject: room!) let isBasic = charRoomBasic.hasCapability(mask: Int(LinphoneChatRoomCapabilitiesBasic.rawValue)) return isBasic } }