From 8de8c30812515cc9964fa2835db358bd5144257e Mon Sep 17 00:00:00 2001 From: QuentinArguillere Date: Fri, 16 Oct 2020 15:59:15 +0200 Subject: [PATCH] Add animation and wait for callback to confirm download --- .../ChatRoomTutorial/ChatRoomExample.swift | 17 +++++++++-------- .../ChatRoomTutorial/ContentView.swift | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/swift/ChatRoomTutorial/ChatRoomTutorial/ChatRoomExample.swift b/swift/ChatRoomTutorial/ChatRoomTutorial/ChatRoomExample.swift index d054054..7cc0d20 100644 --- a/swift/ChatRoomTutorial/ChatRoomTutorial/ChatRoomExample.swift +++ b/swift/ChatRoomTutorial/ChatRoomTutorial/ChatRoomExample.swift @@ -44,7 +44,7 @@ class ChatRoomExampleContext : ObservableObject @Published var chatroomState = ChatroomExampleState.Unstarted @Published var textToSend: String = "msg to send" @Published var sReceivedMessages : String = "" - + @Published var isDownloading : Bool = false var fileFolderUrl : URL? var fileUrl : URL? @@ -52,7 +52,8 @@ class ChatRoomExampleContext : ObservableObject { mChatRoomDelegate.tutorialContext = self mLinphoneCoreDelegate.tutorialContext = self - + mChatMessageDelegate.tutorialContext = self + // Initialize Linphone Core try? mCore = Factory.Instance.createCore(configPath: "", factoryConfigPath: "", systemContext: nil) @@ -177,7 +178,6 @@ class ChatRoomExampleContext : ObservableObject content.subtype = "plain" mChatMessage = try mChatRoom!.createFileTransferMessage(initialContent: content) - mChatMessage!.addDelegate(delegate: self.mChatMessageDelegate) mChatMessage!.send() }catch let error as NSError { print("Unable to create directory",error) @@ -192,6 +192,7 @@ class ChatRoomExampleContext : ObservableObject if (!contentName.isEmpty) { content.filePath = fileFolderUrl!.appendingPathComponent(contentName).path print("Start downloading \(content.name) into \(content.filePath)") + isDownloading = true if (!message.downloadContent(content: content)) { print ("Download of \(contentName) failed") } @@ -236,7 +237,6 @@ class LinphoneCoreDelegate: CoreDelegate { } class LinphoneChatRoomStateTracker: ChatRoomDelegate { - var tutorialContext : ChatRoomExampleContext! func onConferenceJoined(chatRoom: ChatRoom, eventLog: EventLog) { @@ -246,11 +246,12 @@ class LinphoneChatRoomStateTracker: ChatRoomDelegate { } class LinphoneChatMessageTracker: ChatMessageDelegate { + var tutorialContext : ChatRoomExampleContext! + func onMsgStateChanged(message msg: ChatMessage, state: ChatMessage.State) { print("MessageTrace - msg state changed: \(state)\n") + if (state == ChatMessage.State.FileTransferDone && tutorialContext.isDownloading == true) { + tutorialContext.isDownloading = false + } } - - func onFileTransferRecv(message: ChatMessage, content: Content, buffer: Buffer) { - - } } diff --git a/swift/ChatRoomTutorial/ChatRoomTutorial/ContentView.swift b/swift/ChatRoomTutorial/ChatRoomTutorial/ContentView.swift index 62fbb62..b478281 100644 --- a/swift/ChatRoomTutorial/ChatRoomTutorial/ContentView.swift +++ b/swift/ChatRoomTutorial/ChatRoomTutorial/ContentView.swift @@ -18,6 +18,18 @@ func getStateAsString(chatroomState : ChatroomExampleState) -> String } } +struct ActivityIndicator: UIViewRepresentable { + func makeUIView(context: UIViewRepresentableContext) -> UIActivityIndicatorView + { + return UIActivityIndicatorView(style: .medium) + } + + func updateUIView(_ uiView: UIActivityIndicatorView, context: UIViewRepresentableContext) + { + uiView.startAnimating() + } +} + struct ContentView: View { @ObservedObject var tutorialContext : ChatRoomExampleContext @@ -80,6 +92,9 @@ struct ContentView: View { Text(getStateAsString(chatroomState: tutorialContext.chatroomState)) .font(.footnote) .foregroundColor((tutorialContext.chatroomState == ChatroomExampleState.Started) ? Color.green : Color .black) + if (tutorialContext.chatroomState == ChatroomExampleState.Starting) { + ActivityIndicator() + } } } Button(action: { @@ -127,7 +142,6 @@ struct ContentView: View { .frame(width: 120.0, height: 50.0) .background(Color.gray) }.disabled(tutorialContext.chatroomState != ChatroomExampleState.Started) - Button(action: tutorialContext.downloadLastFileMessage) { Text("Download last files \n received") @@ -136,6 +150,9 @@ struct ContentView: View { .frame(width: 150.0, height: 50.0) .background(Color.gray) }.disabled(tutorialContext.mLastFileMessageReceived == nil) + if (tutorialContext.isDownloading) { + ActivityIndicator() + } } } Spacer()