force portait, getID, empty messages, auto-scroll, clear chat

This commit is contained in:
Mathieu B 2023-03-25 15:51:30 +01:00
parent eb5c8b905f
commit 75736c70da
7 changed files with 42 additions and 24 deletions

View File

@ -20,6 +20,7 @@
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:exported="true" android:exported="true"
android:screenOrientation="portrait"
android:theme="@style/Theme.Jarvisclientapp"> android:theme="@style/Theme.Jarvisclientapp">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />

View File

@ -1,7 +1,6 @@
package ch.broillet.jarvis.android package ch.broillet.jarvis.android
import android.os.Bundle import android.os.Bundle
import android.provider.Settings
import android.view.WindowManager import android.view.WindowManager
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
@ -15,6 +14,7 @@ import ch.broillet.jarvis.android.chat.Message
import ch.broillet.jarvis.android.nav.Navigation import ch.broillet.jarvis.android.nav.Navigation
import ch.broillet.jarvis.android.ui.theme.JarvisclientappTheme import ch.broillet.jarvis.android.ui.theme.JarvisclientappTheme
import ch.broillet.jarvis.android.utils.SocketHandler import ch.broillet.jarvis.android.utils.SocketHandler
import ch.broillet.jarvis.android.utils.getDeviceId
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
@ -36,12 +36,7 @@ class MainActivity : ComponentActivity() {
// The following lines connects the Android app to the server. // The following lines connects the Android app to the server.
SocketHandler.setSocket() SocketHandler.setSocket()
SocketHandler.establishConnection() SocketHandler.establishConnection()
SocketHandler.joinRoom( SocketHandler.joinRoom(getDeviceId(this))
Settings.Secure.getString(
applicationContext.contentResolver,
Settings.Secure.ANDROID_ID
)
)
SocketHandler.getSocket() SocketHandler.getSocket()
.on("message_from_jarvis") { SocketHandler.messageFromJarvis(it, uiState) } .on("message_from_jarvis") { SocketHandler.messageFromJarvis(it, uiState) }

View File

@ -2,6 +2,8 @@ package ch.broillet.jarvis.android.chat
import androidx.compose.runtime.Immutable import androidx.compose.runtime.Immutable
import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateListOf
import androidx.compose.ui.res.stringResource
import ch.broillet.jarvis.android.R
class ConversationUiState( class ConversationUiState(
initialMessages: List<Message> initialMessages: List<Message>
@ -13,6 +15,11 @@ class ConversationUiState(
fun addMessage(msg: Message) { fun addMessage(msg: Message) {
_messages.add(0, msg) // Add to the beginning of the list _messages.add(0, msg) // Add to the beginning of the list
} }
fun clearMessage(){
_messages.clear()
addMessage(Message(true, "Hello, I'm Jarvis!"))
}
} }
@Immutable @Immutable

View File

@ -5,10 +5,12 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
@ -16,6 +18,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import ch.broillet.jarvis.android.R import ch.broillet.jarvis.android.R
import ch.broillet.jarvis.android.ui.theme.productSansFont import ch.broillet.jarvis.android.ui.theme.productSansFont
import kotlinx.coroutines.delay
@Composable @Composable
fun Messages( fun Messages(
@ -23,9 +26,15 @@ fun Messages(
modifier: Modifier = Modifier modifier: Modifier = Modifier
) { ) {
Box(modifier = modifier) { Box(modifier = modifier) {
val scrollState = rememberLazyListState()
LaunchedEffect(messages.size) {
delay(50)
scrollState.animateScrollToItem(messages.size)
}
LazyColumn( LazyColumn(
modifier = Modifier modifier = Modifier.fillMaxSize(),
.fillMaxSize() state = scrollState
) { ) {
val reverse: List<Message> = messages.reversed() val reverse: List<Message> = messages.reversed()

View File

@ -48,7 +48,7 @@ fun DisplayMainPage(
// This column regroup the base and all the conversations (everything except the footer) // This column regroup the base and all the conversations (everything except the footer)
Column(Modifier.padding(bottom = 80.dp)) { Column(Modifier.padding(bottom = 80.dp)) {
MainBase(navController) MainBase(navController, uiState)
Messages( Messages(
messages = uiState.messages, messages = uiState.messages,
@ -74,7 +74,7 @@ fun DisplayMainPage(
//Draws the base of the main activity, that includes the 3-dots menu and the "hi text". //Draws the base of the main activity, that includes the 3-dots menu and the "hi text".
@Composable @Composable
fun MainBase(navController: NavController) { fun MainBase(navController: NavController, uiState: ConversationUiState) {
Column( Column(
Modifier Modifier
@ -82,7 +82,7 @@ fun MainBase(navController: NavController) {
.fillMaxWidth() .fillMaxWidth()
) { ) {
Row(Modifier.align(Alignment.End)) { Row(Modifier.align(Alignment.End)) {
DropDownSettingsMenu(navController) DropDownSettingsMenu(navController, uiState)
} }
Text( Text(
@ -95,7 +95,7 @@ fun MainBase(navController: NavController) {
} }
@Composable @Composable
fun DropDownSettingsMenu(navController: NavController) { fun DropDownSettingsMenu(navController: NavController, uiState: ConversationUiState) {
var expanded by remember { mutableStateOf(false) } var expanded by remember { mutableStateOf(false) }
IconButton(onClick = { expanded = true }) { IconButton(onClick = { expanded = true }) {
@ -112,7 +112,7 @@ fun DropDownSettingsMenu(navController: NavController) {
) { ) {
DropdownMenuItem( DropdownMenuItem(
text = { Text(stringResource(id = R.string.main_page_delete_conversation)) }, text = { Text(stringResource(id = R.string.main_page_delete_conversation)) },
onClick = { navController.navigate(Screen.PermissionsScreen.route) }, onClick = { SocketHandler.clearMessages(uiState, getDeviceId(navController.context)) },
leadingIcon = { leadingIcon = {
Icon( Icon(
Icons.Outlined.Edit, Icons.Outlined.Edit,
@ -219,13 +219,7 @@ fun processMessage(text: String, navController: NavController, uiState: Conversa
uiState.addMessage(Message(false, text)) uiState.addMessage(Message(false, text))
navController.context.mainExecutor.execute { navController.context.mainExecutor.execute {
SocketHandler.processMessage( SocketHandler.processMessage(text, getDeviceId(navController.context))
text,
Secure.getString(
navController.context.contentResolver,
Secure.ANDROID_ID
)
)
} }
} }

View File

@ -53,11 +53,20 @@ object SocketHandler {
getSocket().emit("join", body.toString()) getSocket().emit("join", body.toString())
} }
@Synchronized
fun clearMessages(uiState: ConversationUiState, uuid: String) {
val body = JSONObject()
body.put("uuid", uuid)
getSocket().emit("clear_chat", body.toString())
uiState.clearMessage()
}
fun messageFromJarvis(data: Array<Any>, uiState: ConversationUiState) { fun messageFromJarvis(data: Array<Any>, uiState: ConversationUiState) {
if (data[0].toString().contains("data")) { if (data[0].toString().contains("data")) {
val result: JSONObject = data[0] as JSONObject val result: JSONObject = data[0] as JSONObject
if(result.getString("data") != "") { if (result.getString("data") != "") {
uiState.addMessage(Message(true, result.getString("data"))) uiState.addMessage(Message(true, result.getString("data")))
} }
} }
@ -67,7 +76,7 @@ object SocketHandler {
fun messageFromUser(data: Array<Any>, uiState: ConversationUiState) { fun messageFromUser(data: Array<Any>, uiState: ConversationUiState) {
if (data[0].toString().contains("data")) { if (data[0].toString().contains("data")) {
val result: JSONObject = data[0] as JSONObject val result: JSONObject = data[0] as JSONObject
if(result.getString("data") != "") { if (result.getString("data") != "") {
uiState.addMessage(Message(false, result.getString("data"))) uiState.addMessage(Message(false, result.getString("data")))
} }
} }

View File

@ -9,7 +9,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@Composable @Composable
fun DefaultBox( fun DefaultBox(
content: @Composable() (BoxScope.() -> Unit) content: @Composable() (BoxScope.() -> Unit)
@ -36,3 +35,7 @@ fun openAppSettings(context: Context) {
) )
) )
} }
fun getDeviceId(context: Context): String {
return Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
}