[Chat][Feature] send read receipt (#557)

This commit is contained in:
Peixin Liu 2022-11-02 20:13:36 -07:00 коммит произвёл GitHub
Родитель 70b4b2d334
Коммит 2bf7bb9827
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 37 добавлений и 3 удалений

Просмотреть файл

@ -12,6 +12,7 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Alignment
@ -41,6 +42,9 @@ internal fun MessageListView(
dispatchers: Dispatch
) {
requestPages(scrollState, messages, dispatchers)
if (messages.isNotEmpty()) {
sendReadReceipt(scrollState, messages, dispatchers)
}
LazyColumn(
modifier = modifier.fillMaxHeight(),
state = scrollState,
@ -77,9 +81,7 @@ private fun requestPages(
dispatch: Dispatch
) {
if (scrollState.layoutInfo.totalItemsCount == 0) return
val currentLastMessage = messages.first()
if (scrollState.outOfViewItemCount() < MESSAGE_LIST_LOAD_MORE_THRESHOLD) {
val lastTrigger = remember { mutableStateOf("0") }
if (lastTrigger.value != currentLastMessage.message.id) {
@ -89,6 +91,25 @@ private fun requestPages(
}
}
@Composable
private fun sendReadReceipt(
scrollState: LazyListState,
messages: List<MessageViewModel>,
dispatch: Dispatch
) {
val firstVisibleItemIndex = scrollState.firstVisibleItemIndex
val currentBottomMessage = messages[messages.count() - firstVisibleItemIndex - 1]
if (!currentBottomMessage.isLocalUser) {
currentBottomMessage.message.id?.let {
if (it.isNotEmpty()) {
LaunchedEffect(it) {
dispatch(ChatAction.MessageRead(it))
}
}
}
}
}
@Preview
@Composable
internal fun PreviewMessageListView() {
@ -98,7 +119,7 @@ internal fun PreviewMessageListView() {
showLoading = false,
modifier = Modifier.padding(0.dp),
messages = MOCK_MESSAGES.toViewModelList(LocalContext.current, MOCK_LOCAL_USER_ID),
scrollState = LazyListState(),
scrollState = LazyListState()
) {}
}
}

Просмотреть файл

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?><!--
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="11dp"
android:height="6dp"
android:viewportWidth="11"
android:viewportHeight="6">
<path
android:pathData="M0.974,3.659C0.887,3.92 0.604,4.062 0.342,3.974C0.08,3.887 -0.062,3.604 0.026,3.342C0.016,3.372 0.026,3.341 0.026,3.341C0.031,3.324 0.037,3.308 0.043,3.292C0.054,3.264 0.068,3.226 0.088,3.179C0.127,3.084 0.187,2.953 0.269,2.798C0.432,2.488 0.69,2.075 1.069,1.662C1.834,0.827 3.087,0 5,0C6.913,0 8.166,0.827 8.931,1.662C9.31,2.075 9.567,2.488 9.731,2.798C9.813,2.953 9.872,3.084 9.912,3.179C9.931,3.226 9.946,3.264 9.956,3.292C9.959,3.3 9.97,3.352 9.98,3.403C9.989,3.448 9.997,3.492 10,3.5C10,3.5 10.083,3.833 9.658,3.974C9.397,4.061 9.114,3.921 9.026,3.66L9.026,3.659L9.025,3.657L9.019,3.641C9.013,3.624 9.003,3.598 8.989,3.563C8.96,3.494 8.913,3.39 8.847,3.265C8.714,3.012 8.503,2.675 8.194,2.338C7.584,1.673 6.587,1 5,1C3.413,1 2.416,1.673 1.806,2.338C1.497,2.675 1.286,3.012 1.153,3.265C1.087,3.39 1.04,3.494 1.011,3.563C0.997,3.598 0.987,3.624 0.981,3.641L0.974,3.659ZM5,2C3.895,2 3,2.895 3,4C3,5.105 3.895,6 5,6C6.105,6 7,5.105 7,4C7,2.895 6.105,2 5,2ZM4,4C4,3.448 4.448,3 5,3C5.552,3 6,3.448 6,4C6,4.552 5.552,5 5,5C4.448,5 4,4.552 4,4Z"
android:fillColor="#0078D4"/>
</vector>