From e8691c210dbd2ee79d9f06503402a8bd4a2e2720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Sun, 23 May 2021 12:28:45 +0800 Subject: [PATCH] No need for the wrapped Lazy instance in image loading. --- Flow.Launcher/ResultListBox.xaml | 2 +- Flow.Launcher/ViewModel/ResultViewModel.cs | 92 ++++++---------------- 2 files changed, 27 insertions(+), 67 deletions(-) diff --git a/Flow.Launcher/ResultListBox.xaml b/Flow.Launcher/ResultListBox.xaml index 2f9d06d81..0f70dce41 100644 --- a/Flow.Launcher/ResultListBox.xaml +++ b/Flow.Launcher/ResultListBox.xaml @@ -42,7 +42,7 @@ + Source="{Binding Image}" /> diff --git a/Flow.Launcher/ViewModel/ResultViewModel.cs b/Flow.Launcher/ViewModel/ResultViewModel.cs index c91bbb107..dd4b351c3 100644 --- a/Flow.Launcher/ViewModel/ResultViewModel.cs +++ b/Flow.Launcher/ViewModel/ResultViewModel.cs @@ -11,62 +11,12 @@ namespace Flow.Launcher.ViewModel { public class ResultViewModel : BaseModel { - public class LazyAsync : Lazy> - { - private readonly T defaultValue; - - private readonly Action _updateCallback; - public new T Value - { - get - { - if (!IsValueCreated) - { - _ = Exercute(); // manually use callback strategy - - return defaultValue; - } - - if (!base.Value.IsCompletedSuccessfully) - return defaultValue; - - return base.Value.Result; - - // If none of the variables captured by the local function are captured by other lambdas, - // the compiler can avoid heap allocations. - async ValueTask Exercute() - { - await base.Value.ConfigureAwait(false); - _updateCallback(); - } - - } - } - public LazyAsync(Func> factory, T defaultValue, Action updateCallback) : base(factory) - { - if (defaultValue != null) - { - this.defaultValue = defaultValue; - } - - _updateCallback = updateCallback; - } - } - public ResultViewModel(Result result, Settings settings) { if (result != null) { Result = result; - - Image = new LazyAsync( - SetImage, - ImageLoader.DefaultImage, - () => - { - OnPropertyChanged(nameof(Image)); - }); - } + } Settings = settings; } @@ -85,44 +35,54 @@ namespace Flow.Launcher.ViewModel ? Result.SubTitle : Result.SubTitleToolTip; - public LazyAsync Image { get; set; } + private bool ImageLoaded; - private async ValueTask SetImage() + private ImageSource image = ImageLoader.DefaultImage; + + public ImageSource Image + { + get + { + if (!ImageLoaded) + { + ImageLoaded = true; + LoadImage(); + } + return image; + } + private set => image = value; + } + private async void LoadImage() { var imagePath = Result.IcoPath; if (string.IsNullOrEmpty(imagePath) && Result.Icon != null) { try { - return Result.Icon(); + Image = Result.Icon(); + return; } catch (Exception e) { Log.Exception($"|ResultViewModel.Image|IcoPath is empty and exception when calling Icon() for result <{Result.Title}> of plugin <{Result.PluginDirectory}>", e); - return ImageLoader.DefaultImage; } } 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); + Image = ImageLoader.Load(imagePath); + return; + } - return await Task.Run(() => ImageLoader.Load(imagePath)); + Image = await Task.Run(() => ImageLoader.Load(imagePath)).ConfigureAwait(false); } public Result Result { get; } public override bool Equals(object obj) { - var r = obj as ResultViewModel; - if (r != null) - { - return Result.Equals(r.Result); - } - else - { - return false; - } + return obj is ResultViewModel r && Result.Equals(r.Result); } public override int GetHashCode()