@@ -1145,7 +1145,7 @@ protected void NotifyMessageSliceLoaded()
1145
1145
MessageSliceLoaded = null ;
1146
1146
}
1147
1147
1148
- public async Task LoadMessageSliceAsync ( long ? previousId , long maxId , VerticalAlignment alignment = VerticalAlignment . Center , double ? pixel = null , ScrollIntoViewAlignment ? direction = null , bool ? disableAnimation = null , TextQuote highlight = null , int checklistTaskId = 0 , bool onlyRemote = false )
1148
+ public async Task LoadMessageSliceAsync ( long ? previousId , long fromMessageId , VerticalAlignment alignment = VerticalAlignment . Center , double ? pixel = null , ScrollIntoViewAlignment ? direction = null , bool ? disableAnimation = null , TextQuote highlight = null , int checklistTaskId = 0 , int fromDateOffset = 0 , bool onlyRemote = false )
1149
1149
{
1150
1150
if ( Type is not DialogType . History and not DialogType . Thread and not DialogType . Pinned )
1151
1151
{
@@ -1166,15 +1166,15 @@ public async Task LoadMessageSliceAsync(long? previousId, long maxId, VerticalAl
1166
1166
1167
1167
if ( direction == null && TryGetFirstVisibleMessageId ( out long firstVisibleId ) )
1168
1168
{
1169
- direction = firstVisibleId < maxId ? ScrollIntoViewAlignment . Default : ScrollIntoViewAlignment . Leading ;
1169
+ direction = firstVisibleId < fromMessageId ? ScrollIntoViewAlignment . Default : ScrollIntoViewAlignment . Leading ;
1170
1170
}
1171
1171
1172
1172
if ( alignment == VerticalAlignment . Bottom && pixel == null )
1173
1173
{
1174
1174
pixel = int . MaxValue ;
1175
1175
}
1176
1176
1177
- if ( onlyRemote is false && Items . TryGetValue ( maxId , out MessageViewModel already ) )
1177
+ if ( onlyRemote is false && Items . TryGetValue ( fromMessageId , out MessageViewModel already ) )
1178
1178
{
1179
1179
if ( alignment == VerticalAlignment . Center )
1180
1180
{
@@ -1199,14 +1199,14 @@ public async Task LoadMessageSliceAsync(long? previousId, long maxId, VerticalAl
1199
1199
1200
1200
// If we're loading the last message and it has been read already
1201
1201
// then we want to align it at bottom, as it might be taller than the window height
1202
- if ( maxId == details . LastMessageId )
1202
+ if ( fromMessageId == details . LastMessageId )
1203
1203
{
1204
1204
alignment = VerticalAlignment . Bottom ;
1205
1205
pixel = null ;
1206
1206
}
1207
1207
}
1208
1208
1209
- HistoryField ? . ScrollToItem ( already , alignment , alignment == VerticalAlignment . Center ? new MessageBubbleHighlightOptions ( maxId , highlight , checklistTaskId ) : null , pixel , direction ?? ScrollIntoViewAlignment . Leading , disableAnimation ) ;
1209
+ HistoryField ? . ScrollToItem ( already , alignment , alignment == VerticalAlignment . Center ? new MessageBubbleHighlightOptions ( fromMessageId , highlight , checklistTaskId ) : null , pixel , direction ?? ScrollIntoViewAlignment . Leading , disableAnimation ) ;
1210
1210
1211
1211
if ( previousId . HasValue && ! _repliesStack . Contains ( previousId . Value ) )
1212
1212
{
@@ -1234,12 +1234,12 @@ public async Task LoadMessageSliceAsync(long? previousId, long maxId, VerticalAl
1234
1234
1235
1235
System . Diagnostics . Debug . WriteLine ( "DialogViewModel: LoadMessageSliceAsync" ) ;
1236
1236
1237
- var response = await Task . Run ( ( ) => LoadMessageSliceImpl ( chat , maxId , alignment , direction , pixel ) ) ;
1237
+ var response = await Task . Run ( ( ) => LoadMessageSliceImpl ( chat , fromMessageId , fromDateOffset , alignment , direction , pixel ) ) ;
1238
1238
if ( response is LoadSliceResult slice )
1239
1239
{
1240
1240
_groupedMessages . Clear ( ) ;
1241
1241
1242
- maxId = slice . FromMessageId ;
1242
+ fromMessageId = slice . FromMessageId ;
1243
1243
pixel = slice . Pixel ;
1244
1244
alignment = slice . Alignment ;
1245
1245
@@ -1272,9 +1272,9 @@ public async Task LoadMessageSliceAsync(long? previousId, long maxId, VerticalAl
1272
1272
IsOldestSliceLoaded = null ;
1273
1273
IsNewestSliceLoaded = endReached ;
1274
1274
1275
- if ( Items . TryGetValue ( maxId , out already ) )
1275
+ if ( Items . TryGetValue ( fromMessageId , out already ) )
1276
1276
{
1277
- HistoryField ? . ScrollToItem ( already , alignment , alignment == VerticalAlignment . Center ? new MessageBubbleHighlightOptions ( maxId , highlight , checklistTaskId ) : null , pixel , direction ?? ScrollIntoViewAlignment . Leading , disableAnimation ) ;
1277
+ HistoryField ? . ScrollToItem ( already , alignment , alignment == VerticalAlignment . Center ? new MessageBubbleHighlightOptions ( fromMessageId , highlight , checklistTaskId ) : null , pixel , direction ?? ScrollIntoViewAlignment . Leading , disableAnimation ) ;
1278
1278
1279
1279
if ( previousId . HasValue && ! _repliesStack . Contains ( previousId . Value ) )
1280
1280
{
@@ -1319,7 +1319,7 @@ public async Task LoadMessageSliceAsync(long? previousId, long maxId, VerticalAl
1319
1319
_loadingSlice = false ;
1320
1320
IsLoading = false ;
1321
1321
1322
- PinnedMessages . LoadSlice ( maxId ) ;
1322
+ PinnedMessages . LoadSlice ( fromMessageId ) ;
1323
1323
}
1324
1324
1325
1325
if ( loadMore != PanelScrollingDirection . None )
@@ -1377,39 +1377,39 @@ private async Task<Messages> PreloadAlbumsAsync(long chatId, FoundChatMessages f
1377
1377
return new Messages ( foundChatMessages . TotalCount , foundChatMessages . Messages ) ;
1378
1378
}
1379
1379
1380
- private async Task < LoadSliceResult > LoadMessageSliceImpl ( Chat chat , long maxId , VerticalAlignment alignment , ScrollIntoViewAlignment ? direction , double ? pixel )
1380
+ private async Task < LoadSliceResult > LoadMessageSliceImpl ( Chat chat , long fromMessageId , int fromDateOffset , VerticalAlignment alignment , ScrollIntoViewAlignment ? direction , double ? pixel )
1381
1381
{
1382
1382
Task < Object > func ;
1383
1383
if ( Type == DialogType . Pinned )
1384
1384
{
1385
- func = ClientService . SendAsync ( new SearchChatMessages ( chat . Id , Topic , string . Empty , null , maxId , - 25 , 50 , new SearchMessagesFilterPinned ( ) ) ) ;
1385
+ func = ClientService . SendAsync ( new SearchChatMessages ( chat . Id , Topic , string . Empty , null , fromMessageId , - 25 , 50 , new SearchMessagesFilterPinned ( ) ) ) ;
1386
1386
}
1387
1387
else if ( Search ? . SavedMessagesTag != null && Search . FilterByTag )
1388
1388
{
1389
- func = ClientService . SendAsync ( new SearchSavedMessages ( SavedMessagesTopicId , Search . SavedMessagesTag , string . Empty , maxId , - 25 , 50 ) ) ;
1389
+ func = ClientService . SendAsync ( new SearchSavedMessages ( SavedMessagesTopicId , Search . SavedMessagesTag , string . Empty , fromMessageId , - 25 , 50 ) ) ;
1390
1390
}
1391
1391
else if ( SavedMessagesTopic != null )
1392
1392
{
1393
- func = ClientService . SendAsync ( new GetSavedMessagesTopicHistory ( SavedMessagesTopic . Id , maxId , - 25 , 50 ) ) ;
1393
+ func = ClientService . SendAsync ( new GetSavedMessagesTopicHistory ( SavedMessagesTopic . Id , fromMessageId , - 25 , 50 ) ) ;
1394
1394
}
1395
1395
else if ( DirectMessagesChatTopic != null )
1396
1396
{
1397
- func = ClientService . SendAsync ( new GetDirectMessagesChatTopicHistory ( chat . Id , DirectMessagesChatTopic . Id , maxId , - 25 , 50 ) ) ;
1397
+ func = ClientService . SendAsync ( new GetDirectMessagesChatTopicHistory ( chat . Id , DirectMessagesChatTopic . Id , fromMessageId , - 25 , 50 ) ) ;
1398
1398
}
1399
1399
else if ( ForumTopic != null )
1400
1400
{
1401
- func = ClientService . SendAsync ( new GetMessageThreadHistory ( chat . Id , _forumTopic . Info . MessageThreadId , maxId , - 25 , 50 ) ) ;
1401
+ func = ClientService . SendAsync ( new GetMessageThreadHistory ( chat . Id , _forumTopic . Info . MessageThreadId , fromMessageId , - 25 , 50 ) ) ;
1402
1402
}
1403
1403
else if ( Thread != null )
1404
1404
{
1405
1405
// MaxId == 0 means that the thread was never opened
1406
- if ( maxId == 0 || Thread . Messages . Any ( x => x . Id == maxId ) )
1406
+ if ( fromMessageId == 0 || Thread . Messages . Any ( x => x . Id == fromMessageId ) )
1407
1407
{
1408
1408
func = ClientService . SendAsync ( new GetMessageThreadHistory ( chat . Id , _thread . MessageThreadId , 1 , - 25 , 50 ) ) ;
1409
1409
}
1410
1410
else
1411
1411
{
1412
- func = ClientService . SendAsync ( new GetMessageThreadHistory ( chat . Id , _thread . MessageThreadId , maxId , - 25 , 50 ) ) ;
1412
+ func = ClientService . SendAsync ( new GetMessageThreadHistory ( chat . Id , _thread . MessageThreadId , fromMessageId , - 25 , 50 ) ) ;
1413
1413
}
1414
1414
}
1415
1415
else
@@ -1447,7 +1447,7 @@ async Task<Object> GetChatHistoryAsync(long chatId, long fromMessageId, int offs
1447
1447
return response ;
1448
1448
}
1449
1449
1450
- func = GetChatHistoryAsync ( chat . Id , maxId , - 25 , 50 , alignment == VerticalAlignment . Top ) ;
1450
+ func = GetChatHistoryAsync ( chat . Id , fromMessageId , - 25 , 50 , alignment == VerticalAlignment . Top ) ;
1451
1451
}
1452
1452
1453
1453
if ( alignment != VerticalAlignment . Center )
@@ -1486,7 +1486,7 @@ bool Included(long id)
1486
1486
1487
1487
// If we're loading from the last read message
1488
1488
// then we want to skip it to align first unread message at top
1489
- if ( details . LastReadInboxMessageId != 0 && details . LastReadInboxMessageId != details . LastMessageId && Included ( maxId ) && Included ( details . LastReadInboxMessageId ) /*maxId >= lastReadMessageId*/ )
1489
+ if ( details . LastReadInboxMessageId != 0 && details . LastReadInboxMessageId != details . LastMessageId && Included ( fromMessageId ) && Included ( details . LastReadInboxMessageId ) /*maxId >= lastReadMessageId*/ )
1490
1490
{
1491
1491
var target = default ( Message ) ;
1492
1492
var index = - 1 ;
@@ -1527,27 +1527,27 @@ bool Included(long id)
1527
1527
messages . MessagesValue . Insert ( index + 1 , new Message ( 0 , target . SenderId , target . ChatId , null , null , target . IsOutgoing , false , false , false , false , target . IsChannelPost , false , false , false , target . Date , 0 , null , null , null , null , null , null , null , target . MessageThreadId , target . TopicId , null , 0 , 0 , 0 , 0 , 0 , 0 , string . Empty , 0 , 0 , false , string . Empty , new MessageHeaderUnread ( ) , null ) ) ;
1528
1528
unread = true ;
1529
1529
}
1530
- else if ( maxId == details . LastReadInboxMessageId )
1530
+ else if ( fromMessageId == details . LastReadInboxMessageId )
1531
1531
{
1532
1532
Logger . Debug ( "Looking for first unread message, can't find it" ) ;
1533
1533
}
1534
1534
1535
- if ( maxId == details . LastReadInboxMessageId && pixel == null )
1535
+ if ( fromMessageId == details . LastReadInboxMessageId && pixel == null )
1536
1536
{
1537
- maxId = target . Id ;
1537
+ fromMessageId = target . Id ;
1538
1538
pixel = 28 + 48 ;
1539
1539
}
1540
1540
}
1541
1541
}
1542
1542
1543
1543
if ( firstVisibleItem != null && pixel == null )
1544
1544
{
1545
- maxId = firstVisibleItem . Id ;
1545
+ fromMessageId = firstVisibleItem . Id ;
1546
1546
}
1547
1547
1548
1548
// If we're loading the last message and it has been read already
1549
1549
// then we want to align it at bottom, as it might be taller than the window height
1550
- if ( maxId == details . LastMessageId && ! unread )
1550
+ if ( fromMessageId == details . LastMessageId && ! unread )
1551
1551
{
1552
1552
alignment = VerticalAlignment . Bottom ;
1553
1553
pixel = null ;
@@ -1556,14 +1556,16 @@ bool Included(long id)
1556
1556
1557
1557
if ( firstVisibleIndex == - 1 )
1558
1558
{
1559
- for ( int i = 0 ; i < messages . MessagesValue . Count ; i ++ )
1559
+ firstVisibleIndex = fromDateOffset != 0
1560
+ ? messages . MessagesValue . FindLastIndex ( m => m . Date >= fromDateOffset )
1561
+ : messages . MessagesValue . FindIndex ( m => m . Id == fromMessageId ) ;
1562
+
1563
+ if ( firstVisibleIndex != - 1 )
1560
1564
{
1561
- if ( messages . MessagesValue [ i ] . Id == maxId )
1562
- {
1563
- firstVisibleIndex = i ;
1565
+ if ( fromDateOffset != 0 )
1566
+ fromMessageId = messages . MessagesValue [ firstVisibleIndex ] . Id ;
1567
+ else
1564
1568
unread = false ;
1565
- break ;
1566
- }
1567
1569
}
1568
1570
}
1569
1571
@@ -1594,7 +1596,7 @@ bool Included(long id)
1594
1596
1595
1597
if ( values is IList < Message > temp )
1596
1598
{
1597
- if ( _forumTopic == null && _thread != null && ( maxId == 0 || _thread . Messages . Any ( x => x . Id == maxId ) ) )
1599
+ if ( _forumTopic == null && _thread != null && ( fromMessageId == 0 || _thread . Messages . Any ( x => x . Id == fromMessageId ) ) )
1598
1600
{
1599
1601
await AddHeaderAsync ( temp , temp . Count > 0 ? temp [ ^ 1 ] : null ) ;
1600
1602
}
@@ -1605,7 +1607,7 @@ bool Included(long id)
1605
1607
}
1606
1608
1607
1609
var replied = new MessageCollection ( this , null , values , false , Type ) ;
1608
- return new LoadSliceResult ( replied , maxId , scrollMode , alignment , pixel , unread , messages . TotalCount ) ;
1610
+ return new LoadSliceResult ( replied , fromMessageId , scrollMode , alignment , pixel , unread , messages . TotalCount ) ;
1609
1611
}
1610
1612
1611
1613
return null ;
@@ -1750,7 +1752,7 @@ public async Task LoadDateSliceAsync(int dateOffset)
1750
1752
var response = await ClientService . SendAsync ( new GetChatMessageByDate ( chat . Id , dateOffset ) ) ;
1751
1753
if ( response is Message message )
1752
1754
{
1753
- await LoadMessageSliceAsync ( null , message . Id ) ;
1755
+ await LoadMessageSliceAsync ( null , message . Id , fromDateOffset : dateOffset , onlyRemote : true ) ;
1754
1756
}
1755
1757
else
1756
1758
{
0 commit comments