Files
Chat/ChatController/Core/OwnChatCache.cs

465 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Media;
using ChatController.Extensions;
namespace ChatController.Core
{
public class OwnChatCache
{
public static string BaseUri { get; set; }
public long? LoggedOnUserId { get; set; }
private static readonly object _Lock = new object();
private ImageSource _DocumentThumbnail;
public ImageSource DocumentThumbnail
{
get => _DocumentThumbnail;
set
{
lock(_Lock)
{
if(!(value is null) && _DocumentThumbnail is null)
{
_DocumentThumbnail = value;
}
}
}
}
private ImageSource _ImagePlaceholder;
public ImageSource ImagePlaceholder
{
get => _ImagePlaceholder;
set
{
lock(_Lock)
{
if(!(value is null) && _ImagePlaceholder is null)
{
_ImagePlaceholder = value;
}
}
}
}
private Dictionary<DefaultAvatarType, ImageSource> _DefaultAvatars = new Dictionary<DefaultAvatarType, ImageSource>();
private List<ProfilePictureObject> _AvatarCache = new List<ProfilePictureObject>();
private List<long> EmployeesWithDefaultAvatar = new List<long>();
private List<long> ClientsWithDefaultAvatar = new List<long>();
public void AddAvatar(string uri, ImageSource avatar, long? userId, int? groupId)
{
lock(_Lock)
{
if(uri is null || avatar is null)
{
return;
}
if(_AvatarCache is null)
{
_AvatarCache = new List<ProfilePictureObject>();
}
if(!string.IsNullOrWhiteSpace(BaseUri))
{
if(_DefaultAvatars is null)
{
_DefaultAvatars = new Dictionary<DefaultAvatarType, ImageSource>();
}
if(uri.Equals($"{BaseUri}/ownChat_avatar_employee.png"))
{
_DefaultAvatars.AddAndIgnoreDuplicates(DefaultAvatarType.Employee, avatar);
if(userId.HasValue)
{
EmployeesWithDefaultAvatar.AddIfNotIn(userId.Value);
}
}
if(uri.Equals($"{BaseUri}/ownChat_avatar_employee_group.png"))
{
_DefaultAvatars.AddAndIgnoreDuplicates(DefaultAvatarType.EmployeeGroup, avatar);
}
if(uri.Equals($"{BaseUri}/ownChat_avatar_client.png"))
{
_DefaultAvatars.AddAndIgnoreDuplicates(DefaultAvatarType.Client, avatar);
if(userId.HasValue)
{
ClientsWithDefaultAvatar.AddIfNotIn(userId.Value);
}
}
if(uri.Equals($"{BaseUri}/ownChat_avatar_client_group.png"))
{
_DefaultAvatars.AddAndIgnoreDuplicates(DefaultAvatarType.ClientGroup, avatar);
}
}
if(!_AvatarCache.Any(ppo => ppo.Uri.Equals(uri) && ppo.UserId == userId && ppo.GroupId == groupId))
{
_AvatarCache.Add(new ProfilePictureObject(userId, uri, avatar, groupId));
}
}
}
public ImageSource GetAvatar(string uri, long? userId, int? groupId)
{
lock(_Lock)
{
if(uri is null || _AvatarCache is null)
{
return null;
}
if(!string.IsNullOrWhiteSpace(BaseUri))
{
if(_DefaultAvatars is null)
{
_DefaultAvatars = new Dictionary<DefaultAvatarType, ImageSource>();
}
if(uri.Equals($"{BaseUri}/ownChat_avatar_employee.png"))
{
if(userId.HasValue)
{
EmployeesWithDefaultAvatar.AddIfNotIn(userId.Value);
}
return _DefaultAvatars[DefaultAvatarType.Employee];
}
if(uri.Equals($"{BaseUri}/ownChat_avatar_employee_group.png"))
{
return _DefaultAvatars[DefaultAvatarType.EmployeeGroup];
}
if(uri.Equals($"{BaseUri}/ownChat_avatar_client.png"))
{
if(userId.HasValue)
{
ClientsWithDefaultAvatar.AddIfNotIn(userId.Value);
}
return _DefaultAvatars[DefaultAvatarType.Client];
}
if(uri.Equals($"{BaseUri}/ownChat_avatar_client_group.png"))
{
return _DefaultAvatars[DefaultAvatarType.ClientGroup];
}
}
return _AvatarCache.FirstOrDefault(f => uri.Equals(f.Uri) && f.UserId == userId && f.GroupId == groupId)?.ProfilePicture;
}
}
/// <summary>
/// Gibt das Profilbild des Absenders einer Nachricht in Gruppenchats zurück
/// </summary>
/// <param name="userId">UserId des Absenders einer Nachricht</param>
/// <returns>Profilbild des Absenders einer Nachricht mit der angegebenen UserId</returns>
public ImageSource GetAvatarByUserId(long userId)
{
lock(_Lock)
{
var profilePicture = _AvatarCache.FirstOrDefault(f => f.UserId.HasValue && f.UserId.Value.Equals(userId))?.ProfilePicture;
if(!(profilePicture is null))
{
return profilePicture;
}
if(EmployeesWithDefaultAvatar.Contains(userId))
{
profilePicture = _DefaultAvatars[DefaultAvatarType.Employee];
}
if(ClientsWithDefaultAvatar.Contains(userId))
{
profilePicture = _DefaultAvatars[DefaultAvatarType.Client];
}
return profilePicture;
}
}
public bool IsAvatarInCache(string uri, long? userId, int? groupId)
{
lock(_Lock)
{
if(uri is null)
{
return false;
}
if(!string.IsNullOrWhiteSpace(BaseUri))
{
if(_DefaultAvatars is null)
{
_DefaultAvatars = new Dictionary<DefaultAvatarType, ImageSource>();
}
if(uri.Equals($"{BaseUri}/ownChat_avatar_employee.png"))
{
return _DefaultAvatars.ContainsKey(DefaultAvatarType.Employee);
}
if(uri.Equals($"{BaseUri}/ownChat_avatar_employee_group.png"))
{
return _DefaultAvatars.ContainsKey(DefaultAvatarType.EmployeeGroup);
}
if(uri.Equals($"{BaseUri}/ownChat_avatar_client.png"))
{
return _DefaultAvatars.ContainsKey(DefaultAvatarType.Client);
}
if(uri.Equals($"{BaseUri}/ownChat_avatar_client_group.png"))
{
return _DefaultAvatars.ContainsKey(DefaultAvatarType.ClientGroup);
}
}
return _AvatarCache?.Any(a => uri.Equals(a.Uri) && a.UserId == userId && a.GroupId == groupId) ?? false;
}
}
// Key-> "group-127" oder "user-938"
private Dictionary<string, ImageSourceCacheStorage> _ImageSourceCache = new Dictionary<string, ImageSourceCacheStorage>();
private readonly Dictionary<long, ProfilePictureObject> _UserProfilePictures = new Dictionary<long, ProfilePictureObject>();
private static OwnChatCache _Instance;
private OwnChatCache() { }
public static OwnChatCache GetInstance()
{
return _Instance ?? (_Instance = new OwnChatCache());
}
public ImageSource GetImageSourceFromCache(string key, string name, CacheCategory cacheCategory)
{
lock(_Lock)
{
if(key is null || _ImageSourceCache is null || !_ImageSourceCache.ContainsKey(key))
{
return null;
}
var imageSourceCacheObject = _ImageSourceCache[key].GetCachedImageSource(cacheCategory, name);
return imageSourceCacheObject?.Image;
}
}
public void AddImageToCache(string key, ImageSource imageSource, CacheCategory cacheCategory, string name)
{
lock(_Lock)
{
if(_ImageSourceCache is null)
{
_ImageSourceCache = new Dictionary<string, ImageSourceCacheStorage>();
}
if(!_ImageSourceCache.ContainsKey(key))
{
_ImageSourceCache.Add(key, new ImageSourceCacheStorage());
}
_ImageSourceCache[key].AddImageSourceToCachedObjects(imageSource, cacheCategory, name);
}
}
public void AddProfilePictureToCache(string key, ImageSource imageSource, string profilePicturePath)
{
lock(_Lock)
{
if(!_ImageSourceCache.ContainsKey(key))
{
_ImageSourceCache.Add(key, new ImageSourceCacheStorage());
}
_ImageSourceCache[key].SetCachedProfilePicture(imageSource, profilePicturePath);
}
}
public ImageSource GetCachedProfilePicture(string key, string uri)
{
lock(_Lock)
{
foreach (var kvp in _ImageSourceCache)
{
if(!string.IsNullOrWhiteSpace(uri) && uri.ToLower().Contains("ownchat") && kvp.Value.ProfilePicturePath == uri)
{
return kvp.Value.GetCachedProfilePicture();
}
}
return _ImageSourceCache.ContainsKey(key) ?
_ImageSourceCache[key].GetCachedProfilePicture() :
null;
}
}
public void AddUserProfilePictureToCache(ImageSource imageSource, long userId, string uri)
{
lock(_Lock)
{
if(!_UserProfilePictures.ContainsKey(userId))
{
Debug.WriteLine($"+++>OwnChatCache.AddUserProfilePictureToCache: Füge Avatar zum Cache hinzu {userId}");
_UserProfilePictures.Add(userId, new ProfilePictureObject(userId, uri, imageSource, null));
}
else
{
Debug.WriteLine($">>>OwnChatCache.AddUserProfilePictureToCache: Cache enthält bereits den Avatar von {userId}");
}
}
}
public ProfilePictureObject GetUserProfilePictureFromCache(long userId)
{
lock(_Lock)
{
return _UserProfilePictures.ContainsKey(userId) ? _UserProfilePictures[userId] : null;
}
}
public bool IsUserProfilePictureInCache(long userId)
{
lock(_Lock)
{
return _UserProfilePictures.ContainsKey(userId);
}
}
}
public class ProfilePictureObject
{
public long? UserId { get; set; }
public string Uri { get; set; }
public ImageSource ProfilePicture { get; set; }
public int? GroupId { get; set; }
public ProfilePictureObject(long? userId, string uri, ImageSource profilePicture, int? groupId)
{
GroupId = groupId;
UserId = userId;
Uri = uri;
ProfilePicture = profilePicture;
}
}
public class ImageSourceCacheStorage
{
private ImageSourceCacheObject _ProfilePicture;
public string ProfilePicturePath { get; set; }
public ImageSource GetCachedProfilePicture()
{
return _ProfilePicture?.ExpirationDate > DateTime.Now ? _ProfilePicture.Image : null;
}
public void SetCachedProfilePicture(ImageSource avatarImageSource, string profilePicturePath)
{
if(!(avatarImageSource is null) && !string.IsNullOrWhiteSpace(profilePicturePath))
{
ProfilePicturePath = profilePicturePath;
_ProfilePicture = new ImageSourceCacheObject(avatarImageSource, CacheCategory.ProfilePicture);
}
}
public Dictionary<CacheCategory, Dictionary<string, ImageSourceCacheObject>> CachedObjects { get; set; }
public ImageSourceCacheStorage()
{
CachedObjects = new Dictionary<CacheCategory, Dictionary<string, ImageSourceCacheObject>>();
}
public ImageSourceCacheObject GetCachedImageSource(CacheCategory cacheCategory, string name)
{
if(CachedObjects.ContainsKey(cacheCategory) && CachedObjects[cacheCategory].ContainsKey(name))
{
var imageSourceCacheObject = CachedObjects[cacheCategory][name];
return imageSourceCacheObject?.ExpirationDate > DateTime.Now ? imageSourceCacheObject : null;
}
return null;
}
public void AddImageSourceToCachedObjects(ImageSource imageSource, CacheCategory cacheCategory, string name)
{
if(CachedObjects is null)
{
CachedObjects = new Dictionary<CacheCategory, Dictionary<string, ImageSourceCacheObject>>();
}
var newImageSourceCacheObject = new ImageSourceCacheObject(imageSource, cacheCategory);
if(CachedObjects.ContainsKey(cacheCategory) )
{
if (CachedObjects[cacheCategory].ContainsKey(name))
{
CachedObjects[cacheCategory][name] = newImageSourceCacheObject;
}
else
{
CachedObjects[cacheCategory].Add(name, newImageSourceCacheObject);
}
}
else
{
CachedObjects.Add(cacheCategory, new Dictionary<string, ImageSourceCacheObject> { {name, newImageSourceCacheObject } });
}
}
}
public class ImageSourceCacheObject
{
public CacheCategory CacheCategory { get; }
public DateTime ExpirationDate { get; }
public ImageSource Image { get; }
public ImageSourceCacheObject(ImageSource imageSource, CacheCategory cacheCategory)
{
ExpirationDate = DateTime.Now.AddYears(1);
Image = imageSource;
CacheCategory = cacheCategory;
}
}
public enum CacheCategory
{
ProfilePicture,
Thumbnail,
MessageAttachment
}
public enum DefaultAvatarType
{
Employee = 0,
EmployeeGroup = 1,
Client = 2,
ClientGroup = 3
}
}