How can I disable compose reloading in Kotlin?

178 Views Asked by At

I am trying to make a chat bot app and I have a lazycolumn and lazycolumn's item. The bot and the user are talking and the dialogs are displayed with lazycolumn on the screen. Here is a message and there is a textfield under the message that the user can enter, if the user logs in, the textfield should disappear. After the user logs in, the textfield disappears, but when the screen goes down, the top textfield appears on the screen again, why?

hear is my code :

var index = 1
@Composable
fun FirstScreen() {

    val list = remember { mutableStateListOf<Message>() }

    val botList = listOf("Peter", "Francesca", "Luigi", "Igor")
    val random = (0..3).random()
    val botName: String = botList[random]

    val hashMap: HashMap<String, String> = HashMap<String, String>()

    val listState = rememberLazyListState()

    customBotMessage(message = Message1, list)

    Column(
        modifier = Modifier
            .fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Image(
            painter = painterResource(id = R.drawable.diyetkolik_logo),
            contentDescription = "logo",
            modifier = Modifier.padding(10.dp)
        )
        Divider()

        LazyColumn {

            items(list.size) { i ->
                Row(
                    //modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement =
                    if (list[i].id == RECEIVE_ID)
                        Arrangement.Start
                    else
                        Arrangement.End
                ) {
                    if (list[i].id == RECEIVE_ID) {
                        Item(message = list[i], botName, botcolor, list, true, hashMap)
                    } else {
                        Item(message = list[i], "user", usercolor, list, false, hashMap)

                    }
                }
            }
        }
    }
}



private fun customBotMessage(message: String, list: SnapshotStateList<Message>) {

    GlobalScope.launch {
        delay(1000)
        withContext(Dispatchers.Main) {
            list.add(
                Message(
                    message,
                    RECEIVE_ID,
                    Timestamp(System.currentTimeMillis()).toString()
                )
            )
        }
    }
}

Item

@Composable
fun Item(
    message: Message,
    person: String,
    color: Color,
    list: SnapshotStateList<Message>,
    simpleTextFlag: Boolean,
    hashMap:HashMap<String,String>,
) {

    Column() {

        Card(
            modifier = Modifier
                .padding(10.dp),
            backgroundColor = color,
            elevation = 10.dp
        ) {
            Column(
                modifier = Modifier
                    .padding(10.dp)
            ) {
                Row(
                    horizontalArrangement = Arrangement.SpaceEvenly,
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier.padding(4.dp)
                ) {
                    Text(
                        buildAnnotatedString {
                            withStyle(
                                style = SpanStyle(
                                    fontWeight = FontWeight.Medium,
                                    color = Color.Black
                                )
                            ) {
                                append("$person: ")
                            }
                        },
                        modifier = Modifier
                            .padding(4.dp)
                    )
                    Text(
                        buildAnnotatedString {
                            withStyle(
                                style = SpanStyle(
                                    fontWeight = FontWeight.W900,
                                    color = Color.White//Color(/*0xFF4552B8*/)
                                )
                            )
                            {
                                append(message.message)
                            }
                        }
                    )
                    Spacer(modifier = Modifier.padding(3.dp))
                }

            }

        }
       if(simpleTextFlag)
           simpleText(list = list,hashMap)
    }

}

@OptIn(ExperimentalComposeUiApi::class, ExperimentalFoundationApi::class)
@Composable
fun simpleText(list: SnapshotStateList<Message>,hashMap: HashMap<String,String>) {

    val coroutineScope = rememberCoroutineScope()
    val keyboardController = LocalSoftwareKeyboardController.current
    val bringIntoViewRequester = remember { BringIntoViewRequester() }
    var visible by remember { mutableStateOf(true) }

    var text by remember { mutableStateOf("") }

    AnimatedVisibility(
        visible = visible,
        enter = fadeIn() + slideInHorizontally(),
        exit = fadeOut() + slideOutHorizontally()
    ) {
   Row(
        verticalAlignment = Alignment.Bottom,
        horizontalArrangement = Arrangement.Center,
        modifier = Modifier.bringIntoViewRequester(bringIntoViewRequester)
    ) {
            OutlinedTextField(
                modifier = Modifier
                    .padding(8.dp)
                    .onFocusEvent { focusState ->
                        if (focusState.isFocused) {
                            coroutineScope.launch {
                                bringIntoViewRequester.bringIntoView()
                            }
                        }
                    },
                keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
                keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() }),
                value = text,
                onValueChange = { text = it },
                shape = RoundedCornerShape(12.dp),
                label = { Text("send message") })
            IconButton(onClick = {
                if (text.isNotEmpty()) {
                    //Adds it to our local list
                    list.add(
                        Message(
                            text,
                            Constants.SEND_ID,
                            Timestamp(System.currentTimeMillis()).toString()
                        )
                    )
                    hashMap.put(Messages.listOfMessageKeys[index - 1], text)
                    customBotMessage(Messages.listOfMessages[index], list)
                    index += 1
                    visible = false
                }
                text = ""


            }) {
                Icon(
                    modifier = Modifier.padding(8.dp),
                    painter = painterResource(id = R.drawable.ic_baseline_send_24),
                    contentDescription = "send message img"
                )
            }
        }

    }
}

private fun customBotMessage(message: String, list: SnapshotStateList<Message>) {

    GlobalScope.launch {
        delay(1000)
        withContext(Dispatchers.Main) {
            list.add(
                Message(
                    message,
                    Constants.RECEIVE_ID,
                    Timestamp(System.currentTimeMillis()).toString()
                )
            )
        }
    }
}
0

There are 0 best solutions below