Skip to content

Commit 694fe86

Browse files
committed
only move the last read index up, notify windows
1 parent c122538 commit 694fe86

File tree

5 files changed

+117
-8
lines changed

5 files changed

+117
-8
lines changed

Signal-Windows.Lib/SignalLibHandle.cs

+22
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public interface ISignalLibHandle
5050
//Frontend API
5151
SignalStore Store { get; set; }
5252
Task SendMessage(SignalMessage message, SignalConversation conversation);
53+
void SetMessageRead(long index, SignalConversation conversation);
5354
void ResendMessage(SignalMessage message);
5455
List<SignalMessageContainer> GetMessages(SignalConversation thread, int startIndex, int count);
5556
void SaveAndDispatchSignalConversation(SignalConversation updatedConversation, SignalMessage updateMessage);
@@ -326,6 +327,27 @@ await Task.Run(() =>
326327
});
327328
}
328329

330+
/// <summary>
331+
/// Marks and dispatches a message as read. Must not be called on a task which holds the handle lock.
332+
/// </summary>
333+
/// <param name="message"></param>
334+
public void SetMessageRead(long index, SignalConversation conversation)
335+
{
336+
Logger.LogTrace("SetMessageRead() locking");
337+
SemaphoreSlim.Wait(CancelSource.Token);
338+
try
339+
{
340+
Logger.LogTrace("SetMessageRead() locked");
341+
conversation = SignalDBContext.UpdateMessageRead(index, conversation);
342+
DispatchMessageRead(index, conversation);
343+
}
344+
finally
345+
{
346+
SemaphoreSlim.Release();
347+
Logger.LogTrace("SendMessage() released");
348+
}
349+
}
350+
329351
public void ResendMessage(SignalMessage message)
330352
{
331353
OutgoingQueue.Add(message);

Signal-Windows.Lib/Storage/DB.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,9 @@ public static void UpdateExpiresInLocked(SignalConversation thread, uint exp)
986986
}
987987

988988

989-
internal static void UpdateMessageRead(long index, SignalConversation conversation)
989+
internal static SignalConversation UpdateMessageRead(long index, SignalConversation conversation)
990990
{
991+
SignalConversation dbConversation = null;
991992
lock (DBLock)
992993
{
993994
using (var ctx = new SignalDBContext())
@@ -999,8 +1000,9 @@ internal static void UpdateMessageRead(long index, SignalConversation conversati
9991000
.SingleOrDefault();
10001001
if (contact != null)
10011002
{
1002-
contact.LastSeenMessageIndex = index;
1003-
contact.UnreadCount = (uint)(contact.MessagesCount - index);
1003+
contact.LastSeenMessageIndex = Math.Max(index, contact.LastSeenMessageIndex);
1004+
contact.UnreadCount = (uint)(contact.MessagesCount - contact.LastSeenMessageIndex);
1005+
dbConversation = contact;
10041006
}
10051007
}
10061008
else
@@ -1010,13 +1012,15 @@ internal static void UpdateMessageRead(long index, SignalConversation conversati
10101012
.SingleOrDefault();
10111013
if (group != null)
10121014
{
1013-
group.LastSeenMessageIndex = index;
1014-
group.UnreadCount = (uint)(group.MessagesCount - index);
1015+
group.LastSeenMessageIndex = Math.Max(index, group.LastSeenMessageIndex);
1016+
group.UnreadCount = (uint)(group.MessagesCount - group.LastSeenMessageIndex);
1017+
dbConversation = group;
10151018
}
10161019
}
10171020
ctx.SaveChanges();
10181021
}
10191022
}
1023+
return dbConversation;
10201024
}
10211025

10221026
#endregion Threads

Signal-Windows/Controls/Conversation.xaml

+66-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,71 @@
1212
d:DesignWidth="400">
1313

1414
<Control.Resources>
15+
<Style x:Key="ConversationStyle" TargetType="ListView">
16+
<Setter Property="IsTabStop" Value="False" />
17+
<Setter Property="TabNavigation" Value="Once" />
18+
<Setter Property="IsSwipeEnabled" Value="True" />
19+
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
20+
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
21+
<Setter Property="ScrollViewer.HorizontalScrollMode" Value="Disabled" />
22+
<Setter Property="ScrollViewer.IsHorizontalRailEnabled" Value="False" />
23+
<Setter Property="ScrollViewer.VerticalScrollMode" Value="Enabled" />
24+
<Setter Property="ScrollViewer.IsVerticalRailEnabled" Value="True" />
25+
<Setter Property="ScrollViewer.ZoomMode" Value="Disabled" />
26+
<Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False" />
27+
<Setter Property="ScrollViewer.BringIntoViewOnFocusChange" Value="True" />
28+
<Setter Property="ItemContainerTransitions">
29+
<Setter.Value>
30+
<TransitionCollection>
31+
<AddDeleteThemeTransition />
32+
<ContentThemeTransition />
33+
<ReorderThemeTransition />
34+
<EntranceThemeTransition IsStaggeringEnabled="False" />
35+
</TransitionCollection>
36+
</Setter.Value>
37+
</Setter>
38+
<Setter Property="ItemsPanel">
39+
<Setter.Value>
40+
<ItemsPanelTemplate>
41+
<ItemsStackPanel Orientation="Vertical" />
42+
</ItemsPanelTemplate>
43+
</Setter.Value>
44+
</Setter>
45+
<Setter Property="Template">
46+
<Setter.Value>
47+
<ControlTemplate TargetType="ListView">
48+
<Border BorderBrush="{TemplateBinding BorderBrush}"
49+
Background="{TemplateBinding Background}"
50+
BorderThickness="{TemplateBinding BorderThickness}">
51+
<ScrollViewer x:Name="ScrollViewer"
52+
ViewChanged="ScrollViewer_ViewChanged"
53+
TabNavigation="{TemplateBinding TabNavigation}"
54+
HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
55+
HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
56+
IsHorizontalScrollChainingEnabled="{TemplateBinding ScrollViewer.IsHorizontalScrollChainingEnabled}"
57+
VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
58+
VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
59+
IsVerticalScrollChainingEnabled="{TemplateBinding ScrollViewer.IsVerticalScrollChainingEnabled}"
60+
IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}"
61+
IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}"
62+
ZoomMode="{TemplateBinding ScrollViewer.ZoomMode}"
63+
IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
64+
BringIntoViewOnFocusChange="{TemplateBinding ScrollViewer.BringIntoViewOnFocusChange}"
65+
AutomationProperties.AccessibilityView="Raw">
66+
<ItemsPresenter
67+
Header="{TemplateBinding Header}"
68+
HeaderTemplate="{TemplateBinding HeaderTemplate}"
69+
HeaderTransitions="{TemplateBinding HeaderTransitions}"
70+
Footer="{TemplateBinding Footer}"
71+
FooterTemplate="{TemplateBinding FooterTemplate}"
72+
FooterTransitions="{TemplateBinding FooterTransitions}"
73+
Padding="{TemplateBinding Padding}"/>
74+
</ScrollViewer>
75+
</Border>
76+
</ControlTemplate>
77+
</Setter.Value>
78+
</Setter>
79+
</Style>
1580
<DataTemplate x:Key="NormalMessageTemplate" x:DataType="models:SignalMessage">
1681
<local:Message x:Name="ListBoxItemContent" />
1782
</DataTemplate>
@@ -54,7 +119,7 @@
54119
</Button>
55120
</Grid>
56121
</Border>
57-
<ListView Grid.Row="1" Name="ConversationItemsControl" VirtualizingStackPanel.VirtualizationMode="Recycling" Background="White" ScrollViewer.VerticalScrollBarVisibility="Visible" Padding="0 0 15 0" ItemTemplateSelector="{StaticResource MessageDataTemplateSelector}" SelectionMode="None">
122+
<ListView Style="{StaticResource ConversationStyle}" Grid.Row="1" Name="ConversationItemsControl" VirtualizingStackPanel.VirtualizationMode="Recycling" Background="White" ScrollViewer.VerticalScrollBarVisibility="Visible" Padding="0 0 15 0" ItemTemplateSelector="{StaticResource MessageDataTemplateSelector}" SelectionMode="None">
58123
<ListView.ItemContainerStyle>
59124
<Style TargetType="ListViewItem">
60125
<Setter Property="HorizontalContentAlignment" Value="Stretch" />

Signal-Windows/Controls/Conversation.xaml.cs

+19-2
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,7 @@ public void UpdateAttachment(SignalAttachment sa)
225225
public AppendResult Append(SignalMessageContainer sm)
226226
{
227227
AppendResult result = null;
228-
var sourcePanel = (ItemsStackPanel)ConversationItemsControl.ItemsPanelRoot;
229-
bool bottom = sourcePanel.LastVisibleIndex == Collection.Count - 2; // -2 because we already incremented Count
228+
bool bottom = GetBottommostIndex() == Collection.Count - 2; // -2 because we already incremented Count
230229
Collection.Add(sm, sm.Message.Author == null);
231230
if (bottom)
232231
{
@@ -302,6 +301,12 @@ private void ScrollToUnread()
302301
}
303302
}
304303

304+
private int GetBottommostIndex()
305+
{
306+
var sourcePanel = (ItemsStackPanel)ConversationItemsControl.ItemsPanelRoot;
307+
return sourcePanel.LastVisibleIndex;
308+
}
309+
305310
private void ConversationSettingsButton_Click(object sender, RoutedEventArgs e)
306311
{
307312
if (SignalConversation is SignalContact)
@@ -310,6 +315,18 @@ private void ConversationSettingsButton_Click(object sender, RoutedEventArgs e)
310315
GetMainPageVm().View.Frame.Navigate(typeof(ConversationSettingsPage));
311316
}
312317
}
318+
319+
private void ScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
320+
{
321+
int bottomIndex = GetBottommostIndex();
322+
if (SignalConversation.LastSeenMessageIndex < bottomIndex)
323+
{
324+
Task.Run(() =>
325+
{
326+
App.Handle.SetMessageRead(bottomIndex, SignalConversation);
327+
});
328+
}
329+
}
313330
}
314331

315332
public class MessageTemplateSelector : DataTemplateSelector

Signal-Windows/ViewModels/MainPageViewModel.cs

+1
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ public void HandleMessageRead(long messageIndex, SignalConversation conversation
243243
var localConversation = ConversationsDictionary[conversation.ThreadId];
244244
Logger.LogTrace("LastSeenMessageIndex = {0}", messageIndex);
245245
localConversation.LastSeenMessageIndex = messageIndex;
246+
localConversation.UnreadCount = conversation.UnreadCount;
246247
localConversation.UpdateUI();
247248
}
248249

0 commit comments

Comments
 (0)