Bilder werden im Cache gespeichert, um nicht alle 10 Sekunden neugeladen zu werden.
This commit is contained in:
@@ -76,6 +76,7 @@
|
||||
<Compile Include="Converter\BoolToVisibilityConverter.cs" />
|
||||
<Compile Include="Converter\StringEmptyToVisibilityConverter.cs" />
|
||||
<Compile Include="Converter\StringToBoolConverter.cs" />
|
||||
<Compile Include="Core\OwnChatCache.cs" />
|
||||
<Compile Include="Data\LookupResult.cs" />
|
||||
<Compile Include="Data\OwnChatApi.cs" />
|
||||
<Compile Include="Extensions\IDictionaryTExtensions.cs" />
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.ComponentModel;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using ChatController.Core;
|
||||
using ChatController.Utilities;
|
||||
using static System.Windows.HorizontalAlignment;
|
||||
|
||||
@@ -73,7 +74,16 @@ namespace ChatController.ChatKlassen
|
||||
|
||||
FilePath = pUrl;
|
||||
|
||||
Thumbnail = Utils.CreateImageSourceFromPath(pSmallerImagePath);
|
||||
var cache = OwnChatCache.GetInstance();
|
||||
|
||||
var imgSrc = cache.GetImageSourceFromCache($"group-{pGroupId}");
|
||||
|
||||
if(imgSrc == null)
|
||||
{
|
||||
imgSrc = Utils.CreateImageSourceFromPath(pSmallerImagePath);
|
||||
}
|
||||
|
||||
Thumbnail = imgSrc;
|
||||
|
||||
GroupId = pGroupId;
|
||||
|
||||
|
||||
133
ChatController/Core/OwnChatCache.cs
Normal file
133
ChatController/Core/OwnChatCache.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace ChatController.Core
|
||||
{
|
||||
public class OwnChatCache
|
||||
{
|
||||
// Key-> "group-127" oder "user-938"
|
||||
private Dictionary<string, ImageSourceCacheObject> _ImageSources = new Dictionary<string, ImageSourceCacheObject>();
|
||||
|
||||
// Key-> "group-127" oder "user-938"
|
||||
private Dictionary<string, BitmapCacheObject> _Bitmaps = new Dictionary<string, BitmapCacheObject>();
|
||||
|
||||
private static OwnChatCache _Instance;
|
||||
|
||||
private OwnChatCache() { }
|
||||
|
||||
public static OwnChatCache GetInstance()
|
||||
{
|
||||
return _Instance ?? (_Instance = new OwnChatCache());
|
||||
}
|
||||
|
||||
public void ClearAll()
|
||||
{
|
||||
ClearImageSources();
|
||||
ClearBitmaps();
|
||||
}
|
||||
|
||||
public void ClearImageSources()
|
||||
{
|
||||
_ImageSources.Clear();
|
||||
}
|
||||
|
||||
public void ClearBitmaps()
|
||||
{
|
||||
_Bitmaps.Clear();
|
||||
}
|
||||
|
||||
public ImageSource GetImageSourceFromCache(string key)
|
||||
{
|
||||
if(key == null || _ImageSources == null || !_ImageSources.ContainsKey(key))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var imageSourceCacheObject = _ImageSources[key];
|
||||
|
||||
var expirationDate = imageSourceCacheObject.ExpirationDate;
|
||||
|
||||
return expirationDate < DateTime.Now ? null : imageSourceCacheObject.Image;
|
||||
}
|
||||
|
||||
public void AddImageToCache(string key, ImageSource imageSource)
|
||||
{
|
||||
if(_ImageSources == null)
|
||||
{
|
||||
_ImageSources = new Dictionary<string, ImageSourceCacheObject>();
|
||||
}
|
||||
|
||||
var newImageSourceCacheObject = new ImageSourceCacheObject(imageSource);
|
||||
|
||||
if(_ImageSources.ContainsKey(key))
|
||||
{
|
||||
_ImageSources[key] = newImageSourceCacheObject;
|
||||
}
|
||||
else
|
||||
{
|
||||
_ImageSources.Add(key, newImageSourceCacheObject);
|
||||
}
|
||||
}
|
||||
|
||||
public Bitmap GetBitmapFromCache(string key)
|
||||
{
|
||||
if(key == null || _Bitmaps == null || !_Bitmaps.ContainsKey(key))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var bitmapCacheObject = _Bitmaps[key];
|
||||
|
||||
var expirationDate = bitmapCacheObject.ExpirationDate;
|
||||
|
||||
return expirationDate < DateTime.Now ? null : bitmapCacheObject.Bitmap;
|
||||
}
|
||||
|
||||
public void AddBitmapToCache(string key, Bitmap bitmap)
|
||||
{
|
||||
if(_Bitmaps == null)
|
||||
{
|
||||
_Bitmaps = new Dictionary<string, BitmapCacheObject>();
|
||||
}
|
||||
|
||||
var newBitmapCacheObject = new BitmapCacheObject(bitmap);
|
||||
|
||||
if(_Bitmaps.ContainsKey(key))
|
||||
{
|
||||
_Bitmaps[key] = newBitmapCacheObject;
|
||||
}
|
||||
else
|
||||
{
|
||||
_Bitmaps.Add(key, newBitmapCacheObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ImageSourceCacheObject
|
||||
{
|
||||
public DateTime ExpirationDate { get; }
|
||||
|
||||
public ImageSource Image { get; }
|
||||
|
||||
public ImageSourceCacheObject(ImageSource imageSource)
|
||||
{
|
||||
ExpirationDate = DateTime.Now.AddYears(1);
|
||||
Image = imageSource;
|
||||
}
|
||||
}
|
||||
|
||||
public class BitmapCacheObject
|
||||
{
|
||||
public DateTime ExpirationDate { get; }
|
||||
|
||||
public Bitmap Bitmap { get; }
|
||||
|
||||
public BitmapCacheObject(Bitmap bitmap)
|
||||
{
|
||||
ExpirationDate = DateTime.Now.AddYears(1);
|
||||
Bitmap = bitmap;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,20 +3,19 @@ using Newtonsoft.Json;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using ChatController.Core;
|
||||
using ChatController.LoginKlassen;
|
||||
using ChatController.Utilities;
|
||||
using ChatController.Utilities.Extensions;
|
||||
@@ -79,7 +78,6 @@ namespace ChatController.HauptKlassen
|
||||
}
|
||||
|
||||
// WebRequest
|
||||
// TODO: Auth-Token zum Header hinzufügen
|
||||
private ImageSource DownloadImage(string pUri)
|
||||
{
|
||||
try
|
||||
@@ -144,7 +142,6 @@ namespace ChatController.HauptKlassen
|
||||
|
||||
if (!string.IsNullOrEmpty(serverResponse))
|
||||
{
|
||||
Debug.WriteLine(serverResponse);
|
||||
_UserMessages = JsonConvert.DeserializeObject<UserMessages>(serverResponse);
|
||||
|
||||
if (_UserMessages.Response.HasMorePages)
|
||||
@@ -905,7 +902,7 @@ namespace ChatController.HauptKlassen
|
||||
|
||||
var imageName = Utils.GenerateTempName() + ".jpg";
|
||||
|
||||
System.Drawing.Image image = bitmap;
|
||||
Image image = bitmap;
|
||||
|
||||
AddMediumContextDerNachrichtView(Utils.ImageToByteArray(image), imageName, pGroupOid);
|
||||
|
||||
@@ -1147,11 +1144,27 @@ namespace ChatController.HauptKlassen
|
||||
}
|
||||
}
|
||||
|
||||
var avatar = Utils.AvatarToImageSourceConverter(groupInput.Avatar, groupInput.OnlyEmployees, groupInput.Users.Length > 2);
|
||||
var key = $"group-{groupInput.Oid}";
|
||||
|
||||
var profilePicture = Utils.ConvertAvatarToBitmap(groupInput.Avatar, groupInput.OnlyEmployees, groupInput.Users.Length > 2);
|
||||
var cacheInstance = OwnChatCache.GetInstance();
|
||||
|
||||
contacts.Add(new Contact(groupInput.Name, groupInput.LastMessage, formattedTime, groupInput.Oid, userIdManage, groupInput.OnlyEmployees, groupInput.Users.Length, groupInput.CanWrite, groupInput.AccentColor, profilePicture, avatar));
|
||||
var imgSrc = cacheInstance.GetImageSourceFromCache(key);
|
||||
|
||||
if(imgSrc == null)
|
||||
{
|
||||
imgSrc = Utils.AvatarToImageSourceConverter(groupInput.Avatar, groupInput.OnlyEmployees, groupInput.Users.Length > 2);
|
||||
cacheInstance.AddImageToCache(key, imgSrc);
|
||||
}
|
||||
|
||||
var bmp = cacheInstance.GetBitmapFromCache(key);
|
||||
|
||||
if(bmp == null)
|
||||
{
|
||||
bmp = Utils.ConvertAvatarToBitmap(groupInput.Avatar, groupInput.OnlyEmployees, groupInput.Users.Length > 2);
|
||||
cacheInstance.AddBitmapToCache(key, bmp);
|
||||
}
|
||||
|
||||
contacts.Add(new Contact(groupInput.Name, groupInput.LastMessage, formattedTime, groupInput.Oid, userIdManage, groupInput.OnlyEmployees, groupInput.Users.Length, groupInput.CanWrite, groupInput.AccentColor, bmp, imgSrc));
|
||||
|
||||
if(pShouldUpdateLastTimeStamp)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
@@ -36,6 +37,8 @@ namespace ChatController.Utilities
|
||||
|
||||
if(!string.IsNullOrWhiteSpace(linkToPicture))
|
||||
{
|
||||
Debug.WriteLine("Lade Bitmap herunter...");
|
||||
|
||||
var client = new RestClient(linkToPicture);
|
||||
|
||||
var request = new RestRequest(Method.GET)
|
||||
@@ -132,6 +135,8 @@ namespace ChatController.Utilities
|
||||
{
|
||||
if (!string.IsNullOrEmpty(pPathToImageFile))
|
||||
{
|
||||
Debug.WriteLine("Lade Bild herunter...");
|
||||
|
||||
Bitmap bitmap = null;
|
||||
|
||||
var client = new RestClient(pPathToImageFile);
|
||||
|
||||
Reference in New Issue
Block a user