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
android:name=".MainActivity"
android:exported="true"
android:screenOrientation="portrait"
android:theme="@style/Theme.Jarvisclientapp">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -1,7 +1,6 @@
package ch.broillet.jarvis.android
import android.os.Bundle
import android.provider.Settings
import android.view.WindowManager
import androidx.activity.ComponentActivity
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.ui.theme.JarvisclientappTheme
import ch.broillet.jarvis.android.utils.SocketHandler
import ch.broillet.jarvis.android.utils.getDeviceId
class MainActivity : ComponentActivity() {
@ -36,12 +36,7 @@ class MainActivity : ComponentActivity() {
// The following lines connects the Android app to the server.
SocketHandler.setSocket()
SocketHandler.establishConnection()
SocketHandler.joinRoom(
Settings.Secure.getString(
applicationContext.contentResolver,
Settings.Secure.ANDROID_ID
)
)
SocketHandler.joinRoom(getDeviceId(this))
SocketHandler.getSocket()
.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.mutableStateListOf
import androidx.compose.ui.res.stringResource
import ch.broillet.jarvis.android.R
class ConversationUiState(
initialMessages: List<Message>
@ -13,6 +15,11 @@ class ConversationUiState(
fun addMessage(msg: Message) {
_messages.add(0, msg) // Add to the beginning of the list
}
fun clearMessage(){
_messages.clear()
addMessage(Message(true, "Hello, I'm Jarvis!"))
}
}
@Immutable

View File

@ -5,10 +5,12 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
@ -16,6 +18,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import ch.broillet.jarvis.android.R
import ch.broillet.jarvis.android.ui.theme.productSansFont
import kotlinx.coroutines.delay
@Composable
fun Messages(
@ -23,9 +26,15 @@ fun Messages(
modifier: Modifier = Modifier
) {
Box(modifier = modifier) {
val scrollState = rememberLazyListState()
LaunchedEffect(messages.size) {
delay(50)
scrollState.animateScrollToItem(messages.size)
}
LazyColumn(
modifier = Modifier
.fillMaxSize()
modifier = Modifier.fillMaxSize(),
state = scrollState
) {
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)
Column(Modifier.padding(bottom = 80.dp)) {
MainBase(navController)
MainBase(navController, 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".
@Composable
fun MainBase(navController: NavController) {
fun MainBase(navController: NavController, uiState: ConversationUiState) {
Column(
Modifier
@ -82,7 +82,7 @@ fun MainBase(navController: NavController) {
.fillMaxWidth()
) {
Row(Modifier.align(Alignment.End)) {
DropDownSettingsMenu(navController)
DropDownSettingsMenu(navController, uiState)
}
Text(
@ -95,7 +95,7 @@ fun MainBase(navController: NavController) {
}
@Composable
fun DropDownSettingsMenu(navController: NavController) {
fun DropDownSettingsMenu(navController: NavController, uiState: ConversationUiState) {
var expanded by remember { mutableStateOf(false) }
IconButton(onClick = { expanded = true }) {
@ -112,7 +112,7 @@ fun DropDownSettingsMenu(navController: NavController) {
) {
DropdownMenuItem(
text = { Text(stringResource(id = R.string.main_page_delete_conversation)) },
onClick = { navController.navigate(Screen.PermissionsScreen.route) },
onClick = { SocketHandler.clearMessages(uiState, getDeviceId(navController.context)) },
leadingIcon = {
Icon(
Icons.Outlined.Edit,
@ -219,13 +219,7 @@ fun processMessage(text: String, navController: NavController, uiState: Conversa
uiState.addMessage(Message(false, text))
navController.context.mainExecutor.execute {
SocketHandler.processMessage(
text,
Secure.getString(
navController.context.contentResolver,
Secure.ANDROID_ID
)
)
SocketHandler.processMessage(text, getDeviceId(navController.context))
}
}

View File

@ -53,11 +53,20 @@ object SocketHandler {
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) {
if (data[0].toString().contains("data")) {
val result: JSONObject = data[0] as JSONObject
if(result.getString("data") != "") {
if (result.getString("data") != "") {
uiState.addMessage(Message(true, result.getString("data")))
}
}
@ -67,7 +76,7 @@ object SocketHandler {
fun messageFromUser(data: Array<Any>, uiState: ConversationUiState) {
if (data[0].toString().contains("data")) {
val result: JSONObject = data[0] as JSONObject
if(result.getString("data") != "") {
if (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.unit.dp
@Composable
fun DefaultBox(
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)
}