Move initia and save from MainViewModel to App

This commit is contained in:
bao-qian
2017-02-12 16:57:24 +00:00
parent f7c9a12510
commit 00ea4f8701
3 changed files with 33 additions and 34 deletions

View File

@@ -16,7 +16,7 @@ namespace Wox.Infrastructure.Image
public static class ImageLoader public static class ImageLoader
{ {
private static readonly ImageCache ImageCache = new ImageCache(); private static readonly ImageCache ImageCache = new ImageCache();
private static readonly BinaryStorage<ConcurrentDictionary<string, int>> Storage; private static BinaryStorage<ConcurrentDictionary<string, int>> _storage;
private static readonly string[] ImageExtions = private static readonly string[] ImageExtions =
@@ -31,16 +31,38 @@ namespace Wox.Infrastructure.Image
}; };
static ImageLoader() public static void Initialize()
{ {
Storage = new BinaryStorage<ConcurrentDictionary<string, int>> ("Image"); _storage = new BinaryStorage<ConcurrentDictionary<string, int>> ("Image");
ImageCache.Usage = Storage.TryLoad(new ConcurrentDictionary<string, int>()); ImageCache.Usage = _storage.TryLoad(new ConcurrentDictionary<string, int>());
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() public static void Save()
{ {
ImageCache.Cleanup(); ImageCache.Cleanup();
Storage.Save(ImageCache.Usage); _storage.Save(ImageCache.Usage);
} }
private static ImageSource ShellIcon(string fileName) 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) public static ImageSource Load(string path)
{ {
ImageSource image; ImageSource image;

View File

@@ -5,6 +5,7 @@ using System.Windows;
using Wox.Core; using Wox.Core;
using Wox.Core.Plugin; using Wox.Core.Plugin;
using Wox.Helper; using Wox.Helper;
using Wox.Infrastructure;
using Wox.Infrastructure.Image; using Wox.Infrastructure.Image;
using Wox.Infrastructure.Logger; using Wox.Infrastructure.Logger;
using Wox.Infrastructure.UserSettings; using Wox.Infrastructure.UserSettings;
@@ -51,7 +52,7 @@ namespace Wox
API = new PublicAPIInstance(settingVM, mainVM); API = new PublicAPIInstance(settingVM, mainVM);
PluginManager.InitializePlugins(API); PluginManager.InitializePlugins(API);
ImageLoader.PreloadImages(); ImageLoader.Initialize();
Current.MainWindow = window; Current.MainWindow = window;
Current.MainWindow.Title = Infrastructure.Constant.Wox; Current.MainWindow.Title = Infrastructure.Constant.Wox;
@@ -132,6 +133,10 @@ namespace Wox
if (!_disposed) if (!_disposed)
{ {
Current.Dispatcher.Invoke(() => ((MainViewModel)Current.MainWindow?.DataContext)?.Save()); Current.Dispatcher.Invoke(() => ((MainViewModel)Current.MainWindow?.DataContext)?.Save());
PluginManager.Save();
ImageLoader.Save();
_disposed = true; _disposed = true;
} }
} }

View File

@@ -596,9 +596,6 @@ namespace Wox.ViewModel
_userSelectedRecordStorage.Save(); _userSelectedRecordStorage.Save();
_topMostRecordStorage.Save(); _topMostRecordStorage.Save();
PluginManager.Save();
ImageLoader.Save();
_saved = true; _saved = true;
} }
} }