Use a default image to list when the image hasn't been loaded in cache.

Co-authored-by: Bao-Qian <bao.github@outlook.com>
This commit is contained in:
弘韬 张 2020-11-15 20:45:00 +08:00
parent c89ee33c5b
commit dc982e277f
4 changed files with 39 additions and 16 deletions

View file

@ -26,7 +26,7 @@ namespace Flow.Launcher.Infrastructure.Image
private const int MaxCached = 50;
public ConcurrentDictionary<string, ImageUsage> Data { get; private set; } = new ConcurrentDictionary<string, ImageUsage>();
private const int permissibleFactor = 2;
public void Initialization(Dictionary<string, int> usage)
{
foreach (var key in usage.Keys)
@ -44,14 +44,14 @@ namespace Flow.Launcher.Infrastructure.Image
value.usage++;
return value.imageSource;
}
return null;
}
set
{
Data.AddOrUpdate(
path,
new ImageUsage(0, value),
path,
new ImageUsage(0, value),
(k, v) =>
{
v.imageSource = value;
@ -67,7 +67,8 @@ namespace Flow.Launcher.Infrastructure.Image
// To delete the images from the data dictionary based on the resizing of the Usage Dictionary.
foreach (var key in Data.OrderBy(x => x.Value.usage).Take(Data.Count - MaxCached).Select(x => x.Key))
foreach (var key in Data.Where(x => x.Key != Constant.MissingImgIcon)
.OrderBy(x => x.Value.usage).Take(Data.Count - MaxCached).Select(x => x.Key))
{
if (!(key.Equals(Constant.ErrorIcon) || key.Equals(Constant.DefaultIcon)))
{
@ -80,7 +81,7 @@ namespace Flow.Launcher.Infrastructure.Image
public bool ContainsKey(string key)
{
var contains = Data.ContainsKey(key);
var contains = Data.ContainsKey(key) && Data[key] != null;
return contains;
}

View file

@ -61,7 +61,7 @@ namespace Flow.Launcher.Infrastructure.Image
{
lock (_storage)
{
_storage.Save(ImageCache.Data.Select(x => (x.Key, x.Value.usage)).ToDictionary(x => x.Key, y => y.usage));
_storage.Save(ImageCache.Data.Select(x => (x.Key, x.Value.usage)).ToDictionary(x => x.Key, x => x.usage));
}
}
@ -211,6 +211,15 @@ namespace Flow.Launcher.Infrastructure.Image
option);
}
public static bool CacheContainImage(string path)
{
return ImageCache.ContainsKey(path);
}
public static ImageSource LoadDefault(bool loadFullImage = false)
{
return LoadInternal(Constant.MissingImgIcon, loadFullImage).ImageSource;
}
public static ImageSource Load(string path, bool loadFullImage = false)
{
var imageResult = LoadInternal(path, loadFullImage);

View file

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
@ -26,18 +27,18 @@ namespace Flow.Launcher.ViewModel
public Settings Settings { get; private set; }
public Visibility ShowOpenResultHotkey => Settings.ShowOpenResultHotkey ? Visibility.Visible : Visibility.Hidden;
public Visibility ShowOpenResultHotkey => Settings.ShowOpenResultHotkey ? Visibility.Visible : Visibility.Hidden;
public string OpenResultModifiers => Settings.OpenResultModifiers;
public string ShowTitleToolTip => string.IsNullOrEmpty(Result.TitleToolTip)
? Result.Title
? Result.Title
: Result.TitleToolTip;
public string ShowSubTitleToolTip => string.IsNullOrEmpty(Result.SubTitleToolTip)
? Result.SubTitle
? Result.SubTitle
: Result.SubTitleToolTip;
public Lazy<ImageSource> Image { get; set; }
private ImageSource SetImage
@ -57,9 +58,20 @@ namespace Flow.Launcher.ViewModel
imagePath = Constant.MissingImgIcon;
}
}
// will get here either when icoPath has value\icon delegate is null\when had exception in delegate
return ImageLoader.Load(imagePath);
if (ImageLoader.CacheContainImage(imagePath))
// will get here either when icoPath has value\icon delegate is null\when had exception in delegate
return ImageLoader.Load(imagePath);
else
{
Task.Run(() =>
{
Image = new Lazy<ImageSource>(() => ImageLoader.Load(imagePath));
OnPropertyChanged(nameof(Image));
});
return ImageLoader.LoadDefault();
}
}
}
@ -78,7 +90,7 @@ namespace Flow.Launcher.ViewModel
}
}
public override int GetHashCode()
{
return Result.GetHashCode();

View file

@ -201,9 +201,10 @@ namespace Flow.Launcher.ViewModel
var newResults = newRawResults.Select(r => new ResultViewModel(r, _settings)).ToList();
return results.Where(r => r.Result.PluginID != resultId)
.Concat(newResults)
.Concat(results.Intersect(newResults).Union(newResults))
.OrderByDescending(r => r.Result.Score)
.ToList();
}