remove storage dependency from jsonstorage

This commit is contained in:
bao-qian
2017-02-07 00:21:39 +00:00
parent b6a7e049e6
commit f7c9a12510
11 changed files with 67 additions and 71 deletions

View File

@@ -132,9 +132,8 @@ namespace Wox.Plugin.WebSearch
public void Init(PluginInitContext context) public void Init(PluginInitContext context)
{ {
var pluginDirectory = context.CurrentPluginMetadata.PluginDirectory;
_context = context; _context = context;
var pluginDirectory = _context.CurrentPluginMetadata.PluginDirectory;
var bundledImagesDirectory = Path.Combine(pluginDirectory, Images); var bundledImagesDirectory = Path.Combine(pluginDirectory, Images);
ImagesDirectory = Path.Combine(_context.CurrentPluginMetadata.PluginDirectory, Images); ImagesDirectory = Path.Combine(_context.CurrentPluginMetadata.PluginDirectory, Images);
Helper.ValidateDataDirectory(bundledImagesDirectory, ImagesDirectory); Helper.ValidateDataDirectory(bundledImagesDirectory, ImagesDirectory);

View File

@@ -4,7 +4,7 @@ namespace Wox.Plugin.WebSearch
{ {
public class SettingsViewModel public class SettingsViewModel
{ {
private readonly JsonStrorage<Settings> _storage; private readonly PluginJsonStorage<Settings> _storage;
public SettingsViewModel() public SettingsViewModel()
{ {

View File

@@ -55,5 +55,13 @@ namespace Wox.Infrastructure
} }
} }
} }
public static void ValidateDirectory(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
} }
} }

View File

@@ -11,21 +11,21 @@ namespace Wox.Infrastructure.Storage
/// <summary> /// <summary>
/// Stroage object using binary data /// Stroage object using binary data
/// Normally, it has better performance, but not readable /// Normally, it has better performance, but not readable
/// You MUST mark implement class as Serializable
/// </summary> /// </summary>
public class BinaryStorage<T> : Storage<T> public class BinaryStorage<T>
{ {
public BinaryStorage(string filename) public BinaryStorage(string filename)
{ {
FileSuffix = ".cache"; const string directoryName = "Cache";
DirectoryName = "Cache"; var directoryPath = Path.Combine(Constant.DataDirectory, directoryName);
DirectoryPath = Path.Combine(DirectoryPath, DirectoryName); Helper.ValidateDirectory(directoryPath);
FileName = filename;
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
ValidateDirectory(); const string fileSuffix = ".cache";
FilePath = Path.Combine(directoryPath, $"{filename}{fileSuffix}");
} }
public string FilePath { get; }
public T TryLoad(T defaultData) public T TryLoad(T defaultData)
{ {
if (File.Exists(FilePath)) if (File.Exists(FilePath))

View File

@@ -1,6 +1,6 @@
using System.IO; using System;
using System.IO;
using Newtonsoft.Json; using Newtonsoft.Json;
using Wox.Infrastructure;
using Wox.Infrastructure.Logger; using Wox.Infrastructure.Logger;
namespace Wox.Infrastructure.Storage namespace Wox.Infrastructure.Storage
@@ -8,19 +8,19 @@ namespace Wox.Infrastructure.Storage
/// <summary> /// <summary>
/// Serialize object using json format. /// Serialize object using json format.
/// </summary> /// </summary>
public class JsonStrorage<T> : Storage<T> where T : new() public class JsonStrorage<T>
{ {
private readonly JsonSerializerSettings _serializerSettings; private readonly JsonSerializerSettings _serializerSettings;
private T _data;
// need a new directory name
public const string DirectoryName = "Settings";
public const string FileSuffix = ".json";
public string FilePath { get; set; }
public string DirectoryPath { get; set; }
internal JsonStrorage() internal JsonStrorage()
{ {
FileSuffix = ".json";
DirectoryName = "Settings";
DirectoryPath = Path.Combine(Constant.DataDirectory, DirectoryName);
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
ValidateDirectory();
// use property initialization instead of DefaultValueAttribute // use property initialization instead of DefaultValueAttribute
// easier and flexible for default value of object // easier and flexible for default value of object
_serializerSettings = new JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
@@ -48,14 +48,14 @@ namespace Wox.Infrastructure.Storage
{ {
LoadDefault(); LoadDefault();
} }
return Data; return _data;
} }
private void Deserialize(string searlized) private void Deserialize(string searlized)
{ {
try try
{ {
Data = JsonConvert.DeserializeObject<T>(searlized, _serializerSettings); _data = JsonConvert.DeserializeObject<T>(searlized, _serializerSettings);
} }
catch (JsonSerializationException e) catch (JsonSerializationException e)
{ {
@@ -66,13 +66,13 @@ namespace Wox.Infrastructure.Storage
public void LoadDefault() public void LoadDefault()
{ {
Data = JsonConvert.DeserializeObject<T>("{}", _serializerSettings); _data = JsonConvert.DeserializeObject<T>("{}", _serializerSettings);
Save(); Save();
} }
public void Save() public void Save()
{ {
string serialized = JsonConvert.SerializeObject(Data, Formatting.Indented); string serialized = JsonConvert.SerializeObject(_data, Formatting.Indented);
File.WriteAllText(FilePath, serialized); File.WriteAllText(FilePath, serialized);
} }
} }

View File

@@ -6,14 +6,13 @@ namespace Wox.Infrastructure.Storage
{ {
public PluginJsonStorage() public PluginJsonStorage()
{ {
DirectoryName = Constant.Plugins;
// C# releated, add python releated below // C# releated, add python releated below
var assemblyName = DataType.Assembly.GetName().Name; var dataType = typeof(T);
DirectoryPath = Path.Combine(DirectoryPath, DirectoryName, assemblyName); var assemblyName = typeof(T).Assembly.GetName().Name;
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix); DirectoryPath = Path.Combine(Constant.DataDirectory, DirectoryName, Constant.Plugins, assemblyName);
Helper.ValidateDirectory(DirectoryPath);
ValidateDirectory(); FilePath = Path.Combine(DirectoryPath, $"{dataType.Name}{FileSuffix}");
} }
} }
} }

View File

@@ -1,31 +0,0 @@
using System;
using System.IO;
namespace Wox.Infrastructure.Storage
{
public class Storage<T>
{
protected T Data;
protected Type DataType { get; }
public string FileName { get; set; }
public string FilePath { get; set; }
public string FileSuffix { get; set; }
public string DirectoryPath { get; set; }
public string DirectoryName { get; set; }
protected Storage()
{
DataType = typeof (T);
FileName = DataType.Name;
DirectoryPath = Constant.DataDirectory;
}
protected void ValidateDirectory()
{
if (!Directory.Exists(DirectoryPath))
{
Directory.CreateDirectory(DirectoryPath);
}
}
}
}

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wox.Infrastructure.Storage
{
class WoxJsonStorage<T> : JsonStrorage<T> where T : new()
{
public WoxJsonStorage()
{
var directoryPath = Path.Combine(Constant.DataDirectory, DirectoryName);
Helper.ValidateDirectory(directoryPath);
var filename = typeof(T).Name;
FilePath = Path.Combine(directoryPath, $"{filename}{FileSuffix}");
}
}
}

View File

@@ -75,10 +75,10 @@
<Compile Include="Logger\Log.cs" /> <Compile Include="Logger\Log.cs" />
<Compile Include="Storage\ISavable.cs" /> <Compile Include="Storage\ISavable.cs" />
<Compile Include="Storage\PluginJsonStorage.cs" /> <Compile Include="Storage\PluginJsonStorage.cs" />
<Compile Include="Storage\Storage.cs" />
<Compile Include="Stopwatch.cs" /> <Compile Include="Stopwatch.cs" />
<Compile Include="Storage\BinaryStorage.cs" /> <Compile Include="Storage\BinaryStorage.cs" />
<Compile Include="Storage\JsonStorage.cs" /> <Compile Include="Storage\JsonStorage.cs" />
<Compile Include="Storage\WoxJsonStorage.cs" />
<Compile Include="StringMatcher.cs" /> <Compile Include="StringMatcher.cs" />
<Compile Include="Http\Http.cs" /> <Compile Include="Http\Http.cs" />
<Compile Include="FuzzyMatcher.cs" /> <Compile Include="FuzzyMatcher.cs" />

View File

@@ -29,9 +29,9 @@ namespace Wox.ViewModel
private Query _lastQuery; private Query _lastQuery;
private string _queryTextBeforeLeaveResults; private string _queryTextBeforeLeaveResults;
private readonly JsonStrorage<History> _historyItemsStorage; private readonly WoxJsonStorage<History> _historyItemsStorage;
private readonly JsonStrorage<UserSelectedRecord> _userSelectedRecordStorage; private readonly WoxJsonStorage<UserSelectedRecord> _userSelectedRecordStorage;
private readonly JsonStrorage<TopMostRecord> _topMostRecordStorage; private readonly WoxJsonStorage<TopMostRecord> _topMostRecordStorage;
private readonly Settings _settings; private readonly Settings _settings;
private readonly History _history; private readonly History _history;
private readonly UserSelectedRecord _userSelectedRecord; private readonly UserSelectedRecord _userSelectedRecord;
@@ -56,9 +56,9 @@ namespace Wox.ViewModel
_settings = settings; _settings = settings;
_historyItemsStorage = new JsonStrorage<History>(); _historyItemsStorage = new WoxJsonStorage<History>();
_userSelectedRecordStorage = new JsonStrorage<UserSelectedRecord>(); _userSelectedRecordStorage = new WoxJsonStorage<UserSelectedRecord>();
_topMostRecordStorage = new JsonStrorage<TopMostRecord>(); _topMostRecordStorage = new WoxJsonStorage<TopMostRecord>();
_history = _historyItemsStorage.Load(); _history = _historyItemsStorage.Load();
_userSelectedRecord = _userSelectedRecordStorage.Load(); _userSelectedRecord = _userSelectedRecordStorage.Load();
_topMostRecord = _topMostRecordStorage.Load(); _topMostRecord = _topMostRecordStorage.Load();

View File

@@ -19,11 +19,11 @@ namespace Wox.ViewModel
{ {
public class SettingWindowViewModel : BaseModel public class SettingWindowViewModel : BaseModel
{ {
private readonly JsonStrorage<Settings> _storage; private readonly WoxJsonStorage<Settings> _storage;
public SettingWindowViewModel() public SettingWindowViewModel()
{ {
_storage = new JsonStrorage<Settings>(); _storage = new WoxJsonStorage<Settings>();
Settings = _storage.Load(); Settings = _storage.Load();
Settings.PropertyChanged += (s, e) => Settings.PropertyChanged += (s, e) =>
{ {