80 lines
2.4 KiB
C#
80 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Avalonia.Media;
|
|
using Avalonia.Media.Imaging;
|
|
using System.Linq;
|
|
using System.IO;
|
|
using Axiom.Utils;
|
|
|
|
namespace PortfolioManager.Cache
|
|
{
|
|
public class ImageCache : IDisposable
|
|
{
|
|
public enum ImageType {BlueTriangleUp,GreenTriangleUp,GreenTriangleDown,RedTriangleUp,RedTriangleDown,YellowTriangleUp }
|
|
private Dictionary<ImageType,Bitmap> imageCache = new Dictionary<ImageType,Bitmap>();
|
|
private Object thisLock=new Object();
|
|
private static ImageCache imageCacheInstance=null;
|
|
|
|
private ImageCache()
|
|
{
|
|
String currentDirectory = Directory.GetCurrentDirectory();
|
|
String pathToAssets = currentDirectory;
|
|
if (!Directory.Exists(currentDirectory + "/Assets"))
|
|
{
|
|
pathToAssets = currentDirectory + "/publish" + "/Assets";
|
|
}
|
|
else
|
|
{
|
|
pathToAssets = currentDirectory + "/Assets";
|
|
}
|
|
MDTrace.WriteLine(LogLevel.DEBUG,$"Reading assets from {pathToAssets}");
|
|
imageCache.Add(ImageCache.ImageType.BlueTriangleUp,new Bitmap(pathToAssets+"/BlueTriangleUp.png"));
|
|
imageCache.Add(ImageCache.ImageType.GreenTriangleUp,new Bitmap(pathToAssets+"/GreenTriangleUp.png"));
|
|
imageCache.Add(ImageCache.ImageType.GreenTriangleDown,new Bitmap(pathToAssets+"/GreenTriangleDown.png"));
|
|
imageCache.Add(ImageCache.ImageType.RedTriangleUp,new Bitmap(pathToAssets+"/RedTriangleUp.png"));
|
|
imageCache.Add(ImageCache.ImageType.RedTriangleDown,new Bitmap(pathToAssets+"/RedTriangleDown.png"));
|
|
imageCache.Add(ImageCache.ImageType.YellowTriangleUp,new Bitmap(pathToAssets+"/YellowTriangleUp.png"));
|
|
}
|
|
|
|
public static ImageCache GetInstance()
|
|
{
|
|
lock (typeof(SymbolCache))
|
|
{
|
|
if (null == imageCacheInstance) imageCacheInstance = new ImageCache();
|
|
return imageCacheInstance;
|
|
}
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
lock (thisLock)
|
|
{
|
|
imageCache = new Dictionary<ImageType, Bitmap>();
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
lock (thisLock)
|
|
{
|
|
if (null == imageCacheInstance) return;
|
|
List<Bitmap> bitmaps = imageCache.Values.ToList();
|
|
foreach (Bitmap bitmap in bitmaps)
|
|
{
|
|
bitmap.Dispose();
|
|
}
|
|
imageCache = null;
|
|
imageCacheInstance = null;
|
|
}
|
|
}
|
|
|
|
public IImage GetImage(ImageCache.ImageType imageType)
|
|
{
|
|
lock(this)
|
|
{
|
|
return imageCache[imageType];
|
|
}
|
|
}
|
|
}
|
|
}
|