Skip to content

Commit

Permalink
only move the last read index up, notify windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Trolldemorted committed Apr 27, 2018
1 parent c122538 commit 694fe86
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 8 deletions.
22 changes: 22 additions & 0 deletions Signal-Windows.Lib/SignalLibHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public interface ISignalLibHandle
//Frontend API
SignalStore Store { get; set; }
Task SendMessage(SignalMessage message, SignalConversation conversation);
void SetMessageRead(long index, SignalConversation conversation);
void ResendMessage(SignalMessage message);
List<SignalMessageContainer> GetMessages(SignalConversation thread, int startIndex, int count);
void SaveAndDispatchSignalConversation(SignalConversation updatedConversation, SignalMessage updateMessage);
Expand Down Expand Up @@ -326,6 +327,27 @@ await Task.Run(() =>
});
}

/// <summary>
/// Marks and dispatches a message as read. Must not be called on a task which holds the handle lock.
/// </summary>
/// <param name="message"></param>
public void SetMessageRead(long index, SignalConversation conversation)
{
Logger.LogTrace("SetMessageRead() locking");
SemaphoreSlim.Wait(CancelSource.Token);
try
{
Logger.LogTrace("SetMessageRead() locked");
conversation = SignalDBContext.UpdateMessageRead(index, conversation);
DispatchMessageRead(index, conversation);
}
finally
{
SemaphoreSlim.Release();
Logger.LogTrace("SendMessage() released");
}
}

public void ResendMessage(SignalMessage message)
{
OutgoingQueue.Add(message);
Expand Down
14 changes: 9 additions & 5 deletions Signal-Windows.Lib/Storage/DB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -986,8 +986,9 @@ public static void UpdateExpiresInLocked(SignalConversation thread, uint exp)
}


internal static void UpdateMessageRead(long index, SignalConversation conversation)
internal static SignalConversation UpdateMessageRead(long index, SignalConversation conversation)
{
SignalConversation dbConversation = null;
lock (DBLock)
{
using (var ctx = new SignalDBContext())
Expand All @@ -999,8 +1000,9 @@ internal static void UpdateMessageRead(long index, SignalConversation conversati
.SingleOrDefault();
if (contact != null)
{
contact.LastSeenMessageIndex = index;
contact.UnreadCount = (uint)(contact.MessagesCount - index);
contact.LastSeenMessageIndex = Math.Max(index, contact.LastSeenMessageIndex);
contact.UnreadCount = (uint)(contact.MessagesCount - contact.LastSeenMessageIndex);
dbConversation = contact;
}
}
else
Expand All @@ -1010,13 +1012,15 @@ internal static void UpdateMessageRead(long index, SignalConversation conversati
.SingleOrDefault();
if (group != null)
{
group.LastSeenMessageIndex = index;
group.UnreadCount = (uint)(group.MessagesCount - index);
group.LastSeenMessageIndex = Math.Max(index, group.LastSeenMessageIndex);
group.UnreadCount = (uint)(group.MessagesCount - group.LastSeenMessageIndex);
dbConversation = group;
}
}
ctx.SaveChanges();
}
}
return dbConversation;
}

#endregion Threads
Expand Down
67 changes: 66 additions & 1 deletion Signal-Windows/Controls/Conversation.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,71 @@
d:DesignWidth="400">

<Control.Resources>
<Style x:Key="ConversationStyle" TargetType="ListView">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="TabNavigation" Value="Once" />
<Setter Property="IsSwipeEnabled" Value="True" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.HorizontalScrollMode" Value="Disabled" />
<Setter Property="ScrollViewer.IsHorizontalRailEnabled" Value="False" />
<Setter Property="ScrollViewer.VerticalScrollMode" Value="Enabled" />
<Setter Property="ScrollViewer.IsVerticalRailEnabled" Value="True" />
<Setter Property="ScrollViewer.ZoomMode" Value="Disabled" />
<Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False" />
<Setter Property="ScrollViewer.BringIntoViewOnFocusChange" Value="True" />
<Setter Property="ItemContainerTransitions">
<Setter.Value>
<TransitionCollection>
<AddDeleteThemeTransition />
<ContentThemeTransition />
<ReorderThemeTransition />
<EntranceThemeTransition IsStaggeringEnabled="False" />
</TransitionCollection>
</Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<ItemsStackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListView">
<Border BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer x:Name="ScrollViewer"
ViewChanged="ScrollViewer_ViewChanged"
TabNavigation="{TemplateBinding TabNavigation}"
HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
IsHorizontalScrollChainingEnabled="{TemplateBinding ScrollViewer.IsHorizontalScrollChainingEnabled}"
VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
IsVerticalScrollChainingEnabled="{TemplateBinding ScrollViewer.IsVerticalScrollChainingEnabled}"
IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}"
IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}"
ZoomMode="{TemplateBinding ScrollViewer.ZoomMode}"
IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
BringIntoViewOnFocusChange="{TemplateBinding ScrollViewer.BringIntoViewOnFocusChange}"
AutomationProperties.AccessibilityView="Raw">
<ItemsPresenter
Header="{TemplateBinding Header}"
HeaderTemplate="{TemplateBinding HeaderTemplate}"
HeaderTransitions="{TemplateBinding HeaderTransitions}"
Footer="{TemplateBinding Footer}"
FooterTemplate="{TemplateBinding FooterTemplate}"
FooterTransitions="{TemplateBinding FooterTransitions}"
Padding="{TemplateBinding Padding}"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="NormalMessageTemplate" x:DataType="models:SignalMessage">
<local:Message x:Name="ListBoxItemContent" />
</DataTemplate>
Expand Down Expand Up @@ -54,7 +119,7 @@
</Button>
</Grid>
</Border>
<ListView Grid.Row="1" Name="ConversationItemsControl" VirtualizingStackPanel.VirtualizationMode="Recycling" Background="White" ScrollViewer.VerticalScrollBarVisibility="Visible" Padding="0 0 15 0" ItemTemplateSelector="{StaticResource MessageDataTemplateSelector}" SelectionMode="None">
<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">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
Expand Down
21 changes: 19 additions & 2 deletions Signal-Windows/Controls/Conversation.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,7 @@ public void UpdateAttachment(SignalAttachment sa)
public AppendResult Append(SignalMessageContainer sm)
{
AppendResult result = null;
var sourcePanel = (ItemsStackPanel)ConversationItemsControl.ItemsPanelRoot;
bool bottom = sourcePanel.LastVisibleIndex == Collection.Count - 2; // -2 because we already incremented Count
bool bottom = GetBottommostIndex() == Collection.Count - 2; // -2 because we already incremented Count
Collection.Add(sm, sm.Message.Author == null);
if (bottom)
{
Expand Down Expand Up @@ -302,6 +301,12 @@ private void ScrollToUnread()
}
}

private int GetBottommostIndex()
{
var sourcePanel = (ItemsStackPanel)ConversationItemsControl.ItemsPanelRoot;
return sourcePanel.LastVisibleIndex;
}

private void ConversationSettingsButton_Click(object sender, RoutedEventArgs e)
{
if (SignalConversation is SignalContact)
Expand All @@ -310,6 +315,18 @@ private void ConversationSettingsButton_Click(object sender, RoutedEventArgs e)
GetMainPageVm().View.Frame.Navigate(typeof(ConversationSettingsPage));
}
}

private void ScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
int bottomIndex = GetBottommostIndex();
if (SignalConversation.LastSeenMessageIndex < bottomIndex)
{
Task.Run(() =>
{
App.Handle.SetMessageRead(bottomIndex, SignalConversation);
});
}
}
}

public class MessageTemplateSelector : DataTemplateSelector
Expand Down
1 change: 1 addition & 0 deletions Signal-Windows/ViewModels/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public void HandleMessageRead(long messageIndex, SignalConversation conversation
var localConversation = ConversationsDictionary[conversation.ThreadId];
Logger.LogTrace("LastSeenMessageIndex = {0}", messageIndex);
localConversation.LastSeenMessageIndex = messageIndex;
localConversation.UnreadCount = conversation.UnreadCount;
localConversation.UpdateUI();
}

Expand Down

0 comments on commit 694fe86

Please sign in to comment.