@@ -93,16 +93,20 @@ private static void MatchOrLikeNotification(object matchOrLike)
9393 List < Profile > profileList ;
9494
9595 // Prepares data depending on the match or like handling
96- if ( ( MatchOrLike ) matchOrLike == MatchOrLike . Matched )
96+ if ( ( MatchOrLike ) matchOrLike == MatchOrLike . Matched && Account . UserID != null )
9797 {
9898 current = new HashSet < int > ( MatchDataAccess . GetAllMatchesFromUser ( Account . UserID . Value , MatchOrLike . Matched ) ) ;
9999 profileList = ViewModelMediators . Matches ;
100100 }
101- else
101+ else if ( ( MatchOrLike ) matchOrLike == MatchOrLike . Liked && Account . UserID != null )
102102 {
103103 current = new HashSet < int > ( MatchDataAccess . GetReceivedLikesFromUser ( Account . UserID . Value ) ) ;
104104 profileList = ViewModelMediators . Likes ;
105105 }
106+ else
107+ {
108+ throw new Exception ( "Match or like notification MatchOrLike enum not set correctly" ) ;
109+ }
106110
107111 // Add the user ID's to the HashSet
108112 foreach ( Profile p in profileList )
@@ -116,17 +120,29 @@ private static void MatchOrLikeNotification(object matchOrLike)
116120 // If the amounts are different OR there is at least 1 new user...
117121 if ( current . Count != previous . Count || current . Count > 0 )
118122 {
119- bool notificationsOn ;
123+ bool notificationsOn = false ;
120124 // ...reload the profiles
121125 if ( ( MatchOrLike ) matchOrLike == MatchOrLike . Matched )
122126 {
123- ViewModelMediators . Matches = MatchHelper . LoadProfilesOfMatches ( Account . UserID . Value ) ;
124- notificationsOn = Account . NotificationSettings . Matches ;
127+ if ( Account . UserID != null )
128+ {
129+ ViewModelMediators . Matches = MatchHelper . LoadProfilesOfMatches ( Account . UserID . Value ) ;
130+ }
131+ if ( Account . NotificationSettings != null )
132+ {
133+ notificationsOn = Account . NotificationSettings . Matches ;
134+ }
125135 }
126136 else
127137 {
128- ViewModelMediators . Likes = MatchHelper . LoadProfilesOfLikes ( Account . UserID . Value ) ;
129- notificationsOn = Account . NotificationSettings . Likes ;
138+ if ( Account . UserID != null )
139+ {
140+ ViewModelMediators . Likes = MatchHelper . LoadProfilesOfLikes ( Account . UserID . Value ) ;
141+ }
142+ if ( Account . NotificationSettings != null )
143+ {
144+ notificationsOn = Account . NotificationSettings . Likes ;
145+ }
130146 }
131147
132148 // If the current amount are more (so there is a new user instead of an user who removed you) and notifications are turned on...
@@ -172,30 +188,32 @@ private static void ChatNotifications(object state)
172188 unreadChatMessages . Add ( c ) ;
173189 }
174190 }
191+ Account . NotifiedChatMessages = newChatMessages ; // Set the NotifiedChatMessages to the newly obtained chat messages list
175192
176193 // Reload the profiles of matches if there have been new unread chat messages (for chat notification indicator)
177194 if ( unreadChatMessages . Count > 0 )
178195 {
179196 ViewModelMediators . Matches = MatchHelper . LoadProfilesOfMatches ( Account . UserID . Value ) ;
180- }
181197
182- // Throw notifications for every new chat message if notifications are on
183- if ( Account . NotificationSettings . Chat )
184- {
185- foreach ( ChatMessage c in unreadChatMessages )
198+ // Throw notifications for every new chat message if notifications are on
199+ if ( Account . NotificationSettings != null && Account . NotificationSettings . Chat )
186200 {
187- Profile chatProfile = Account . Matches . FirstOrDefault ( p => p . UserID == c . FromUserId ) ;
188- ThrowChatMessageNotification ( chatProfile . FirstName ,
189- chatProfile . LastName ,
190- chatProfile . FirstUserMedia ,
191- c . Content ,
192- chatProfile . UserID ) ;
193- c . Seen = true ;
201+ foreach ( ChatMessage c in unreadChatMessages )
202+ {
203+ bool dictionaryContainsUser = ViewModelMediators . ChatWindowFocus . ContainsKey ( c . FromUserId ) ;
204+ if ( ( dictionaryContainsUser && ! ViewModelMediators . ChatWindowFocus [ c . FromUserId ] ) || ! dictionaryContainsUser )
205+ {
206+ Profile chatProfile = Account . Matches . FirstOrDefault ( p => p . UserID == c . FromUserId ) ;
207+ ThrowChatMessageNotification ( chatProfile . FirstName ,
208+ chatProfile . LastName ,
209+ chatProfile . FirstUserMedia ,
210+ c . Content ,
211+ chatProfile . UserID ) ;
212+ }
213+ c . Seen = true ;
214+ }
194215 }
195216 }
196-
197- // Set the NotifiedChatMessages to the newly obtained chat messages list
198- Account . NotifiedChatMessages = newChatMessages ;
199217 }
200218 }
201219 #endregion
0 commit comments