Improved PictureStorage thread safety and more intuitive naming.
This commit is contained in:
parent
7a90d9fba9
commit
27ae5facbe
@ -34,12 +34,10 @@ namespace FileManager
|
|||||||
private static string getPath(PictureDefinition def)
|
private static string getPath(PictureDefinition def)
|
||||||
=> Path.Combine(ImagesDirectory, $"{def.PictureId}{def.Size}.jpg");
|
=> Path.Combine(ImagesDirectory, $"{def.PictureId}{def.Size}.jpg");
|
||||||
|
|
||||||
static Task backgroundDownloader;
|
|
||||||
|
|
||||||
static PictureStorage()
|
static PictureStorage()
|
||||||
{
|
{
|
||||||
backgroundDownloader = new Task(BackgroundDownloader);
|
new Task(BackgroundDownloader, TaskCreationOptions.LongRunning)
|
||||||
backgroundDownloader.Start();
|
.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static event EventHandler<PictureCachedEventArgs> PictureCached;
|
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<PictureDefinition, byte[]> cache { get; } = new Dictionary<PictureDefinition, byte[]>();
|
||||||
private static Dictionary<PictureSize, byte[]> defaultImages { get; } = new Dictionary<PictureSize, byte[]>();
|
private static Dictionary<PictureSize, byte[]> defaultImages { get; } = new Dictionary<PictureSize, byte[]>();
|
||||||
public static (bool isDefault, byte[] bytes) GetPicture(PictureDefinition def)
|
public static (bool isDefault, byte[] bytes) GetPicture(PictureDefinition def)
|
||||||
|
{
|
||||||
|
lock (cache)
|
||||||
{
|
{
|
||||||
if (cache.ContainsKey(def))
|
if (cache.ContainsKey(def))
|
||||||
return (false, cache[def]);
|
return (false, cache[def]);
|
||||||
@ -63,8 +63,11 @@ namespace FileManager
|
|||||||
DownloadQueue.Add(def);
|
DownloadQueue.Add(def);
|
||||||
return (true, getDefaultImage(def.Size));
|
return (true, getDefaultImage(def.Size));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static byte[] GetPictureSynchronously(PictureDefinition def)
|
public static byte[] GetPictureSynchronously(PictureDefinition def)
|
||||||
|
{
|
||||||
|
lock (cache)
|
||||||
{
|
{
|
||||||
if (!cache.ContainsKey(def) || cache[def] == null)
|
if (!cache.ContainsKey(def) || cache[def] == null)
|
||||||
{
|
{
|
||||||
@ -83,6 +86,7 @@ namespace FileManager
|
|||||||
}
|
}
|
||||||
return cache[def];
|
return cache[def];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void SetDefaultImage(PictureSize pictureSize, byte[] bytes)
|
public static void SetDefaultImage(PictureSize pictureSize, byte[] bytes)
|
||||||
=> defaultImages[pictureSize] = bytes;
|
=> defaultImages[pictureSize] = bytes;
|
||||||
@ -100,6 +104,7 @@ namespace FileManager
|
|||||||
|
|
||||||
var bytes = downloadBytes(def);
|
var bytes = downloadBytes(def);
|
||||||
saveFile(def, bytes);
|
saveFile(def, bytes);
|
||||||
|
lock (cache)
|
||||||
cache[def] = bytes;
|
cache[def] = bytes;
|
||||||
|
|
||||||
PictureCached?.Invoke(nameof(PictureStorage), new PictureCachedEventArgs { Definition = def, Picture = bytes });
|
PictureCached?.Invoke(nameof(PictureStorage), new PictureCachedEventArgs { Definition = def, Picture = bytes });
|
||||||
|
|||||||
@ -10,7 +10,7 @@ using System.Linq;
|
|||||||
|
|
||||||
namespace LibationWinForms
|
namespace LibationWinForms
|
||||||
{
|
{
|
||||||
internal class GridEntry : AsyncNotifyPropertyChanged, IObjectMemberComparable
|
internal class GridEntry : AsyncNotifyPropertyChanged, IMemberComparable
|
||||||
{
|
{
|
||||||
#region implementation properties
|
#region implementation properties
|
||||||
// hide from public fields from Data Source GUI with [Browsable(false)]
|
// hide from public fields from Data Source GUI with [Browsable(false)]
|
||||||
|
|||||||
@ -3,7 +3,7 @@ using System.Collections;
|
|||||||
|
|
||||||
namespace LibationWinForms
|
namespace LibationWinForms
|
||||||
{
|
{
|
||||||
internal interface IObjectMemberComparable
|
internal interface IMemberComparable
|
||||||
{
|
{
|
||||||
IComparer GetMemberComparer(Type memberType);
|
IComparer GetMemberComparer(Type memberType);
|
||||||
object GetMemberValue(string memberName);
|
object GetMemberValue(string memberName);
|
||||||
@ -3,7 +3,7 @@ using System.ComponentModel;
|
|||||||
|
|
||||||
namespace LibationWinForms
|
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 ListSortDirection Direction { get; set; } = ListSortDirection.Ascending;
|
||||||
public string PropertyName { get; set; }
|
public string PropertyName { get; set; }
|
||||||
@ -5,7 +5,7 @@ using System.ComponentModel;
|
|||||||
|
|
||||||
namespace LibationWinForms
|
namespace LibationWinForms
|
||||||
{
|
{
|
||||||
internal class SortableBindingList2<T> : BindingList<T> where T : IObjectMemberComparable
|
internal class SortableBindingList2<T> : BindingList<T> where T : IMemberComparable
|
||||||
{
|
{
|
||||||
private bool isSorted;
|
private bool isSorted;
|
||||||
private ListSortDirection listSortDirection;
|
private ListSortDirection listSortDirection;
|
||||||
@ -14,7 +14,7 @@ namespace LibationWinForms
|
|||||||
public SortableBindingList2() : base(new List<T>()) { }
|
public SortableBindingList2() : base(new List<T>()) { }
|
||||||
public SortableBindingList2(IEnumerable<T> enumeration) : base(new List<T>(enumeration)) { }
|
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 SupportsSortingCore => true;
|
||||||
protected override bool SupportsSearchingCore => true;
|
protected override bool SupportsSearchingCore => true;
|
||||||
protected override bool IsSortedCore => isSorted;
|
protected override bool IsSortedCore => isSorted;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user