Files
Chat/ChatController/ChatKlassen/Contact.cs

183 lines
5.4 KiB
C#
Raw Normal View History

2017-09-12 14:32:34 +02:00
using System;
using System.Collections.Generic;
2017-09-12 14:32:34 +02:00
using System.ComponentModel;
using System.Windows.Media;
using ChatController.Core;
using ChatController.LoginKlassen;
using ChatController.Utilities;
using Brush = System.Windows.Media.Brush;
2017-09-12 14:32:34 +02:00
namespace ChatController.ChatKlassen
{
public class Contact : INotifyPropertyChanged
2017-09-12 14:32:34 +02:00
{
2019-04-04 13:00:10 +02:00
public string Name { get; }
2017-09-12 14:32:34 +02:00
private GroupLatestMessage _ReceivedMessage;
public GroupLatestMessage ReceivedMessage
2017-09-12 14:32:34 +02:00
{
get => _ReceivedMessage;
2017-09-12 14:32:34 +02:00
set
{
2019-04-04 13:00:10 +02:00
_ReceivedMessage = value;
OnPropertyChanged(nameof(ReceivedMessage));
2017-09-12 14:32:34 +02:00
}
}
public bool IsNewMessage { get; set; }
private Brush _Color;
2017-09-12 14:32:34 +02:00
public Brush Color
{
get => _Color;
2019-04-04 13:00:10 +02:00
2017-09-12 14:32:34 +02:00
set
{
_Color = value;
2019-04-04 13:00:10 +02:00
OnPropertyChanged(nameof(Color));
2017-09-12 14:32:34 +02:00
}
}
private bool _HasUnreadMessages;
public bool HasUnreadMessages
2017-09-12 14:32:34 +02:00
{
get => _HasUnreadMessages;
2017-09-12 14:32:34 +02:00
set
{
_HasUnreadMessages = value;
var userId = OwnChatCache.GetInstance().LoggedOnUserId;
var rmUserId = ReceivedMessage?.UserId;
var isOwnMessage = rmUserId.HasValue && rmUserId.Value.Equals(userId);
if (_HasUnreadMessages && !isOwnMessage)
2017-09-12 14:32:34 +02:00
{
_Color = new BrushConverter().ConvertFromString("#ff5e00") as SolidColorBrush;
2017-09-12 14:32:34 +02:00
}
else
2017-09-12 14:32:34 +02:00
{
_Color = new SolidColorBrush(Colors.Gray);
2017-09-12 14:32:34 +02:00
}
OnPropertyChanged(nameof(HasUnreadMessages));
2019-04-04 13:00:10 +02:00
OnPropertyChanged(nameof(Color));
2017-09-12 14:32:34 +02:00
}
}
private ImageSource _Image;
public ImageSource Image
{
get => _Image;
set
{
if(!Equals(_Image, value))
{
_Image = value;
OnPropertyChanged(nameof(Image));
}
}
}
2017-09-12 14:32:34 +02:00
public DateTime TimeStamp { get; set; }
public int GroupId { get; }
public string UserIdManage { get; }
2017-09-12 14:32:34 +02:00
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)
2017-09-12 14:32:34 +02:00
{
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)
{
if(obj is Contact y)
2017-11-16 15:38:30 +01:00
{
if(ReferenceEquals(this, y))
{
return true;
}
if(GetType() != y.GetType())
{
return false;
}
return Name == y.Name &&
IsNewMessage == y.IsNewMessage &&
TimeStamp.Equals(y.TimeStamp) &&
GroupId == y.GroupId &&
UserIdManage == y.UserIdManage &&
IsChatMessageInputGridVisible == y.IsChatMessageInputGridVisible;
2017-11-16 15:38:30 +01:00
}
return false;
}
public override int GetHashCode()
{
unchecked
2017-11-16 15:38:30 +01:00
{
var hashCode = Name != null ? Name.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ GroupId;
hashCode = (hashCode * 397) ^ (UserIdManage != null ? UserIdManage.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ IsChatMessageInputGridVisible.GetHashCode();
return hashCode;
2017-11-16 15:38:30 +01:00
}
2017-09-12 14:32:34 +02:00
}
public SolidColorBrush AccentColorBrush { get; }
2017-09-12 14:32:34 +02:00
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
2019-04-04 13:00:10 +02:00
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
2017-09-12 14:32:34 +02:00
}
}
}