diff --git a/ChatController/ChatController.csproj b/ChatController/ChatController.csproj index b411819..3449ee1 100644 --- a/ChatController/ChatController.csproj +++ b/ChatController/ChatController.csproj @@ -72,6 +72,8 @@ ChatMainControl.xaml + + diff --git a/ChatController/ChatKlassen/Contact.cs b/ChatController/ChatKlassen/Contact.cs index d28734f..a934c06 100644 --- a/ChatController/ChatKlassen/Contact.cs +++ b/ChatController/ChatKlassen/Contact.cs @@ -1,6 +1,7 @@ using System; using System.ComponentModel; using System.Windows.Media; +using ChatController.LoginKlassen; using static System.String; namespace ChatController.ChatKlassen @@ -9,18 +10,10 @@ namespace ChatController.ChatKlassen { public string Name { get; } - private string _ReceivedMessage; - public string ReceivedMessage + private GroupLatestMessage _ReceivedMessage; + public GroupLatestMessage ReceivedMessage { - get - { - if (!IsNullOrEmpty(_ReceivedMessage) && _ReceivedMessage.Length > 25) - { - return _ReceivedMessage.Substring(0, 25) + "..."; - } - - return _ReceivedMessage; - } + get => _ReceivedMessage; set { @@ -84,10 +77,10 @@ namespace ChatController.ChatKlassen public int UserOid { get; set; } public string UserIdManage { get; set; } - public Contact(string pName, ImageSource pImage, string pLastMessageText, DateTime pTimeStamp, int pGroupId, string pUserIdManage, bool pOnlyEmployees, int pUserCount) + public Contact(string pName, ImageSource pImage, GroupLatestMessage pLatestMessage, DateTime pTimeStamp, int pGroupId, string pUserIdManage, bool pOnlyEmployees, int pUserCount) { Name = pName; - ReceivedMessage = pLastMessageText; + ReceivedMessage = pLatestMessage; Image = pImage; TimeStamp = pTimeStamp; GroupId = pGroupId; diff --git a/ChatController/ChatMainControl.xaml b/ChatController/ChatMainControl.xaml index e864040..d2f7f79 100644 --- a/ChatController/ChatMainControl.xaml +++ b/ChatController/ChatMainControl.xaml @@ -92,7 +92,7 @@ - + diff --git a/ChatController/ChatMainControl.xaml.cs b/ChatController/ChatMainControl.xaml.cs index 8b14784..abf6f65 100644 --- a/ChatController/ChatMainControl.xaml.cs +++ b/ChatController/ChatMainControl.xaml.cs @@ -15,6 +15,7 @@ using System.Windows.Navigation; using System.Windows.Threading; using ChatController.ChatKlassen; +using ChatController.Extensions; using ChatController.HauptKlassen; using ChatController.LoginKlassen; using ChatController.Utilities; @@ -36,7 +37,7 @@ namespace ChatController { public partial class ChatMainControl { - private readonly Dictionary _Group2NotifyIcons = new Dictionary(); + private readonly Dictionary> _Group2NotifyIcons = new Dictionary>(); private readonly Dictionary _GroupId2DateTime = new Dictionary(); @@ -127,51 +128,51 @@ namespace ChatController Clientlist.Items.Refresh(); } - public void ShowNotification(string pTitle, string pMessage, int pGroupId) + public void ShowNotification(string pTitle, GroupLatestMessage pMessage, int pGroupId) { - if (!_Group2NotifyIcons.ContainsKey(pGroupId)) + var notificationIcon = Resource.ownchat_favicon; + + var contact = _ContactList.FirstOrDefault(a => a.GroupId == pGroupId); + + if (contact != null) { - var notificationIcon = Resource.ownchat_favicon; - - var contact = _ContactList.FirstOrDefault(a => a.GroupId == pGroupId); - - if(contact != null) - { - notificationIcon = Utils.ImageSourceToIcon(contact.Image); - } - - var notifyIcon = new NotifyIcon { Icon = notificationIcon, Visible = true }; - - notifyIcon.BalloonTipClicked += (sender, args) => - { - DisposeAndRemoveNotification((NotifyIcon)sender, pGroupId); - - if (_ContainingWindow.WindowState == WindowState.Minimized) - { - _ContainingWindow.WindowState = WindowState.Normal; - } - - _ContainingWindow.Activate(); - - SelectContact(_ContactList.FirstOrDefault(f => f.GroupId.Equals(pGroupId))); - }; - - notifyIcon.BalloonTipClosed += (sender, args) => - { - DisposeAndRemoveNotification((NotifyIcon)sender, pGroupId); - }; - - _Group2NotifyIcons.Add(pGroupId, notifyIcon); + notificationIcon = Utils.ImageSourceToIcon(contact.Image); } - _Group2NotifyIcons[pGroupId].ShowBalloonTip(Constants.NotificationTimeout, pTitle, pMessage, ToolTipIcon.None); + var notifyIcon = new NotifyIcon { Icon = notificationIcon, Visible = true, Tag = pMessage }; + + notifyIcon.BalloonTipClicked += (sender, args) => + { + DisposeAndRemoveNotification((NotifyIcon)sender, pGroupId); + + if (_ContainingWindow.WindowState == WindowState.Minimized) + { + _ContainingWindow.WindowState = WindowState.Normal; + } + + _ContainingWindow.Activate(); + + SelectContact(_ContactList.FirstOrDefault(f => f.GroupId.Equals(pGroupId))); + }; + + notifyIcon.BalloonTipClosed += (sender, args) => + { + DisposeAndRemoveNotification((NotifyIcon)sender, pGroupId); + }; + + _Group2NotifyIcons.AddOrUpdateValueInDictionary(pGroupId, notifyIcon); + + _Group2NotifyIcons[pGroupId].Find(f => f.Tag is GroupLatestMessage message && message.Id.Equals(pMessage.Id))?.ShowBalloonTip(Constants.NotificationTimeout, pTitle, "Sie haben eine neue Nachricht", ToolTipIcon.None); } public void ClearNotifications() { foreach(var group2NotifyIcon in _Group2NotifyIcons) { - group2NotifyIcon.Value.Dispose(); + foreach(var notifyIcon in group2NotifyIcon.Value) + { + notifyIcon.Dispose(); + } } _Group2NotifyIcons.Clear(); @@ -701,7 +702,8 @@ namespace ChatController { if (contact.GroupId == newContact.GroupId) { - if (!contact.ReceivedMessage.Equals(newContact.ReceivedMessage)) + // ReceivedMessage kann null sein! + if (contact.ReceivedMessage != null && !contact.ReceivedMessage.Id.Equals(newContact.ReceivedMessage?.Id)) { contact.ReceivedMessage = newContact.ReceivedMessage; contact.TimeStamp = newContact.TimeStamp; @@ -710,7 +712,7 @@ namespace ChatController if ((Clientlist.SelectedItem != null && !((Contact)Clientlist.SelectedItem).GroupId.Equals(newContact.GroupId) || Clientlist.SelectedItem == null) || _IsInBackground) { ContainingWindow.Icon = Utils.GetImageSourceFromIcon(Resource.oC_favico_NewMessage); - ShowNotification(newContact.Name, "Sie haben eine neue Nachricht", newContact.GroupId); + ShowNotification(newContact.Name, newContact.ReceivedMessage, newContact.GroupId); } // Wenn der momentan ausgewählte Kontakt der aktuelle Kontakt in der Schleife ist, werden die Nachrichten nicht als ungelesen angezeigt, sonst schon. @@ -1014,7 +1016,7 @@ namespace ChatController foreach(var contactItem in Clientlist.Items) { - if(contactItem is Contact contact && contact.HasUnreadMessages && !string.IsNullOrEmpty(contact.ReceivedMessage)) + if(contactItem is Contact contact && contact.HasUnreadMessages && !string.IsNullOrEmpty(contact.ReceivedMessage?.Text)) { contacts.Add(contact); } diff --git a/ChatController/HauptKlassen/Chat.cs b/ChatController/HauptKlassen/Chat.cs index 453f01b..ee0dfe9 100644 --- a/ChatController/HauptKlassen/Chat.cs +++ b/ChatController/HauptKlassen/Chat.cs @@ -1234,26 +1234,18 @@ namespace ChatController.HauptKlassen foreach(var groupInput in pGroupInputs) { - var messageText = string.Empty; var formattedTime = new DateTime(); var latestMessage = groupInput.LastMessage; if(latestMessage?.Text != null) { - messageText = latestMessage.Text; - var timeStamp = DateTime.Parse(latestMessage.Timestamp.Date); var clientZone = TimeZoneInfo.Local; formattedTime = TimeZoneInfo.ConvertTimeFromUtc(timeStamp, clientZone); } - if(string.IsNullOrEmpty(messageText) && !string.IsNullOrEmpty(groupInput.LastMessage?.File)) - { - messageText = Path.GetFileName(groupInput.LastMessage.File); - } - string userIdManage = null; foreach(var user in groupInput.Users) @@ -1268,8 +1260,8 @@ namespace ChatController.HauptKlassen } var avatar = Utils.AvatarToImageSourceConverter(groupInput.Avatar, groupInput.OnlyEmployees, groupInput.Users.Length > 2); - - contacts.Add(new Contact(groupInput.Name, avatar, messageText, formattedTime, groupInput.Oid, userIdManage, groupInput.OnlyEmployees, groupInput.Users.Length)); + + contacts.Add(new Contact(groupInput.Name, avatar, groupInput.LastMessage, formattedTime, groupInput.Oid, userIdManage, groupInput.OnlyEmployees, groupInput.Users.Length)); if(pShouldUpdateLastTimeStamp) { diff --git a/ChatController/LoginKlassen/ChatGruppenDaten.cs b/ChatController/LoginKlassen/ChatGruppenDaten.cs index bf726a6..1f9f934 100644 --- a/ChatController/LoginKlassen/ChatGruppenDaten.cs +++ b/ChatController/LoginKlassen/ChatGruppenDaten.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.IO; namespace ChatController.LoginKlassen { @@ -24,7 +25,30 @@ namespace ChatController.LoginKlassen public class GroupLatestMessage { public long Id { get; set; } - public string Text { get; set; } + private string _Text; + + public string Text + { + get + { + var result = _Text; + + if (string.IsNullOrEmpty(_Text) && !string.IsNullOrEmpty(File)) + { + result = Path.GetFileName(File); + } + + if (!string.IsNullOrEmpty(_Text) && _Text.Length > 25) + { + result = _Text.Substring(0, 25) + "..."; + } + + return result; + } + + set => _Text = value; + } + public string File { get; set; } public string OriginalFileName { get; set; } public string SmallerImage { get; set; }