From 1c8b755ce0ada70d1dbdb831e4e6e3faf1848fbf Mon Sep 17 00:00:00 2001 From: Benoit Martins Date: Tue, 17 Jan 2023 10:02:00 +0100 Subject: [PATCH] Add loading spinner for to wait for media data --- .../Views/ChatConversationViewSwift.swift | 58 ++++++++++++++++--- Classes/Swift/Chat/Views/MessageView.swift | 3 +- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/Classes/Swift/Chat/Views/ChatConversationViewSwift.swift b/Classes/Swift/Chat/Views/ChatConversationViewSwift.swift index 4d827003d..c14833d4e 100644 --- a/Classes/Swift/Chat/Views/ChatConversationViewSwift.swift +++ b/Classes/Swift/Chat/Views/ChatConversationViewSwift.swift @@ -76,6 +76,9 @@ import AVFoundation return collectionView }() + let loadingView = UIView() + let loading = RotatingSpinner() + let menu: DropDown = { let menu = DropDown() menu.dataSource = [""] @@ -181,7 +184,6 @@ import AVFoundation override func viewDidAppear(_ animated: Bool) { tableController.reloadData() - messageView.ephemeralIndicator.isHidden = (linphone_chat_room_ephemeral_enabled(chatRoom?.getCobject) == 0) } override func viewDidDisappear(_ animated: Bool) { @@ -1024,6 +1026,15 @@ import AVFoundation collectionView.dataSource = self collectionView.delegate = self collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell") + + + loadingView.backgroundColor = .gray.withAlphaComponent(0.6) + mediaSelector.addSubview(loadingView) + loadingView.matchParentEdges().done() + + + loadingView.addSubview(loading) + loading.square(Int(top_bar_height*2)).alignVerticalCenterWith(loadingView).alignHorizontalCenterWith(loadingView).done() } @objc func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { @@ -1031,17 +1042,13 @@ import AVFoundation } @objc(collectionView:cellForItemAtIndexPath:) func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { - if(self.mediaCollectionView.count > 0 && !mediaSelectorVisible){ - self.selectionMedia() - self.messageView.sendButton.isEnabled = true - self.messageView.fileContext = true - } - let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) let viewCell: UIView = UIView(frame: cell.contentView.frame) cell.addSubview(viewCell) - let deleteButton = CallControlButton(width: 22, height: 22, buttonTheme:VoipTheme.nav_black_button("reply_cancel"), onClickAction: { + let deleteButton = CallControlButton(width: 22, height: 22, buttonTheme:VoipTheme.nav_black_button("reply_cancel")) + + deleteButton.onClickAction = { self.collectionView.deleteItems(at: [indexPath]) self.mediaCollectionView.remove(at: indexPath.row) self.mediaURLCollection.remove(at: indexPath.row) @@ -1055,7 +1062,7 @@ import AVFoundation self.messageView.sendButton.isEnabled = true } } - }) + } let imageCell = mediaCollectionView[indexPath.row] var myImageView = UIImageView() @@ -1095,6 +1102,22 @@ import AVFoundation @available(iOS 14.0, *) func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { + + let mediaCount = mediaCollectionView.count + let newMediaCount = results.count + loadingView.isHidden = false + messageView.isLoading = true + loading.startRotation() + + if(self.mediaCollectionView.count == 0 && results.count >= 1){ + self.selectionMedia() + self.messageView.sendButton.isEnabled = !messageView.isLoading + self.messageView.fileContext = true + } + if(self.mediaCollectionView.count > 0){ + self.messageView.sendButton.isEnabled = !messageView.isLoading + } + picker.dismiss(animated: true, completion: nil) let itemProviders = results.map(\.itemProvider) for item in itemProviders { @@ -1109,6 +1132,14 @@ import AVFoundation let indexPath = IndexPath(row: self.mediaCollectionView.count, section: 0) self.mediaCollectionView.append(image) self.collectionView.insertItems(at: [indexPath]) + if(mediaCount + newMediaCount <= indexPath.row+1){ + if(self.mediaCollectionView.count > 0){ + self.messageView.sendButton.isEnabled = true + } + self.loadingView.isHidden = true + self.messageView.isLoading = false + self.loading.stopRotation() + } }, completion: nil) } } @@ -1138,6 +1169,14 @@ import AVFoundation } catch let error{ print(error.localizedDescription) } + if(mediaCount + newMediaCount <= indexPath.row+1){ + if(self.mediaCollectionView.count > 0){ + self.messageView.sendButton.isEnabled = true + } + self.loadingView.isHidden = true + self.messageView.isLoading = false + self.loading.stopRotation() + } }, completion: nil) } } @@ -1254,6 +1293,7 @@ import AVFoundation } } } + controller.dismiss(animated: true) } } diff --git a/Classes/Swift/Chat/Views/MessageView.swift b/Classes/Swift/Chat/Views/MessageView.swift index 5b02f74a7..56f5e8665 100644 --- a/Classes/Swift/Chat/Views/MessageView.swift +++ b/Classes/Swift/Chat/Views/MessageView.swift @@ -37,6 +37,7 @@ class MessageView: UIView, UITextViewDelegate { let ephemeralIndicator = UIImageView(image: UIImage(named: "ephemeral_messages_color_A.png")) var fileContext = false var isComposing = false + var isLoading = false override init(frame: CGRect) { super.init(frame: frame) @@ -84,7 +85,7 @@ class MessageView: UIView, UITextViewDelegate { func textViewDidChangeSelection(_ textView: UITextView) { let chatRoom = ChatRoom.getSwiftObject(cObject: PhoneMainView.instance().currentRoom) - if messageText.text.isEmpty && fileContext == false { + if ((messageText.text.isEmpty && !fileContext) || isLoading) { sendButton.isEnabled = false } else { if !isComposing {