diff --git a/Classes/FloatingScrollDownButton.swift b/Classes/FloatingScrollDownButton.swift index 79633729b..dc7f3aae3 100644 --- a/Classes/FloatingScrollDownButton.swift +++ b/Classes/FloatingScrollDownButton.swift @@ -8,7 +8,7 @@ import Foundation import UIKit -public extension ChatConversationTableView { +extension ChatConversationTableViewSwift { private enum Constants { static let trailingValue: CGFloat = 30.0 @@ -17,6 +17,7 @@ public extension ChatConversationTableView { static let buttonWidth: CGFloat = 16.0 } + /* override func viewDidLoad() { super.viewDidLoad() @@ -27,28 +28,15 @@ public extension ChatConversationTableView { super.viewDidAppear(animated) createFloatingButton() } + */ override func viewDidDisappear(_ animated: Bool) { super.viewDidAppear(animated) self.floatingScrollButton?.removeFromSuperview() self.floatingScrollBackground?.removeFromSuperview() } - - override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { - if let lastCellRowIndex = tableView.indexPathsForVisibleRows?.last?.row { - if( lastCellRowIndex != self.totalNumberOfItems() - 1) { - self.floatingScrollButton?.isHidden = false - self.floatingScrollBackground?.isHidden = false; - self.scrollBadge?.isHidden = (self.scrollBadge?.text == nil) - } else { - self.floatingScrollButton?.isHidden = true - self.floatingScrollBackground?.isHidden = true; - self.scrollBadge?.text = nil - } - } - } - private func createFloatingButton() { + func createFloatingButton() { self.floatingScrollButton = UIButton(type: .custom) self.floatingScrollBackground = UIButton(type: .custom) self.floatingScrollButton?.translatesAutoresizingMaskIntoConstraints = false @@ -97,7 +85,7 @@ public extension ChatConversationTableView { } @IBAction private func scrollToBottomButtonAction() { - scroll(toBottom: true) + scrollToBottomWithRelaod() } diff --git a/Classes/LinphoneUI/UIChatBubbleTextCell.m b/Classes/LinphoneUI/UIChatBubbleTextCell.m index aedfb3bd2..b149e1a17 100644 --- a/Classes/LinphoneUI/UIChatBubbleTextCell.m +++ b/Classes/LinphoneUI/UIChatBubbleTextCell.m @@ -324,11 +324,13 @@ - (void)onDelete { if (_message != NULL) { + /* UITableView *tableView = VIEW(ChatConversationViewSwift).tableController.tableView; NSIndexPath *indexPath = [tableView indexPathForCell:self]; [tableView.dataSource tableView:tableView commitEditingStyle:UITableViewCellEditingStyleDelete forRowAtIndexPath:indexPath]; + */ } } @@ -341,8 +343,8 @@ static void message_status(LinphoneChatMessage *msg, LinphoneChatMessageState st if (!linphone_chat_message_is_outgoing(msg) || (state != LinphoneChatMessageStateFileTransferDone && state != LinphoneChatMessageStateFileTransferInProgress)) { LinphoneEventLog *event = (LinphoneEventLog *)linphone_chat_message_cbs_get_user_data(linphone_chat_message_get_callbacks(msg)); ChatConversationViewSwift *view = VIEW(ChatConversationViewSwift); - [view.tableController updateEventEntry:event]; - [view.tableController scrollToBottom:true]; + //[view.tableController updateEventEntry:event]; + //[view.tableController scrollToBottom:true]; } } @@ -767,7 +769,7 @@ static const CGFloat REPLY_OR_FORWARD_TAG_HEIGHT = 18; - (void)layoutSubviews { [super layoutSubviews]; if (_message != nil) { - UITableView *tableView = VIEW(ChatConversationViewSwift).tableController.tableView; + //UITableView *tableView = VIEW(ChatConversationViewSwift).tableController.tableView; BOOL is_outgoing = linphone_chat_message_is_outgoing(_message); CGRect bubbleFrame = _bubbleView.frame; int available_width = self.frame.size.width; @@ -778,11 +780,13 @@ static const CGFloat REPLY_OR_FORWARD_TAG_HEIGHT = 18; bubbleFrame.size.width = MAX(bubbleFrame.size.width, 300); } + /* if (tableView.isEditing) { origin_x = 0; } else { origin_x = (is_outgoing ? self.frame.size.width - bubbleFrame.size.width : 0); } + */ CGRect r = _messageText.frame; r.origin.y = linphone_chat_message_is_reply(_message) ? _replyView.view.frame.origin.y + _replyView.view.frame.size.height + 5 : 3; @@ -947,7 +951,7 @@ static const CGFloat REPLY_OR_FORWARD_TAG_HEIGHT = 18; [_messageActionsBlocks addObject:^{ [thiz dismissPopup]; linphone_chat_room_delete_message(linphone_chat_message_get_chat_room(message), message); - [VIEW(ChatConversationViewSwift).tableController reloadData]; + //[VIEW(ChatConversationViewSwift).tableController reloadData]; }]; } @@ -959,7 +963,7 @@ static const CGFloat REPLY_OR_FORWARD_TAG_HEIGHT = 18; return; - [VIEW(ChatConversationViewSwift).tableController dismissMessagesPopups]; + //[VIEW(ChatConversationViewSwift).tableController dismissMessagesPopups]; [self buildActions]; int width = 250; int cellHeight = 45; @@ -967,13 +971,14 @@ static const CGFloat REPLY_OR_FORWARD_TAG_HEIGHT = 18; CGRect screenRect = UIScreen.mainScreen.bounds; int menuHeight = numberOfItems * cellHeight; + /* CGRect frame = CGRectMake( linphone_chat_message_is_outgoing(self.message) ? screenRect.size.width - width - 10 : 10, (self.frame.origin.y + self.frame.size.height) - [VIEW(ChatConversationViewSwift).tableController .tableView contentOffset].y > screenRect.size.height /2 ? self.frame.origin.y - menuHeight - 10: self.frame.origin.y + self.frame.size.height, width, - menuHeight); + menuHeight);*/ - _popupMenu = [[UITableView alloc]initWithFrame:frame]; + //_popupMenu = [[UITableView alloc]initWithFrame:frame]; _popupMenu.scrollEnabled = false; _popupMenu.dataSource = self; _popupMenu.delegate = self; @@ -991,11 +996,11 @@ static const CGFloat REPLY_OR_FORWARD_TAG_HEIGHT = 18; _popupMenu.editing = NO; _popupMenu.userInteractionEnabled = true; [_popupMenu reloadData]; - [VIEW(ChatConversationViewSwift).tableController.view addSubview:_popupMenu]; + //[VIEW(ChatConversationViewSwift).tableController.view addSubview:_popupMenu]; UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOutsideMenu:)]; tapGestureRecognizer.cancelsTouchesInView = NO; tapGestureRecognizer.numberOfTapsRequired = 1; - [VIEW(ChatConversationViewSwift).tableController.view addGestureRecognizer:tapGestureRecognizer]; + //[VIEW(ChatConversationViewSwift).tableController.view addGestureRecognizer:tapGestureRecognizer]; } -(void) dismissPopup { @@ -1008,10 +1013,12 @@ static const CGFloat REPLY_OR_FORWARD_TAG_HEIGHT = 18; -(void) tapOutsideMenu:(UITapGestureRecognizer *) g { + /* CGPoint p = [g locationInView:VIEW(ChatConversationViewSwift).tableController.view]; if (!CGRectContainsPoint(_popupMenu.frame,p)) { [self dismissPopup]; } + */ } -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { diff --git a/Classes/Swift/Chat/ViewModels/ChatConversationViewModel.swift b/Classes/Swift/Chat/ViewModels/ChatConversationViewModel.swift index 0ee272eac..12eb514c0 100644 --- a/Classes/Swift/Chat/ViewModels/ChatConversationViewModel.swift +++ b/Classes/Swift/Chat/ViewModels/ChatConversationViewModel.swift @@ -21,7 +21,6 @@ class ChatConversationViewModel: ControlsViewModel { var mediaCount : Int = 0 var newMediaCount : Int = 0 - var unread_msg : Int32 = 0 var address: String? = nil var participants: String? = nil @@ -157,7 +156,6 @@ class ChatConversationViewModel: ControlsViewModel { } messageReceived.value = event_log - unread_msg = linphone_chat_room_get_unread_messages_count(cr?.getCobject) } func on_chat_room_state_changed(_ cr: ChatRoom?) { diff --git a/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift b/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift index 31e40ebdb..46e17f423 100644 --- a/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift +++ b/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift @@ -29,6 +29,10 @@ class ChatConversationTableViewSwift: UIViewController, UICollectionViewDataSour var basic :Bool = false + var floatingScrollButton : UIButton? + var scrollBadge : UILabel? + var floatingScrollBackground : UIButton? + override func viewDidLoad() { super.viewDidLoad() @@ -82,13 +86,13 @@ class ChatConversationTableViewSwift: UIViewController, UICollectionViewDataSour } override func viewWillAppear(_ animated: Bool) { - //ChatConversationTableViewModel.sharedModel.updateData() collectionView.reloadData() } override func viewDidAppear(_ animated: Bool) { + createFloatingButton() if ChatConversationTableViewModel.sharedModel.getNBMessages() > 0 { - self.collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: false) + scrollToBottom() } } @@ -106,33 +110,50 @@ class ChatConversationTableViewSwift: UIViewController, UICollectionViewDataSour func scrollToBottom(){ self.collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .bottom, animated: true) + ChatConversationViewSwift.markAsRead(ChatConversationViewModel.sharedModel.chatRoom?.getCobject) + scrollBadge!.text = "0" + } + + func scrollToBottomWithRelaod(){ + let isDisplayingBottomOfTable = collectionView.indexPathsForVisibleItems.sorted().first?.row == 0 + collectionView.reloadData() + if isDisplayingBottomOfTable { + self.collectionView.scrollToItem(at: IndexPath(item: 1, section: 0), at: .bottom, animated: false) + } + DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { + self.scrollToBottom() + } } func refreshData(){ - let indexBottom = self.collectionView.indexPathsForVisibleItems.sorted().first?.row - let offset = self.collectionView.contentOffset - print("MultilineMessageCell configure \(offset) \(indexBottom)") + let indexBottom = collectionView.indexPathsForVisibleItems.sorted().first?.row + let isDisplayingBottomOfTable = collectionView.indexPathsForVisibleItems.sorted().first?.row == 0 collectionView.reloadData() - self.collectionView.scrollToItem(at: IndexPath(row: indexBottom! + 1, section: 0), at: .top, animated: false) - /* - DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { - let offsetIndex = self.collectionView.contentOffset - print("MultilineMessageCell configure \(offsetIndex)") - self.collectionView.setContentOffset(CGPoint(x: self.collectionView.contentOffset.x, y: self.collectionView.contentOffset.y + offset.y + 10), animated: false) + if isDisplayingBottomOfTable { + self.collectionView.scrollToItem(at: IndexPath(item: 1, section: 0), at: .bottom, animated: false) + DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { + self.scrollToBottom() + } + } else { + DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { + self.collectionView.contentOffset = CGPoint(x: self.collectionView.contentOffset.x, y: self.collectionView.contentOffset.y + (self.collectionView.cellForItem(at: IndexPath(row: indexBottom! + 1, section: 0))?.frame.size.height)! + 2.0) + } + scrollBadge!.isHidden = false + scrollBadge!.text = "\(ChatConversationViewModel.sharedModel.chatRoom?.unreadMessagesCount ?? 0)" } - */ - - //self.collectionView.scrollToItem(at: IndexPath(item: 1, section: 0), at: .bottom, animated: false) - //DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { - // self.scrollToBottom() - //} } // MARK: - UICollectionViewDataSource - func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MultilineMessageCell.reuseId, for: indexPath) as! MultilineMessageCell - + + if(indexPath.row <= 1) { + self.floatingScrollButton?.isHidden = true + self.floatingScrollBackground?.isHidden = true; + self.scrollBadge?.text = "0" + } + if let event = ChatConversationTableViewModel.sharedModel.getMessage(index: indexPath.row){ cell.configure(event: event) @@ -159,6 +180,12 @@ class ChatConversationTableViewSwift: UIViewController, UICollectionViewDataSour if cell.isPlayingVoiceRecording { AudioPlayer.stopSharedPlayer() } + + if(indexPath.row <= 1) { + self.floatingScrollButton?.isHidden = false + self.floatingScrollBackground?.isHidden = false; + self.scrollBadge?.isHidden = true + } } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { diff --git a/Classes/Swift/Chat/Views/ChatConversationViewSwift.swift b/Classes/Swift/Chat/Views/ChatConversationViewSwift.swift index 42391b14b..ba609226e 100644 --- a/Classes/Swift/Chat/Views/ChatConversationViewSwift.swift +++ b/Classes/Swift/Chat/Views/ChatConversationViewSwift.swift @@ -37,7 +37,6 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll @objc var linphoneChatRoom: OpaquePointer? = nil - @objc let tableController = ChatConversationTableView() @objc var tableControllerSwift = ChatConversationTableViewSwift() @objc var pendingForwardMessage : OpaquePointer? = nil @objc var sharingMedia : Bool = false @@ -157,18 +156,6 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll } ChatConversationViewModel.sharedModel.messageReceived.observe { message in - let isDisplayingBottomOfTable = self.tableController.tableView.indexPathsForVisibleRows?.last?.row == (self.tableController.totalNumberOfItems() ) - 1 - self.tableController.addEventEntry(message?.getCobject) - - if isDisplayingBottomOfTable { - self.tableController.scroll(toBottom: true) - self.tableController.scrollBadge!.text = nil - self.tableController.scrollBadge!.isHidden = true - } else { - self.tableController.scrollBadge!.isHidden = false - self.tableController.scrollBadge!.text = "\(ChatConversationViewModel.sharedModel.unread_msg+1)" - } - self.tableControllerSwift.refreshData() } @@ -179,9 +166,8 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll } ChatConversationViewModel.sharedModel.secureLevelChanged.observe { secure in - self.tableController.addEventEntry(secure?.getCobject) self.updateParticipantLabel() - self.tableController.scroll(toBottom: true) + self.tableControllerSwift.scrollToBottomWithRelaod() self.changeSecureLevel(secureLevel: ChatConversationViewModel.sharedModel.secureLevel != nil, imageBadge: ChatConversationViewModel.sharedModel.secureLevel) } @@ -190,14 +176,12 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll if let subjectVM { self.titleGroupLabel.text = subjectVM self.titleLabel.text = subjectVM - self.tableController.addEventEntry(subject?.getCobject) - self.tableController.scroll(toBottom: true) + self.tableControllerSwift.scrollToBottomWithRelaod() } } ChatConversationViewModel.sharedModel.eventLog.observe { event in - self.tableController.addEventEntry(event?.getCobject) - self.tableController.scroll(toBottom: true) + self.tableControllerSwift.scrollToBottomWithRelaod() } ChatConversationViewModel.sharedModel.indexPathVM.observe { index in @@ -229,7 +213,6 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll ChatConversationViewModel.sharedModel.createChatConversation() topBar.backgroundColor = VoipTheme.voipToolbarBackgroundColor.get() - self.contentView.addSubview(tableController.tableView) self.contentView.addSubview(tableControllerSwift.view) // Setup Autolayout constraints @@ -239,19 +222,14 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll tableControllerSwift.view.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0).isActive = true tableControllerSwift.view.rightAnchor.constraint(equalTo: self.contentView.rightAnchor, constant: 0).isActive = true - tableController.chatRoom = ChatConversationViewModel.sharedModel.chatRoom?.getCobject ChatConversationTableViewModel.sharedModel.chatRoom = ChatConversationViewModel.sharedModel.chatRoom - refreshControl.addTarget(self, action: #selector(refreshData), for: .valueChanged) - tableController.refreshControl = refreshControl - tableController.toggleSelectionButton = action1SelectAllButton messageView.sendButton.onClickAction = onSendClick messageView.pictureButton.onClickAction = alertAction messageView.voiceRecordButton.onClickAction = onVrStart recordingDeleteButton.onClickAction = cancelVoiceRecording recordingPlayButton.onClickAction = onvrPlayPauseStop recordingStopButton.onClickAction = onvrPlayPauseStop - tableController.tableView.separatorColor = .clear if !ChatConversationViewModel.sharedModel.chatRoom!.isReadOnly { messageView.ephemeralIndicator.isHidden = !ChatConversationViewModel.sharedModel.chatRoom!.ephemeralEnabled @@ -264,10 +242,6 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll ChatConversationViewModel.sharedModel.shareFile() } - override func viewDidAppear(_ animated: Bool) { - tableController.reloadData() - } - override func viewDidDisappear(_ animated: Bool) { resetView() } @@ -302,8 +276,6 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll self.messageView.pictureButton.isEnabled = true isComposingTextView.text = "" - - tableController.floatingScrollBackground?.isHidden = true; } func goBackChatListView() { @@ -630,36 +602,23 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll @objc func dismissOnTapOutsideOrCancel(){ self.dismiss(animated: true, completion: nil) } - - @objc func refreshData() { - tableController.refreshData() - refreshControl.endRefreshing() - if tableController.totalNumberOfItems() == 0 { - return - } - tableController.loadData() - tableController.tableView.scrollToRow( - at: IndexPath(row: tableController.currentIndex, section: 0), - at: .top, - animated: false) - } - + override func editModeOn(){ super.editModeOn() - tableController.setEditing(true, animated: false) + //tableController.setEditing(true, animated: false) } override func editModeOff(){ super.editModeOff() - tableController.setEditing(false, animated: false) + //tableController.setEditing(false, animated: false) } override func selectDeselectAll(){ super.selectDeselectAll() if(action1SelectAllButton.isHidden){ - tableController.onSelectionToggle(action1SelectAllButton) + //tableController.onSelectionToggle(action1SelectAllButton) }else{ - tableController.onSelectionToggle(action1SelectAllButton) + //tableController.onSelectionToggle(action1SelectAllButton) } } @@ -677,9 +636,9 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll onCancelClick: { [self] in onEditionChangeClick()}, onConfirmationClick: { - self.tableController.removeSelection(nil) + //self.tableController.removeSelection(nil) self.editModeOff() - self.tableController.loadData() + //self.tableController.loadData() } ) } @@ -762,7 +721,7 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll fileTransfer.text = messageView.messageText.text fileTransfer.uploadFileContent(forSwift: ChatConversationViewModel.sharedModel.fileContext, urlList: ChatConversationViewModel.sharedModel.mediaURLCollection, for: ChatConversationViewModel.sharedModel.chatRoom?.getCobject, rootMessage: rootMessage?.getCobject) messageView.messageText.text = "" - tableController.scroll(toBottom: true) + tableControllerSwift.scrollToBottomWithRelaod() return true } @@ -779,7 +738,7 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll func startUploadData(_ data: Data?, withType type: String?, withName name: String?, andMessage message: String?, rootMessage: ChatMessage?){ ChatConversationViewModel.sharedModel.startUploadData(data, withType: type, withName: name, andMessage: message, rootMessage: rootMessage) - tableController.scroll(toBottom: true) + tableControllerSwift.scrollToBottomWithRelaod() } func setComposingVisible(_ visible: Bool, withDelay delay: CGFloat) { @@ -800,42 +759,21 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll isComposingTextView.text = String.localizedStringWithFormat(NSLocalizedString("%@ are writing...", comment: ""), composingAddresses!) } } - var isBottomOfView = false - if (tableController.tableView.contentOffset.y + 1) >= (tableController.tableView.contentSize.height - tableController.tableView.frame.size.height) { - isBottomOfView = true - } UIView.animate(withDuration: 0.3, animations: { self.isComposingView.isHidden = !self.isComposingView.isHidden }) - if(isBottomOfView){ - tableController.scroll(toBottom: false) - } } func selectionMedia() { - var isBottomOfView = false - if (tableController.tableView.contentOffset.y + 1) >= (tableController.tableView.contentSize.height - tableController.tableView.frame.size.height) { - isBottomOfView = true - } UIView.animate(withDuration: 0.3, animations: { self.mediaSelector.isHidden = !self.mediaSelector.isHidden }) - if(isBottomOfView){ - tableController.scroll(toBottom: false) - } } func setRecordingVisible(visible : Bool) { - var isBottomOfView = false - if (tableController.tableView.contentOffset.y + 1) >= (tableController.tableView.contentSize.height - tableController.tableView.frame.size.height) { - isBottomOfView = true - } UIView.animate(withDuration: 0.3, animations: { self.recordingView.isHidden = visible }) - if(isBottomOfView){ - tableController.scroll(toBottom: false) - } } func initReplyView(_ visible: Bool, message: OpaquePointer?) { @@ -895,16 +833,9 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll } } - var isBottomOfView = false - if (tableController.tableView.contentOffset.y + 1) >= (tableController.tableView.contentSize.height - tableController.tableView.frame.size.height) { - isBottomOfView = true - } UIView.animate(withDuration: 0.3, animations: { self.replyBubble.isHidden = !self.replyBubble.isHidden }) - if(isBottomOfView){ - tableController.scroll(toBottom: false) - } } @objc class func getKeyFromFileType(_ fileType: String?, fileName name: String?) -> String? { @@ -1296,7 +1227,7 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll func startFileUpload(_ data: Data?, withName name: String?, rootMessage: ChatMessage?){ ChatConversationViewModel.sharedModel.startFileUpload(data, withName: name, rootMessage: rootMessage) - tableController.scroll(toBottom: true) + tableControllerSwift.scrollToBottomWithRelaod() } @objc class func getFileUrl(_ name: String?) -> URL? {