From 9aa03fc03fa169da235b3176feec4af9f9d0bbdd Mon Sep 17 00:00:00 2001 From: Mathieu B Date: Sun, 4 Dec 2022 15:29:57 +0100 Subject: [PATCH] added message_from_user socket --- .../java/ch/broillet/jarvis/android/MainActivity.kt | 2 ++ .../java/ch/broillet/jarvis/android/pages/MainPage.kt | 4 ++-- .../ch/broillet/jarvis/android/utils/SocketHandler.kt | 10 +++++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/ch/broillet/jarvis/android/MainActivity.kt b/app/src/main/java/ch/broillet/jarvis/android/MainActivity.kt index 352d921..9b79e34 100644 --- a/app/src/main/java/ch/broillet/jarvis/android/MainActivity.kt +++ b/app/src/main/java/ch/broillet/jarvis/android/MainActivity.kt @@ -45,6 +45,8 @@ class MainActivity : ComponentActivity() { SocketHandler.getSocket() .on("message_from_jarvis") { SocketHandler.messageFromJarvis(it, uiState) } + SocketHandler.getSocket() + .on("message_from_user") { SocketHandler.messageFromUser(it, uiState) } super.onCreate(savedInstanceState) diff --git a/app/src/main/java/ch/broillet/jarvis/android/pages/MainPage.kt b/app/src/main/java/ch/broillet/jarvis/android/pages/MainPage.kt index 9906152..4140481 100644 --- a/app/src/main/java/ch/broillet/jarvis/android/pages/MainPage.kt +++ b/app/src/main/java/ch/broillet/jarvis/android/pages/MainPage.kt @@ -208,6 +208,8 @@ fun processAudio(audioFile: File): String { } fun processMessage(text: String, navController: NavController, uiState: ConversationUiState) { + uiState.addMessage(Message(false, text)) + navController.context.mainExecutor.execute { SocketHandler.processMessage( text, @@ -217,8 +219,6 @@ fun processMessage(text: String, navController: NavController, uiState: Conversa ) ) } - - uiState.addMessage(Message(false, text)) } diff --git a/app/src/main/java/ch/broillet/jarvis/android/utils/SocketHandler.kt b/app/src/main/java/ch/broillet/jarvis/android/utils/SocketHandler.kt index 5bf4399..d3e03ab 100644 --- a/app/src/main/java/ch/broillet/jarvis/android/utils/SocketHandler.kt +++ b/app/src/main/java/ch/broillet/jarvis/android/utils/SocketHandler.kt @@ -57,4 +57,12 @@ object SocketHandler { } } -} \ No newline at end of file + + fun messageFromUser(data: Array, uiState: ConversationUiState) { + if (data[0].toString().contains("data")) { + val result: JSONObject = data[0] as JSONObject + uiState.addMessage(Message(false, result.getString("data"))) + } + + } +}