using System; using System.Collections.Generic; using System.Windows.Media; namespace ChatController.Core { public class OwnChatCache { 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; } } } } // Key-> "group-127" oder "user-938" private Dictionary _ImageSourceCache = new Dictionary(); private readonly Dictionary _UserProfilePictures = new Dictionary(); 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(); } 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)) { _UserProfilePictures.Add(userId, new ProfilePictureObject(userId, uri, imageSource)); } } } 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 ProfilePictureObject(long userId, string uri, ImageSource profilePicture) { 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> CachedObjects { get; set; } public ImageSourceCacheStorage() { CachedObjects = new Dictionary>(); } 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>(); } 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 { {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 } }