Skip to content

Commit d6a0684

Browse files
committed
Fix loading messages by date
1 parent 95f1753 commit d6a0684

File tree

2 files changed

+50
-34
lines changed

2 files changed

+50
-34
lines changed

Telegram/Common/Extensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ void OnClosed(ContentDialog sender, ContentDialogClosedEventArgs args)
129129
return popup.ShowQueuedAsync(frame.XamlRoot);
130130
}
131131

132+
public static int FindIndex<T>(this IList<T> list, Func<T, bool> predicate)
133+
{
134+
for (int i = 0; i < list.Count; i++)
135+
if (predicate(list[i])) return i;
136+
return -1;
137+
}
138+
139+
public static int FindLastIndex<T>(this IList<T> list, Func<T, bool> predicate)
140+
{
141+
for (int i = list.Count - 1; i >= 0; i--)
142+
if (predicate(list[i])) return i;
143+
return -1;
144+
}
145+
132146
public static void AddCubicBezier(this PathFigure figure, Point controlPoint1, Point controlPoint2, Point endPoint)
133147
{
134148
figure.Segments.Add(new BezierSegment

Telegram/ViewModels/DialogViewModel.cs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ protected void NotifyMessageSliceLoaded()
11451145
MessageSliceLoaded = null;
11461146
}
11471147

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)
11491149
{
11501150
if (Type is not DialogType.History and not DialogType.Thread and not DialogType.Pinned)
11511151
{
@@ -1166,15 +1166,15 @@ public async Task LoadMessageSliceAsync(long? previousId, long maxId, VerticalAl
11661166

11671167
if (direction == null && TryGetFirstVisibleMessageId(out long firstVisibleId))
11681168
{
1169-
direction = firstVisibleId < maxId ? ScrollIntoViewAlignment.Default : ScrollIntoViewAlignment.Leading;
1169+
direction = firstVisibleId < fromMessageId ? ScrollIntoViewAlignment.Default : ScrollIntoViewAlignment.Leading;
11701170
}
11711171

11721172
if (alignment == VerticalAlignment.Bottom && pixel == null)
11731173
{
11741174
pixel = int.MaxValue;
11751175
}
11761176

1177-
if (onlyRemote is false && Items.TryGetValue(maxId, out MessageViewModel already))
1177+
if (onlyRemote is false && Items.TryGetValue(fromMessageId, out MessageViewModel already))
11781178
{
11791179
if (alignment == VerticalAlignment.Center)
11801180
{
@@ -1199,14 +1199,14 @@ public async Task LoadMessageSliceAsync(long? previousId, long maxId, VerticalAl
11991199

12001200
// If we're loading the last message and it has been read already
12011201
// 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)
12031203
{
12041204
alignment = VerticalAlignment.Bottom;
12051205
pixel = null;
12061206
}
12071207
}
12081208

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);
12101210

12111211
if (previousId.HasValue && !_repliesStack.Contains(previousId.Value))
12121212
{
@@ -1234,12 +1234,12 @@ public async Task LoadMessageSliceAsync(long? previousId, long maxId, VerticalAl
12341234

12351235
System.Diagnostics.Debug.WriteLine("DialogViewModel: LoadMessageSliceAsync");
12361236

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));
12381238
if (response is LoadSliceResult slice)
12391239
{
12401240
_groupedMessages.Clear();
12411241

1242-
maxId = slice.FromMessageId;
1242+
fromMessageId = slice.FromMessageId;
12431243
pixel = slice.Pixel;
12441244
alignment = slice.Alignment;
12451245

@@ -1272,9 +1272,9 @@ public async Task LoadMessageSliceAsync(long? previousId, long maxId, VerticalAl
12721272
IsOldestSliceLoaded = null;
12731273
IsNewestSliceLoaded = endReached;
12741274

1275-
if (Items.TryGetValue(maxId, out already))
1275+
if (Items.TryGetValue(fromMessageId, out already))
12761276
{
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);
12781278

12791279
if (previousId.HasValue && !_repliesStack.Contains(previousId.Value))
12801280
{
@@ -1319,7 +1319,7 @@ public async Task LoadMessageSliceAsync(long? previousId, long maxId, VerticalAl
13191319
_loadingSlice = false;
13201320
IsLoading = false;
13211321

1322-
PinnedMessages.LoadSlice(maxId);
1322+
PinnedMessages.LoadSlice(fromMessageId);
13231323
}
13241324

13251325
if (loadMore != PanelScrollingDirection.None)
@@ -1377,39 +1377,39 @@ private async Task<Messages> PreloadAlbumsAsync(long chatId, FoundChatMessages f
13771377
return new Messages(foundChatMessages.TotalCount, foundChatMessages.Messages);
13781378
}
13791379

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)
13811381
{
13821382
Task<Object> func;
13831383
if (Type == DialogType.Pinned)
13841384
{
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()));
13861386
}
13871387
else if (Search?.SavedMessagesTag != null && Search.FilterByTag)
13881388
{
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));
13901390
}
13911391
else if (SavedMessagesTopic != null)
13921392
{
1393-
func = ClientService.SendAsync(new GetSavedMessagesTopicHistory(SavedMessagesTopic.Id, maxId, -25, 50));
1393+
func = ClientService.SendAsync(new GetSavedMessagesTopicHistory(SavedMessagesTopic.Id, fromMessageId, -25, 50));
13941394
}
13951395
else if (DirectMessagesChatTopic != null)
13961396
{
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));
13981398
}
13991399
else if (ForumTopic != null)
14001400
{
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));
14021402
}
14031403
else if (Thread != null)
14041404
{
14051405
// 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))
14071407
{
14081408
func = ClientService.SendAsync(new GetMessageThreadHistory(chat.Id, _thread.MessageThreadId, 1, -25, 50));
14091409
}
14101410
else
14111411
{
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));
14131413
}
14141414
}
14151415
else
@@ -1447,7 +1447,7 @@ async Task<Object> GetChatHistoryAsync(long chatId, long fromMessageId, int offs
14471447
return response;
14481448
}
14491449

1450-
func = GetChatHistoryAsync(chat.Id, maxId, -25, 50, alignment == VerticalAlignment.Top);
1450+
func = GetChatHistoryAsync(chat.Id, fromMessageId, -25, 50, alignment == VerticalAlignment.Top);
14511451
}
14521452

14531453
if (alignment != VerticalAlignment.Center)
@@ -1486,7 +1486,7 @@ bool Included(long id)
14861486

14871487
// If we're loading from the last read message
14881488
// 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*/)
14901490
{
14911491
var target = default(Message);
14921492
var index = -1;
@@ -1527,27 +1527,27 @@ bool Included(long id)
15271527
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));
15281528
unread = true;
15291529
}
1530-
else if (maxId == details.LastReadInboxMessageId)
1530+
else if (fromMessageId == details.LastReadInboxMessageId)
15311531
{
15321532
Logger.Debug("Looking for first unread message, can't find it");
15331533
}
15341534

1535-
if (maxId == details.LastReadInboxMessageId && pixel == null)
1535+
if (fromMessageId == details.LastReadInboxMessageId && pixel == null)
15361536
{
1537-
maxId = target.Id;
1537+
fromMessageId = target.Id;
15381538
pixel = 28 + 48;
15391539
}
15401540
}
15411541
}
15421542

15431543
if (firstVisibleItem != null && pixel == null)
15441544
{
1545-
maxId = firstVisibleItem.Id;
1545+
fromMessageId = firstVisibleItem.Id;
15461546
}
15471547

15481548
// If we're loading the last message and it has been read already
15491549
// 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)
15511551
{
15521552
alignment = VerticalAlignment.Bottom;
15531553
pixel = null;
@@ -1556,14 +1556,16 @@ bool Included(long id)
15561556

15571557
if (firstVisibleIndex == -1)
15581558
{
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)
15601564
{
1561-
if (messages.MessagesValue[i].Id == maxId)
1562-
{
1563-
firstVisibleIndex = i;
1565+
if (fromDateOffset != 0)
1566+
fromMessageId = messages.MessagesValue[firstVisibleIndex].Id;
1567+
else
15641568
unread = false;
1565-
break;
1566-
}
15671569
}
15681570
}
15691571

@@ -1594,7 +1596,7 @@ bool Included(long id)
15941596

15951597
if (values is IList<Message> temp)
15961598
{
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)))
15981600
{
15991601
await AddHeaderAsync(temp, temp.Count > 0 ? temp[^1] : null);
16001602
}
@@ -1605,7 +1607,7 @@ bool Included(long id)
16051607
}
16061608

16071609
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);
16091611
}
16101612

16111613
return null;
@@ -1750,7 +1752,7 @@ public async Task LoadDateSliceAsync(int dateOffset)
17501752
var response = await ClientService.SendAsync(new GetChatMessageByDate(chat.Id, dateOffset));
17511753
if (response is Message message)
17521754
{
1753-
await LoadMessageSliceAsync(null, message.Id);
1755+
await LoadMessageSliceAsync(null, message.Id, fromDateOffset: dateOffset, onlyRemote: true);
17541756
}
17551757
else
17561758
{

0 commit comments

Comments
 (0)