|
@@ -2,13 +2,20 @@ package io.nexilis.alpha.ui.main
|
|
|
|
|
|
import androidx.compose.foundation.layout.*
|
|
|
import androidx.compose.foundation.lazy.LazyColumn
|
|
|
+import androidx.compose.foundation.lazy.LazyListState
|
|
|
import androidx.compose.foundation.lazy.items
|
|
|
import androidx.compose.foundation.lazy.rememberLazyListState
|
|
|
+import androidx.compose.foundation.shape.CircleShape
|
|
|
+import androidx.compose.material.icons.Icons
|
|
|
+import androidx.compose.material.icons.filled.KeyboardArrowDown
|
|
|
import androidx.compose.material3.*
|
|
|
import androidx.compose.runtime.*
|
|
|
import androidx.compose.runtime.livedata.observeAsState
|
|
|
import androidx.compose.ui.Modifier
|
|
|
import androidx.compose.ui.graphics.Color
|
|
|
+import androidx.compose.ui.unit.dp
|
|
|
+import androidx.constraintlayout.compose.ConstraintLayout
|
|
|
+import androidx.constraintlayout.compose.Dimension
|
|
|
import androidx.hilt.navigation.compose.hiltViewModel
|
|
|
import androidx.navigation.NavHostController
|
|
|
import io.nexilis.alpha.ui.components.ContentChat
|
|
@@ -17,10 +24,12 @@ import io.nexilis.alpha.ui.components.LeftBubbleChat
|
|
|
import io.nexilis.alpha.ui.components.RightBubbleChat
|
|
|
import io.nexilis.service.data.entities.Buddy
|
|
|
import io.nexilis.service.data.viewmodels.MessageViewModel
|
|
|
+import kotlinx.coroutines.launch
|
|
|
|
|
|
@OptIn(ExperimentalLayoutApi::class)
|
|
|
@Composable
|
|
|
fun Chat(
|
|
|
+ modifier: Modifier = Modifier,
|
|
|
navController: NavHostController,
|
|
|
pin: String,
|
|
|
me: Buddy
|
|
@@ -28,21 +37,24 @@ fun Chat(
|
|
|
val messageModel: MessageViewModel = hiltViewModel()
|
|
|
val messages by messageModel.getOpposite(pin).observeAsState()
|
|
|
val listState = rememberLazyListState()
|
|
|
+ val scope = rememberCoroutineScope()
|
|
|
messages?.let {
|
|
|
LaunchedEffect(key1 = it.size) {
|
|
|
if (it.isNotEmpty())
|
|
|
listState.scrollToItem(it.size - 1)
|
|
|
}
|
|
|
}
|
|
|
- Column(
|
|
|
- modifier = Modifier
|
|
|
- .fillMaxSize()
|
|
|
- .imePadding()
|
|
|
-// .imeNestedScroll()
|
|
|
- ) {
|
|
|
+ ConstraintLayout(modifier = modifier
|
|
|
+ .imePadding()
|
|
|
+ .imeNestedScroll()) {
|
|
|
+ val (list, fab, input) = createRefs()
|
|
|
LazyColumn(
|
|
|
modifier = Modifier
|
|
|
- .weight(1f),
|
|
|
+ .constrainAs(list) {
|
|
|
+ top.linkTo(parent.top)
|
|
|
+ bottom.linkTo(input.top)
|
|
|
+ height = Dimension.fillToConstraints
|
|
|
+ },
|
|
|
state = listState,
|
|
|
reverseLayout = false
|
|
|
) {
|
|
@@ -79,40 +91,42 @@ fun Chat(
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-// ConstraintLayout {
|
|
|
-// val (fab, input) = createRefs()
|
|
|
-// val showButton by remember {
|
|
|
-// derivedStateOf {
|
|
|
-// listState.firstVisibleItemIndex > 0
|
|
|
-// }
|
|
|
-// }
|
|
|
-// if (showButton) {
|
|
|
-// Button(
|
|
|
-// onClick = {
|
|
|
-// scope.launch { listState.scrollToItem(0) }
|
|
|
-// },
|
|
|
-// modifier = Modifier
|
|
|
-// .constrainAs(fab) {
|
|
|
-// end.linkTo(parent.end)
|
|
|
-// bottom.linkTo(input.top)
|
|
|
-// }
|
|
|
-// .padding(end = 6.dp, bottom = 6.dp)
|
|
|
-// .size(32.dp),
|
|
|
-// shape = CircleShape,
|
|
|
-// contentPadding = PaddingValues(0.dp)
|
|
|
-// ) {
|
|
|
-// Icon(imageVector = Icons.Default.KeyboardArrowDown, contentDescription = "")
|
|
|
-// }
|
|
|
-// }
|
|
|
+ val showButton by remember {
|
|
|
+ derivedStateOf {
|
|
|
+ !listState.isScrolledToTheEnd()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (showButton) {
|
|
|
+ Button(
|
|
|
+ onClick = {
|
|
|
+ messages?.let {
|
|
|
+ if (it.isNotEmpty())
|
|
|
+ scope.launch { listState.scrollToItem(it.size - 1) }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ modifier = Modifier
|
|
|
+ .constrainAs(fab) {
|
|
|
+ end.linkTo(parent.end)
|
|
|
+ bottom.linkTo(input.top)
|
|
|
+ }
|
|
|
+ .padding(end = 6.dp, bottom = 6.dp)
|
|
|
+ .size(32.dp),
|
|
|
+ shape = CircleShape,
|
|
|
+ contentPadding = PaddingValues(0.dp)
|
|
|
+ ) {
|
|
|
+ Icon(imageVector = Icons.Default.KeyboardArrowDown, contentDescription = "")
|
|
|
+ }
|
|
|
+ }
|
|
|
InputChat(
|
|
|
-// modifier = Modifier.constrainAs(input) {
|
|
|
-// bottom.linkTo(parent.bottom)
|
|
|
-// },
|
|
|
+ modifier = Modifier.constrainAs(input) {
|
|
|
+ bottom.linkTo(parent.bottom)
|
|
|
+ },
|
|
|
navController = navController,
|
|
|
pin = pin,
|
|
|
me = me
|
|
|
)
|
|
|
-// }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+fun LazyListState.isScrolledToTheEnd() =
|
|
|
+ layoutInfo.visibleItemsInfo.lastOrNull()?.index == layoutInfo.totalItemsCount - 1
|