From 00ea4f870196bdbf91ce30c5b411d1fb4e965463 Mon Sep 17 00:00:00 2001 From: bao-qian Date: Sun, 12 Feb 2017 16:57:24 +0000 Subject: [PATCH] Move initia and save from MainViewModel to App --- Wox.Infrastructure/Image/ImageLoader.cs | 57 ++++++++++++------------- Wox/App.xaml.cs | 7 ++- Wox/ViewModel/MainViewModel.cs | 3 -- 3 files changed, 33 insertions(+), 34 deletions(-) diff --git a/Wox.Infrastructure/Image/ImageLoader.cs b/Wox.Infrastructure/Image/ImageLoader.cs index d99cf789d2..791cf1dd30 100644 --- a/Wox.Infrastructure/Image/ImageLoader.cs +++ b/Wox.Infrastructure/Image/ImageLoader.cs @@ -16,7 +16,7 @@ namespace Wox.Infrastructure.Image public static class ImageLoader { private static readonly ImageCache ImageCache = new ImageCache(); - private static readonly BinaryStorage> Storage; + private static BinaryStorage> _storage; private static readonly string[] ImageExtions = @@ -31,16 +31,38 @@ namespace Wox.Infrastructure.Image }; - static ImageLoader() + public static void Initialize() { - Storage = new BinaryStorage> ("Image"); - ImageCache.Usage = Storage.TryLoad(new ConcurrentDictionary()); + _storage = new BinaryStorage> ("Image"); + ImageCache.Usage = _storage.TryLoad(new ConcurrentDictionary()); + + foreach (var icon in new[] { Constant.DefaultIcon, Constant.ErrorIcon }) + { + ImageSource img = new BitmapImage(new Uri(icon)); + img.Freeze(); + ImageCache[icon] = img; + } + Task.Run(() => + { + Stopwatch.Normal("|ImageLoader.Initialize|Preload images cost", () => + { + ImageCache.Usage.AsParallel().Where(i => !ImageCache.ContainsKey(i.Key)).ForAll(i => + { + var img = Load(i.Key); + if (img != null) + { + ImageCache[i.Key] = img; + } + }); + }); + Log.Info($"|ImageLoader.Initialize|Number of preload images is <{ImageCache.Usage.Count}>"); + }); } public static void Save() { ImageCache.Cleanup(); - Storage.Save(ImageCache.Usage); + _storage.Save(ImageCache.Usage); } private static ImageSource ShellIcon(string fileName) @@ -81,31 +103,6 @@ namespace Wox.Infrastructure.Image } } - public static void PreloadImages() - { - foreach (var icon in new[] { Constant.DefaultIcon, Constant.ErrorIcon }) - { - ImageSource img = new BitmapImage(new Uri(icon)); - img.Freeze(); - ImageCache[icon] = img; - } - Task.Run(() => - { - Stopwatch.Normal("|ImageLoader.PreLoadImages|Preload images cost", () => - { - ImageCache.Usage.AsParallel().Where(i => !ImageCache.ContainsKey(i.Key)).ForAll(i => - { - var img = Load(i.Key); - if (img != null) - { - ImageCache[i.Key] = img; - } - }); - }); - Log.Info($"|ImageLoader.PreLoadImages|Number of preload images is <{ImageCache.Usage.Count}>"); - }); - } - public static ImageSource Load(string path) { ImageSource image; diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index 12d07f2556..f2e61bd3bf 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -5,6 +5,7 @@ using System.Windows; using Wox.Core; using Wox.Core.Plugin; using Wox.Helper; +using Wox.Infrastructure; using Wox.Infrastructure.Image; using Wox.Infrastructure.Logger; using Wox.Infrastructure.UserSettings; @@ -51,7 +52,7 @@ namespace Wox API = new PublicAPIInstance(settingVM, mainVM); PluginManager.InitializePlugins(API); - ImageLoader.PreloadImages(); + ImageLoader.Initialize(); Current.MainWindow = window; Current.MainWindow.Title = Infrastructure.Constant.Wox; @@ -132,6 +133,10 @@ namespace Wox if (!_disposed) { Current.Dispatcher.Invoke(() => ((MainViewModel)Current.MainWindow?.DataContext)?.Save()); + + PluginManager.Save(); + ImageLoader.Save(); + _disposed = true; } } diff --git a/Wox/ViewModel/MainViewModel.cs b/Wox/ViewModel/MainViewModel.cs index da5757ea62..1c7cad6e77 100644 --- a/Wox/ViewModel/MainViewModel.cs +++ b/Wox/ViewModel/MainViewModel.cs @@ -596,9 +596,6 @@ namespace Wox.ViewModel _userSelectedRecordStorage.Save(); _topMostRecordStorage.Save(); - PluginManager.Save(); - ImageLoader.Save(); - _saved = true; } }