2020-11-13 21:51:57 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
|
|
|
|
|
|
namespace ChatController.Core
|
|
|
|
|
|
{
|
|
|
|
|
|
public class OwnChatCache
|
|
|
|
|
|
{
|
2021-07-14 19:33:50 +02:00
|
|
|
|
private static readonly object _Lock = new object();
|
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
|
|
|
|
|
2021-09-07 19:32:10 +02:00
|
|
|
|
private Dictionary<long, ProfilePictureObject> _UserProfilePictures = new Dictionary<long, ProfilePictureObject>();
|
|
|
|
|
|
|
2020-11-13 21:51:57 +01:00
|
|
|
|
private static OwnChatCache _Instance;
|
|
|
|
|
|
|
|
|
|
|
|
private OwnChatCache() { }
|
|
|
|
|
|
|
|
|
|
|
|
public static OwnChatCache GetInstance()
|
|
|
|
|
|
{
|
|
|
|
|
|
return _Instance ?? (_Instance = new OwnChatCache());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ClearAll()
|
|
|
|
|
|
{
|
|
|
|
|
|
ClearImageSources();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ClearImageSources()
|
|
|
|
|
|
{
|
2021-07-14 19:33:50 +02:00
|
|
|
|
lock(_Lock)
|
|
|
|
|
|
{
|
|
|
|
|
|
_ImageSourceCache.Clear();
|
|
|
|
|
|
}
|
2020-11-13 21:51:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(key == null || _ImageSourceCache == null || !_ImageSourceCache.ContainsKey(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
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
|
|
|
|
{
|
2021-07-14 19:33:50 +02:00
|
|
|
|
if(_ImageSourceCache == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_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))
|
|
|
|
|
|
{
|
|
|
|
|
|
_UserProfilePictures.Add(userId, new ProfilePictureObject(userId, uri, imageSource));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ProfilePictureObject GetUserProfilePictureFromCache(long userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock(_Lock)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _UserProfilePictures.ContainsKey(userId) ? _UserProfilePictures[userId] : null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
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
|
|
|
|
{
|
2021-08-04 17:04:18 +02:00
|
|
|
|
if(avatarImageSource != 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
|
|
|
|
{
|
2021-07-14 19:33:50 +02:00
|
|
|
|
if(CachedObjects == 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
|
|
|
|
|
|
{
|
2021-07-14 19:33:50 +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
|
|
|
|
}
|
|
|
|
|
|
}
|