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)
{
var pluginDirectory = context.CurrentPluginMetadata.PluginDirectory;
_context = context;
var pluginDirectory = _context.CurrentPluginMetadata.PluginDirectory;
var bundledImagesDirectory = Path.Combine(pluginDirectory, Images);
ImagesDirectory = Path.Combine(_context.CurrentPluginMetadata.PluginDirectory, Images);
Helper.ValidateDataDirectory(bundledImagesDirectory, ImagesDirectory);

View file

@ -4,7 +4,7 @@ namespace Wox.Plugin.WebSearch
{
public class SettingsViewModel
{
private readonly JsonStrorage<Settings> _storage;
private readonly PluginJsonStorage<Settings> _storage;
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>
/// Stroage object using binary data
/// Normally, it has better performance, but not readable
/// You MUST mark implement class as Serializable
/// </summary>
public class BinaryStorage<T> : Storage<T>
public class BinaryStorage<T>
{
public BinaryStorage(string filename)
{
FileSuffix = ".cache";
DirectoryName = "Cache";
DirectoryPath = Path.Combine(DirectoryPath, DirectoryName);
FileName = filename;
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
const string directoryName = "Cache";
var directoryPath = Path.Combine(Constant.DataDirectory, directoryName);
Helper.ValidateDirectory(directoryPath);
ValidateDirectory();
const string fileSuffix = ".cache";
FilePath = Path.Combine(directoryPath, $"{filename}{fileSuffix}");
}
public string FilePath { get; }
public T TryLoad(T defaultData)
{
if (File.Exists(FilePath))

View file

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

View file

@ -6,14 +6,13 @@ namespace Wox.Infrastructure.Storage
{
public PluginJsonStorage()
{
DirectoryName = Constant.Plugins;
// C# releated, add python releated below
var assemblyName = DataType.Assembly.GetName().Name;
DirectoryPath = Path.Combine(DirectoryPath, DirectoryName, assemblyName);
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
var dataType = typeof(T);
var assemblyName = typeof(T).Assembly.GetName().Name;
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="Storage\ISavable.cs" />
<Compile Include="Storage\PluginJsonStorage.cs" />
<Compile Include="Storage\Storage.cs" />
<Compile Include="Stopwatch.cs" />
<Compile Include="Storage\BinaryStorage.cs" />
<Compile Include="Storage\JsonStorage.cs" />
<Compile Include="Storage\WoxJsonStorage.cs" />
<Compile Include="StringMatcher.cs" />
<Compile Include="Http\Http.cs" />
<Compile Include="FuzzyMatcher.cs" />

View file

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

View file

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