Improved PictureStorage thread safety and more intuitive naming.

This commit is contained in:
Michael Bucari-Tovo 2021-08-15 13:39:06 -06:00
parent 7a90d9fba9
commit 27ae5facbe
5 changed files with 38 additions and 33 deletions

View File

@ -34,12 +34,10 @@ namespace FileManager
private static string getPath(PictureDefinition def)
=> Path.Combine(ImagesDirectory, $"{def.PictureId}{def.Size}.jpg");
static Task backgroundDownloader;
static PictureStorage()
{
backgroundDownloader = new Task(BackgroundDownloader);
backgroundDownloader.Start();
new Task(BackgroundDownloader, TaskCreationOptions.LongRunning)
.Start();
}
public static event EventHandler<PictureCachedEventArgs> PictureCached;
@ -48,6 +46,8 @@ namespace FileManager
private static Dictionary<PictureDefinition, byte[]> cache { get; } = new Dictionary<PictureDefinition, byte[]>();
private static Dictionary<PictureSize, byte[]> defaultImages { get; } = new Dictionary<PictureSize, byte[]>();
public static (bool isDefault, byte[] bytes) GetPicture(PictureDefinition def)
{
lock (cache)
{
if (cache.ContainsKey(def))
return (false, cache[def]);
@ -63,8 +63,11 @@ namespace FileManager
DownloadQueue.Add(def);
return (true, getDefaultImage(def.Size));
}
}
public static byte[] GetPictureSynchronously(PictureDefinition def)
{
lock (cache)
{
if (!cache.ContainsKey(def) || cache[def] == null)
{
@ -83,6 +86,7 @@ namespace FileManager
}
return cache[def];
}
}
public static void SetDefaultImage(PictureSize pictureSize, byte[] bytes)
=> defaultImages[pictureSize] = bytes;
@ -100,6 +104,7 @@ namespace FileManager
var bytes = downloadBytes(def);
saveFile(def, bytes);
lock (cache)
cache[def] = bytes;
PictureCached?.Invoke(nameof(PictureStorage), new PictureCachedEventArgs { Definition = def, Picture = bytes });

View File

@ -10,7 +10,7 @@ using System.Linq;
namespace LibationWinForms
{
internal class GridEntry : AsyncNotifyPropertyChanged, IObjectMemberComparable
internal class GridEntry : AsyncNotifyPropertyChanged, IMemberComparable
{
#region implementation properties
// hide from public fields from Data Source GUI with [Browsable(false)]

View File

@ -3,7 +3,7 @@ using System.Collections;
namespace LibationWinForms
{
internal interface IObjectMemberComparable
internal interface IMemberComparable
{
IComparer GetMemberComparer(Type memberType);
object GetMemberValue(string memberName);

View File

@ -3,7 +3,7 @@ using System.ComponentModel;
namespace LibationWinForms
{
internal class ObjectMemberComparer<T> : IComparer<T> where T : IObjectMemberComparable
internal class MemberComparer<T> : IComparer<T> where T : IMemberComparable
{
public ListSortDirection Direction { get; set; } = ListSortDirection.Ascending;
public string PropertyName { get; set; }

View File

@ -5,7 +5,7 @@ using System.ComponentModel;
namespace LibationWinForms
{
internal class SortableBindingList2<T> : BindingList<T> where T : IObjectMemberComparable
internal class SortableBindingList2<T> : BindingList<T> where T : IMemberComparable
{
private bool isSorted;
private ListSortDirection listSortDirection;
@ -14,7 +14,7 @@ namespace LibationWinForms
public SortableBindingList2() : base(new List<T>()) { }
public SortableBindingList2(IEnumerable<T> enumeration) : base(new List<T>(enumeration)) { }
private ObjectMemberComparer<T> Comparer { get; } = new();
private MemberComparer<T> Comparer { get; } = new();
protected override bool SupportsSortingCore => true;
protected override bool SupportsSearchingCore => true;
protected override bool IsSortedCore => isSorted;