Files
Chat/ChatController/ChatKlassen/Contact.cs

174 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using ChatController.Core;
using ChatController.LoginKlassen;
using ChatController.Utilities;
using Brush = System.Windows.Media.Brush;
namespace ChatController.ChatKlassen
{
public class Contact : INotifyPropertyChanged
{
public string Name { get; }
private GroupLatestMessage _ReceivedMessage;
public GroupLatestMessage ReceivedMessage
{
get => _ReceivedMessage;
set
{
_ReceivedMessage = value;
OnPropertyChanged(nameof(ReceivedMessage));
}
}
public bool IsNewMessage { get; set; }
private Brush _Color;
public Brush Color
{
get => _Color;
set
{
_Color = value;
OnPropertyChanged(nameof(Color));
}
}
private bool _HasUnreadMessages;
public bool HasUnreadMessages
{
get => _HasUnreadMessages;
set
{
_HasUnreadMessages = value;
var userId = OwnChatCache.GetInstance().LoggedOnUserId;
var rmUserId = ReceivedMessage?.UserId;
var isOwnMessage = rmUserId.HasValue && rmUserId.Value.Equals(userId);
_Color = _HasUnreadMessages && !isOwnMessage ? new BrushConverter().ConvertFromString("#FF5A00") as SolidColorBrush : new SolidColorBrush(Colors.Gray);
OnPropertyChanged(nameof(HasUnreadMessages));
OnPropertyChanged(nameof(Color));
}
}
private ImageSource _Image;
public ImageSource Image
{
get => _Image;
set
{
if(Equals(_Image, value))
{
return;
}
_Image = value;
OnPropertyChanged(nameof(Image));
}
}
public DateTime TimeStamp { get; set; }
public int GroupId { get; }
public string UserIdManage { get; }
public bool IsChatMessageInputGridVisible { get; }
public Contact(string pName, GroupLatestMessage pLatestMessage, DateTime pTimeStamp, int pGroupId, string pUserIdManage, bool pOnlyEmployees, int pUserCount, bool isChatMessageInputGridVisible, string accentColorBrushString, string profilePicturePath, string key, Dictionary<long, string> userId2Uri)
{
Name = pName;
ReceivedMessage = pLatestMessage;
TimeStamp = pTimeStamp;
GroupId = pGroupId;
UserIdManage = pUserIdManage;
HasUnreadMessages = false;
IsChatMessageInputGridVisible = isChatMessageInputGridVisible;
Utils.DownloadUserProfilePicturesAsync(userId2Uri);
if (accentColorBrushString is null)
{
if(!(pUserIdManage is null))
{
accentColorBrushString = "3B7799";
}
else if(pOnlyEmployees && pUserCount > 2)
{
accentColorBrushString = "45993B";
}
else
{
accentColorBrushString = "000000";
}
}
AccentColorBrush = new BrushConverter().ConvertFromString($"#{accentColorBrushString}") as SolidColorBrush;
IsGroupChat = pUserCount > 2;
Utils.DownloadGroupProfilePictureAsync(profilePicturePath, key, delegate(ImageSource imageSource)
{
Image = imageSource;
});
}
public bool IsGroupChat { get; }
public override bool Equals(object obj)
{
return obj is Contact y && GroupId == y.GroupId;
}
public override int GetHashCode()
{
unchecked
{
var hashCode = Name?.GetHashCode() ?? 0;
hashCode = (hashCode * 397) ^ GroupId;
hashCode = (hashCode * 397) ^ (UserIdManage?.GetHashCode() ?? 0);
hashCode = (hashCode * 397) ^ IsChatMessageInputGridVisible.GetHashCode();
return hashCode;
}
}
public override string ToString()
{
return $"({GroupId}) {Name}";
}
public SolidColorBrush AccentColorBrush { get; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ContactDependencyObject : DependencyObject
{
public Contact Contact { get; set; }
public ContactDependencyObject(Contact contact)
{
Contact = contact;
}
}
}