Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit 7fa4d54

Browse files
committed
Fixed #129
1 parent 26f825c commit 7fa4d54

4 files changed

Lines changed: 32 additions & 24 deletions

File tree

View/OverviewMatches.xaml.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ public OverviewMatches()
2121
/// </summary>
2222
private void Profile_Click(object sender, RoutedEventArgs e)
2323
{
24-
Profile profile = ((Button)sender).DataContext as Profile;
25-
if (!FocusOpenedWindow<ProfileWindow>(profile))
24+
if (((Button)sender).DataContext is Profile profile && !FocusOpenedWindow<ProfileWindow>(profile))
2625
{
2726
ProfileWindow profileWindow = new ProfileWindow();
2827
profileWindow.ProfileWindowFrame.Content = new ProfilePage(new ProfilePageViewModel(profile));
2928

30-
profileWindow.Show(); // Show the authentication window
29+
profileWindow.Show();
3130
}
3231
}
3332

@@ -39,13 +38,12 @@ private void MatchingProfile_Click(object sender, RoutedEventArgs e)
3938
ProfileWindow profileWindow = new ProfileWindow();
4039
profileWindow.ProfileWindowFrame.Content = new MatchingProfilePage(new MatchingProfilePageViewModel(((Button)sender).DataContext as Profile));
4140

42-
profileWindow.Show(); // Show the authentication window
41+
profileWindow.Show();
4342
}
4443

4544
private void Chat_Click(object sender, RoutedEventArgs e)
4645
{
47-
Profile profile = ((Button)sender).DataContext as Profile;
48-
if (!FocusOpenedWindow<ChatWindow>(profile))
46+
if (((Button)sender).DataContext is Profile profile && !FocusOpenedWindow<ChatWindow>(profile))
4947
{
5048
ChatWindow chatWindow = new ChatWindow
5149
{
@@ -72,10 +70,10 @@ public static void Chat_Base(ChatWindow chatWindow)
7270
public static bool FocusOpenedWindow<T>(int userID) where T : Window
7371
{
7472
bool success = false;
75-
System.Windows.Application.Current.Dispatcher.Invoke(delegate // Dispatcher delegate for threads
73+
Application.Current.Dispatcher.Invoke(delegate // Dispatcher delegate for threads
7674
{
7775
// Check if a window is already open for the user, and if so, focus it instead of opening a new window
78-
foreach (T t in System.Windows.Application.Current.Windows.OfType<T>())
76+
foreach (T t in Application.Current.Windows.OfType<T>())
7977
{
8078
if (typeof(T) == typeof(ChatWindow))
8179
{

ViewModel/ChatWindowViewModel.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading;
88
using System.Windows.Input;
99
using ViewModel.Commands;
10+
using ViewModel.Mediators;
1011

1112
namespace ViewModel
1213
{
@@ -175,18 +176,21 @@ public void SeenChatMessages()
175176
}
176177
}
177178

178-
public bool ChatWindowHasFocus { internal get; set; }
179-
#endregion
180-
181-
#region Construction and destruction
182-
/// <summary>
183-
/// Creates an empty conversation
184-
/// </summary>
185-
public ChatWindowViewModel()
179+
public bool ChatWindowHasFocus
186180
{
187-
181+
get
182+
{
183+
if (!ViewModelMediators.ChatWindowFocus.ContainsKey(Receiver.UserID))
184+
{
185+
ViewModelMediators.ChatWindowFocus.Add(Receiver.UserID, true);
186+
}
187+
return ViewModelMediators.ChatWindowFocus[Receiver.UserID];
188+
}
189+
set => ViewModelMediators.ChatWindowFocus[Receiver.UserID] = value;
188190
}
191+
#endregion
189192

193+
#region Construction and destruction
190194
/// <summary>
191195
/// Creates a conversationViewModel with given userId
192196
/// </summary>
@@ -207,10 +211,11 @@ public ChatWindowViewModel(Profile otherUser)
207211
}
208212

209213
/// <summary>
210-
/// Destructs ChatWindowViewModel, and so stops the background timer for updating chats
214+
/// Destructs ChatWindowViewModel, and so stops the background timer for updating chats and cleans up ChatWindowFocus
211215
/// </summary>
212216
~ChatWindowViewModel()
213217
{
218+
ViewModelMediators.ChatWindowFocus.Remove(Receiver.UserID);
214219
Account.BackgroundThreads[backgroundThreadName].Dispose();
215220
Account.BackgroundThreads.Remove(backgroundThreadName);
216221
}

ViewModel/Helpers/NotificationHelper.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,15 @@ private static void ChatNotifications(object state)
190190
{
191191
foreach (ChatMessage c in unreadChatMessages)
192192
{
193-
Profile chatProfile = Account.Matches.FirstOrDefault(p => p.UserID == c.FromUserId);
194-
ThrowChatMessageNotification(chatProfile.FirstName,
195-
chatProfile.LastName,
196-
chatProfile.FirstUserMedia,
197-
c.Content,
198-
chatProfile.UserID);
193+
if (!ViewModelMediators.ChatWindowFocus[c.FromUserId])
194+
{
195+
Profile chatProfile = Account.Matches.FirstOrDefault(p => p.UserID == c.FromUserId);
196+
ThrowChatMessageNotification(chatProfile.FirstName,
197+
chatProfile.LastName,
198+
chatProfile.FirstUserMedia,
199+
c.Content,
200+
chatProfile.UserID);
201+
}
199202
c.Seen = true;
200203
}
201204
}

ViewModel/Mediators/ViewModelMediators.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public static List<Profile> Likes
5757
LikesChanged?.Invoke();
5858
}
5959
}
60+
61+
public static Dictionary<int, bool> ChatWindowFocus { get; set; } = new Dictionary<int, bool>();
6062
#endregion
6163
}
6264
}

0 commit comments

Comments
 (0)