@@ -239,56 +239,6 @@ fun ChatScreenWrapper(
239
239
}
240
240
}
241
241
242
- @Composable
243
- fun LazyListState.OnBottomReached (
244
- thresholdItems : Int = 0,
245
- onBottomReached : () -> Unit
246
- ) {
247
- LaunchedEffect (this ) {
248
- var prevIndex = firstVisibleItemIndex
249
- snapshotFlow { firstVisibleItemIndex }
250
- .distinctUntilChanged()
251
- .collect { index ->
252
- Timber .d(" DROID-2966 OnBottomReached scroll index: $index " )
253
- val isDragging = isScrollInProgress
254
-
255
- // Are we scrolling *toward* the bottom edge?
256
- val scrollingDown = isDragging && prevIndex > index
257
-
258
- // Have we crossed into the threshold zone?
259
- val atBottom = index <= thresholdItems
260
-
261
- if (scrollingDown && atBottom) {
262
- onBottomReached()
263
- }
264
- prevIndex = index
265
- }
266
- }
267
- }
268
-
269
- @Composable
270
- private fun LazyListState.OnTopReached (
271
- thresholdItems : Int = 0,
272
- onTopReached : () -> Unit
273
- ) {
274
- val isReached = remember {
275
- derivedStateOf {
276
- val lastVisibleItem = layoutInfo.visibleItemsInfo.lastOrNull()
277
- if (lastVisibleItem != null ) {
278
- lastVisibleItem.index >= layoutInfo.totalItemsCount - 1 - thresholdItems
279
- } else {
280
- false
281
- }
282
- }
283
- }
284
-
285
- LaunchedEffect (isReached) {
286
- snapshotFlow { isReached.value }
287
- .distinctUntilChanged()
288
- .collect { if (it) onTopReached() }
289
- }
290
- }
291
-
292
242
@Composable
293
243
private fun LazyListState.OnTopReachedSafely (
294
244
thresholdItems : Int = 0,
@@ -303,7 +253,6 @@ private fun LazyListState.OnTopReachedSafely(
303
253
if (lastVisibleItem != null ) {
304
254
val isTop = lastVisibleItem.index >= info.totalItemsCount - 1 - thresholdItems
305
255
if (isTop) {
306
- Timber .d(" DROID-2966 Safe onTopReached triggered" )
307
256
onTopReached()
308
257
}
309
258
}
@@ -325,7 +274,6 @@ private fun LazyListState.OnBottomReachedSafely(
325
274
if (firstVisibleItem != null ) {
326
275
val isBottom = firstVisibleItem.index <= thresholdItems
327
276
if (isBottom) {
328
- Timber .d(" DROID-2966 Safe onBottomReached triggered" )
329
277
onBottomReached()
330
278
}
331
279
}
@@ -369,7 +317,7 @@ fun ChatScreen(
369
317
onScrollToBottomClicked : () -> Unit
370
318
) {
371
319
372
- Timber .d(" DROID-2966 Render called with state" )
320
+ Timber .d(" DROID-2966 Render called with state, number of messages: ${uiMessageState.messages.size} " )
373
321
374
322
var text by rememberSaveable(stateSaver = TextFieldValue .Saver ) {
375
323
mutableStateOf(TextFieldValue ())
@@ -381,15 +329,20 @@ fun ChatScreen(
381
329
382
330
val scope = rememberCoroutineScope()
383
331
332
+ val latestMessages by rememberUpdatedState(uiMessageState.messages)
333
+
334
+ val isPerformingScrollIntent = remember { mutableStateOf(false ) }
335
+
384
336
LaunchedEffect (uiMessageState.intent) {
385
337
when (val intent = uiMessageState.intent) {
386
338
is ChatContainer .Intent .ScrollToMessage -> {
339
+ isPerformingScrollIntent.value = true
387
340
val index = uiMessageState.messages.indexOfFirst {
388
341
it is ChatView .Message && it.id == intent.id
389
342
}
390
343
391
344
if (index >= 0 ) {
392
- Timber .d(" DROID-2966 Waiting for layout to stabilize..." )
345
+ Timber .d(" DROID-2966 Found the scrolling target at index: $index . Waiting for layout to stabilize..." )
393
346
394
347
snapshotFlow { lazyListState.layoutInfo.totalItemsCount }
395
348
.first { it > index }
@@ -409,20 +362,24 @@ fun ChatScreen(
409
362
410
363
val delta = itemCenterFromTop - viewportCenter
411
364
412
- Timber .d(" DROID-2966 Calculated delta for centering (reverseLayout-aware): $delta " )
365
+ Timber .d(" DROID-2966 Found scroll-target item info and Calculated delta for centering (reverseLayout-aware): $delta " )
413
366
414
367
// move negatively because reverseLayout flips
415
- lazyListState.animateScrollBy(delta.toFloat())
368
+ // lazyListState.animateScrollBy(delta.toFloat())
416
369
417
370
Timber .d(" DROID-2966 Scroll complete. Now clearing intent." )
418
371
419
- onClearIntent()
420
372
} else {
421
373
Timber .w(" DROID-2966 Target item not found after scroll!" )
422
374
}
375
+ } else {
376
+ Timber .d(" DROID-2966 Could not found the scrolling target for the intent" )
423
377
}
378
+ // onClearIntent()
379
+ isPerformingScrollIntent.value = false
424
380
}
425
381
is ChatContainer .Intent .ScrollToBottom -> {
382
+ Timber .d(" DROID-2966 COMPOSE scroll to bottom" )
426
383
smoothScrollToBottom2(lazyListState)
427
384
onClearIntent()
428
385
}
@@ -433,32 +390,36 @@ fun ChatScreen(
433
390
}
434
391
}
435
392
436
- val latestMessages by rememberUpdatedState(uiMessageState.messages)
437
-
438
393
// Scrolling to bottom when list size changes and we are at the bottom of the list
439
- LaunchedEffect (latestMessages) {
440
- if (lazyListState.firstVisibleItemScrollOffset == 0 ) {
441
- scope.launch {
442
- lazyListState.animateScrollToItem(0 )
443
- }
444
- }
445
- }
446
-
447
- lazyListState.OnBottomReachedSafely (
448
- thresholdItems = 3
449
- ) {
450
- if (latestMessages.isNotEmpty()) {
451
- onChatScrolledToBottom()
452
- }
453
- }
454
-
455
- lazyListState.OnTopReachedSafely (
456
- thresholdItems = 3
457
- ) {
458
- if (latestMessages.isNotEmpty()) {
459
- onChatScrolledToTop()
460
- }
461
- }
394
+ // LaunchedEffect(latestMessages) {
395
+ // if (lazyListState.firstVisibleItemScrollOffset == 0) {
396
+ // scope.launch {
397
+ // lazyListState.animateScrollToItem(0)
398
+ // }
399
+ // }
400
+ // }
401
+
402
+ // lazyListState.OnBottomReachedSafely(
403
+ // thresholdItems = 3
404
+ // ) {
405
+ // if (!isPerformingScrollIntent.value && latestMessages.isNotEmpty()) {
406
+ // Timber.d("DROID-2966 Safe onBottomReached dispatched from compose to VM")
407
+ // onChatScrolledToBottom()
408
+ // } else {
409
+ // Timber.d("DROID-2966 Safe onBottomReached skipped")
410
+ // }
411
+ // }
412
+ //
413
+ // lazyListState.OnTopReachedSafely(
414
+ // thresholdItems = 3
415
+ // ) {
416
+ // if (!isPerformingScrollIntent.value && latestMessages.isNotEmpty()) {
417
+ // Timber.d("DROID-2966 Safe onTopReached dispatched from compose to VM")
418
+ // onChatScrolledToTop()
419
+ // } else {
420
+ // Timber.d("DROID-2966 Safe onTopReached skipped")
421
+ // }
422
+ // }
462
423
463
424
Column (
464
425
modifier = Modifier .fillMaxSize()
@@ -684,7 +645,7 @@ fun Messages(
684
645
onMentionClicked : (Id ) -> Unit ,
685
646
onScrollToReplyClicked : (Id ) -> Unit ,
686
647
) {
687
- Timber .d(" DROID-2966 Messages composition: ${messages.map { if (it is ChatView .Message ) it.content.msg else it }} " )
648
+ // Timber.d("DROID-2966 Messages composition: ${messages.map { if (it is ChatView.Message) it.content.msg else it }}")
688
649
val scope = rememberCoroutineScope()
689
650
LazyColumn (
690
651
modifier = modifier,
0 commit comments