2020-11-13 21:51:57 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2023-07-24 18:04:56 +02:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Linq;
|
2020-11-13 21:51:57 +01:00
|
|
|
|
using System.Windows.Media;
|
2023-07-24 18:04:56 +02:00
|
|
|
|
using ChatController.Extensions;
|
2020-11-13 21:51:57 +01:00
|
|
|
|
|
|
|
|
|
|
namespace ChatController.Core
|
|
|
|
|
|
{
|
|
|
|
|
|
public class OwnChatCache
|
|
|
|
|
|
{
|
2023-07-24 18:04:56 +02:00
|
|
|
|
public static string BaseUri { get; set; }
|
2022-05-30 20:48:14 +02:00
|
|
|
|
public long? LoggedOnUserId { get; set; }
|
|
|
|
|
|
|
2021-07-14 19:33:50 +02:00
|
|
|
|
private static readonly object _Lock = new object();
|
2020-11-13 21:51:57 +01:00
|
|
|
|
|
2022-07-28 19:18:15 +02:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-24 18:04:56 +02:00
|
|
|
|
|
|
|
|
|
|
private Dictionary<DefaultAvatarType, ImageSource> _DefaultAvatars = new Dictionary<DefaultAvatarType, ImageSource>();
|
|
|
|
|
|
|
|
|
|
|
|
private List<ProfilePictureObject> _AvatarCache = new List<ProfilePictureObject>();
|
|
|
|
|
|
|
2023-08-16 14:09:53 +02:00
|
|
|
|
private List<long> EmployeesWithDefaultAvatar = new List<long>();
|
|
|
|
|
|
private List<long> ClientsWithDefaultAvatar = new List<long>();
|
|
|
|
|
|
|
2023-07-24 18:04:56 +02:00
|
|
|
|
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);
|
2023-08-16 14:09:53 +02:00
|
|
|
|
if(userId.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
EmployeesWithDefaultAvatar.AddIfNotIn(userId.Value);
|
|
|
|
|
|
}
|
2023-07-24 18:04:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2023-08-16 14:09:53 +02:00
|
|
|
|
if(userId.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
ClientsWithDefaultAvatar.AddIfNotIn(userId.Value);
|
|
|
|
|
|
}
|
2023-07-24 18:04:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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"))
|
|
|
|
|
|
{
|
2023-08-16 14:09:53 +02:00
|
|
|
|
if(userId.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
EmployeesWithDefaultAvatar.AddIfNotIn(userId.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-24 18:04:56 +02:00
|
|
|
|
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"))
|
|
|
|
|
|
{
|
2023-08-16 14:09:53 +02:00
|
|
|
|
if(userId.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
ClientsWithDefaultAvatar.AddIfNotIn(userId.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-24 18:04:56 +02:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2023-08-16 14:09:53 +02:00
|
|
|
|
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;
|
2023-07-24 18:04:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-11-13 21:51:57 +01:00
|
|
|
|
// Key-> "group-127" oder "user-938"
|
2021-07-14 19:33:50 +02:00
|
|
|
|
private Dictionary<string, ImageSourceCacheStorage> _ImageSourceCache = new Dictionary<string, ImageSourceCacheStorage>();
|
2020-11-13 21:51:57 +01:00
|
|
|
|
|
2022-02-04 15:36:42 +01:00
|
|
|
|
private readonly Dictionary<long, ProfilePictureObject> _UserProfilePictures = new Dictionary<long, ProfilePictureObject>();
|
2021-09-07 19:32:10 +02:00
|
|
|
|
|
2020-11-13 21:51:57 +01:00
|
|
|
|
private static OwnChatCache _Instance;
|
|
|
|
|
|
|
|
|
|
|
|
private OwnChatCache() { }
|
|
|
|
|
|
|
|
|
|
|
|
public static OwnChatCache GetInstance()
|
|
|
|
|
|
{
|
|
|
|
|
|
return _Instance ?? (_Instance = new OwnChatCache());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-14 19:33:50 +02:00
|
|
|
|
public ImageSource GetImageSourceFromCache(string key, string name, CacheCategory cacheCategory)
|
2020-11-13 21:51:57 +01:00
|
|
|
|
{
|
2021-07-14 19:33:50 +02:00
|
|
|
|
lock(_Lock)
|
|
|
|
|
|
{
|
2022-07-28 19:18:15 +02:00
|
|
|
|
if(key is null || _ImageSourceCache is null || !_ImageSourceCache.ContainsKey(key))
|
2021-07-14 19:33:50 +02:00
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var imageSourceCacheObject = _ImageSourceCache[key].GetCachedImageSource(cacheCategory, name);
|
|
|
|
|
|
|
|
|
|
|
|
return imageSourceCacheObject?.Image;
|
|
|
|
|
|
}
|
2020-11-13 21:51:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-14 19:33:50 +02:00
|
|
|
|
public void AddImageToCache(string key, ImageSource imageSource, CacheCategory cacheCategory, string name)
|
2020-11-13 21:51:57 +01:00
|
|
|
|
{
|
2021-07-14 19:33:50 +02:00
|
|
|
|
lock(_Lock)
|
2020-11-13 21:51:57 +01:00
|
|
|
|
{
|
2022-07-28 19:18:15 +02:00
|
|
|
|
if(_ImageSourceCache is null)
|
2021-07-14 19:33:50 +02:00
|
|
|
|
{
|
|
|
|
|
|
_ImageSourceCache = new Dictionary<string, ImageSourceCacheStorage>();
|
|
|
|
|
|
}
|
2020-11-13 21:51:57 +01:00
|
|
|
|
|
2021-07-14 19:33:50 +02:00
|
|
|
|
if(!_ImageSourceCache.ContainsKey(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
_ImageSourceCache.Add(key, new ImageSourceCacheStorage());
|
|
|
|
|
|
}
|
2020-11-13 21:51:57 +01:00
|
|
|
|
|
2021-07-14 19:33:50 +02:00
|
|
|
|
_ImageSourceCache[key].AddImageSourceToCachedObjects(imageSource, cacheCategory, name);
|
|
|
|
|
|
}
|
2020-11-13 21:51:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-04 17:04:18 +02:00
|
|
|
|
public void AddProfilePictureToCache(string key, ImageSource imageSource, string profilePicturePath)
|
2020-11-13 21:51:57 +01:00
|
|
|
|
{
|
2021-07-14 19:33:50 +02:00
|
|
|
|
lock(_Lock)
|
2020-11-13 21:51:57 +01:00
|
|
|
|
{
|
2021-07-14 19:33:50 +02:00
|
|
|
|
if(!_ImageSourceCache.ContainsKey(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
_ImageSourceCache.Add(key, new ImageSourceCacheStorage());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-04 17:04:18 +02:00
|
|
|
|
_ImageSourceCache[key].SetCachedProfilePicture(imageSource, profilePicturePath);
|
2020-11-13 21:51:57 +01:00
|
|
|
|
}
|
2021-07-14 19:33:50 +02:00
|
|
|
|
}
|
2020-11-13 21:51:57 +01:00
|
|
|
|
|
2021-08-04 17:04:18 +02:00
|
|
|
|
public ImageSource GetCachedProfilePicture(string key, string uri)
|
2021-07-14 19:33:50 +02:00
|
|
|
|
{
|
|
|
|
|
|
lock(_Lock)
|
2020-11-13 21:51:57 +01:00
|
|
|
|
{
|
2021-08-04 17:04:18 +02:00
|
|
|
|
foreach (var kvp in _ImageSourceCache)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(uri) && uri.ToLower().Contains("ownchat") && kvp.Value.ProfilePicturePath == uri)
|
|
|
|
|
|
{
|
|
|
|
|
|
return kvp.Value.GetCachedProfilePicture();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-14 19:33:50 +02:00
|
|
|
|
return _ImageSourceCache.ContainsKey(key) ?
|
2021-08-04 17:04:18 +02:00
|
|
|
|
_ImageSourceCache[key].GetCachedProfilePicture() :
|
|
|
|
|
|
null;
|
2020-11-13 21:51:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-09-07 19:32:10 +02:00
|
|
|
|
|
|
|
|
|
|
public void AddUserProfilePictureToCache(ImageSource imageSource, long userId, string uri)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock(_Lock)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!_UserProfilePictures.ContainsKey(userId))
|
|
|
|
|
|
{
|
2023-07-24 18:04:56 +02:00
|
|
|
|
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}");
|
2021-09-07 19:32:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ProfilePictureObject GetUserProfilePictureFromCache(long userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock(_Lock)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _UserProfilePictures.ContainsKey(userId) ? _UserProfilePictures[userId] : null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-05-27 11:50:37 +02:00
|
|
|
|
|
|
|
|
|
|
public bool IsUserProfilePictureInCache(long userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock(_Lock)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _UserProfilePictures.ContainsKey(userId);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-09-07 19:32:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class ProfilePictureObject
|
|
|
|
|
|
{
|
2023-07-24 18:04:56 +02:00
|
|
|
|
public long? UserId { get; set; }
|
2021-09-07 19:32:10 +02:00
|
|
|
|
|
|
|
|
|
|
public string Uri { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public ImageSource ProfilePicture { get; set; }
|
|
|
|
|
|
|
2023-07-24 18:04:56 +02:00
|
|
|
|
public int? GroupId { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public ProfilePictureObject(long? userId, string uri, ImageSource profilePicture, int? groupId)
|
2021-09-07 19:32:10 +02:00
|
|
|
|
{
|
2023-07-24 18:04:56 +02:00
|
|
|
|
GroupId = groupId;
|
2021-09-07 19:32:10 +02:00
|
|
|
|
UserId = userId;
|
|
|
|
|
|
Uri = uri;
|
|
|
|
|
|
ProfilePicture = profilePicture;
|
|
|
|
|
|
}
|
2021-07-14 19:33:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class ImageSourceCacheStorage
|
|
|
|
|
|
{
|
|
|
|
|
|
private ImageSourceCacheObject _ProfilePicture;
|
2020-11-13 21:51:57 +01:00
|
|
|
|
|
2021-08-04 17:04:18 +02:00
|
|
|
|
public string ProfilePicturePath { get; set; }
|
|
|
|
|
|
|
2021-07-14 19:33:50 +02:00
|
|
|
|
public ImageSource GetCachedProfilePicture()
|
2020-11-13 21:51:57 +01:00
|
|
|
|
{
|
2021-07-14 19:33:50 +02:00
|
|
|
|
return _ProfilePicture?.ExpirationDate > DateTime.Now ? _ProfilePicture.Image : null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-04 17:04:18 +02:00
|
|
|
|
public void SetCachedProfilePicture(ImageSource avatarImageSource, string profilePicturePath)
|
2021-07-14 19:33:50 +02:00
|
|
|
|
{
|
2022-07-28 19:18:15 +02:00
|
|
|
|
if(!(avatarImageSource is null) && !string.IsNullOrWhiteSpace(profilePicturePath))
|
2020-11-13 21:51:57 +01:00
|
|
|
|
{
|
2021-08-04 17:04:18 +02:00
|
|
|
|
ProfilePicturePath = profilePicturePath;
|
2021-07-14 19:33:50 +02:00
|
|
|
|
_ProfilePicture = new ImageSourceCacheObject(avatarImageSource, CacheCategory.ProfilePicture);
|
2020-11-13 21:51:57 +01:00
|
|
|
|
}
|
2021-07-14 19:33:50 +02:00
|
|
|
|
}
|
2020-11-13 21:51:57 +01:00
|
|
|
|
|
2021-07-14 19:33:50 +02:00
|
|
|
|
public Dictionary<CacheCategory, Dictionary<string, ImageSourceCacheObject>> CachedObjects { get; set; }
|
2020-11-13 21:51:57 +01:00
|
|
|
|
|
2021-07-14 19:33:50 +02:00
|
|
|
|
public ImageSourceCacheStorage()
|
|
|
|
|
|
{
|
|
|
|
|
|
CachedObjects = new Dictionary<CacheCategory, Dictionary<string, ImageSourceCacheObject>>();
|
|
|
|
|
|
}
|
2020-11-13 21:51:57 +01:00
|
|
|
|
|
2021-07-14 19:33:50 +02:00
|
|
|
|
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;
|
2020-11-13 21:51:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-14 19:33:50 +02:00
|
|
|
|
public void AddImageSourceToCachedObjects(ImageSource imageSource, CacheCategory cacheCategory, string name)
|
2020-11-13 21:51:57 +01:00
|
|
|
|
{
|
2022-07-28 19:18:15 +02:00
|
|
|
|
if(CachedObjects is null)
|
2020-11-13 21:51:57 +01:00
|
|
|
|
{
|
2021-07-14 19:33:50 +02:00
|
|
|
|
CachedObjects = new Dictionary<CacheCategory, Dictionary<string, ImageSourceCacheObject>>();
|
2020-11-13 21:51:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-14 19:33:50 +02:00
|
|
|
|
var newImageSourceCacheObject = new ImageSourceCacheObject(imageSource, cacheCategory);
|
2020-11-13 21:51:57 +01:00
|
|
|
|
|
2021-07-14 19:33:50 +02:00
|
|
|
|
if(CachedObjects.ContainsKey(cacheCategory) )
|
2020-11-13 21:51:57 +01:00
|
|
|
|
{
|
2021-07-14 19:33:50 +02:00
|
|
|
|
if (CachedObjects[cacheCategory].ContainsKey(name))
|
|
|
|
|
|
{
|
|
|
|
|
|
CachedObjects[cacheCategory][name] = newImageSourceCacheObject;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
CachedObjects[cacheCategory].Add(name, newImageSourceCacheObject);
|
|
|
|
|
|
}
|
2020-11-13 21:51:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2022-07-28 19:18:15 +02:00
|
|
|
|
CachedObjects.Add(cacheCategory, new Dictionary<string, ImageSourceCacheObject> { {name, newImageSourceCacheObject } });
|
2020-11-13 21:51:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class ImageSourceCacheObject
|
|
|
|
|
|
{
|
2021-07-14 19:33:50 +02:00
|
|
|
|
public CacheCategory CacheCategory { get; }
|
|
|
|
|
|
|
2020-11-13 21:51:57 +01:00
|
|
|
|
public DateTime ExpirationDate { get; }
|
|
|
|
|
|
|
|
|
|
|
|
public ImageSource Image { get; }
|
|
|
|
|
|
|
2021-07-14 19:33:50 +02:00
|
|
|
|
public ImageSourceCacheObject(ImageSource imageSource, CacheCategory cacheCategory)
|
2020-11-13 21:51:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
ExpirationDate = DateTime.Now.AddYears(1);
|
2021-07-14 19:33:50 +02:00
|
|
|
|
Image = imageSource;
|
|
|
|
|
|
CacheCategory = cacheCategory;
|
2020-11-13 21:51:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-14 19:33:50 +02:00
|
|
|
|
public enum CacheCategory
|
2020-11-13 21:51:57 +01:00
|
|
|
|
{
|
2021-07-14 19:33:50 +02:00
|
|
|
|
ProfilePicture,
|
|
|
|
|
|
Thumbnail,
|
|
|
|
|
|
MessageAttachment
|
2020-11-13 21:51:57 +01:00
|
|
|
|
}
|
2023-07-24 18:04:56 +02:00
|
|
|
|
|
|
|
|
|
|
public enum DefaultAvatarType
|
|
|
|
|
|
{
|
|
|
|
|
|
Employee = 0,
|
|
|
|
|
|
EmployeeGroup = 1,
|
|
|
|
|
|
Client = 2,
|
|
|
|
|
|
ClientGroup = 3
|
|
|
|
|
|
}
|
2020-11-13 21:51:57 +01:00
|
|
|
|
}
|