Remove instance logic for BinaryStorage and JsonStorage, part 1

1. part of #389
2. huge refactoring
This commit is contained in:
bao-qian
2016-04-21 01:53:21 +01:00
parent 0bcb76fa81
commit 8d10c9aa41
52 changed files with 502 additions and 584 deletions

View File

@@ -8,6 +8,7 @@ using WindowsInput;
using WindowsInput.Native;
using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Control = System.Windows.Controls.Control;
namespace Wox.Plugin.CMD
@@ -17,7 +18,20 @@ namespace Wox.Plugin.CMD
private PluginInitContext context;
private bool WinRStroked;
private readonly KeyboardSimulator keyboardSimulator = new KeyboardSimulator(new InputSimulator());
private readonly CMDStorage _settings = CMDStorage.Instance;
private readonly CMDHistory _settings;
private readonly PluginSettingsStorage<CMDHistory> _storage;
public CMD()
{
_storage = new PluginSettingsStorage<CMDHistory>();
_settings = _storage.Load();
}
~CMD()
{
_storage.Save();
}
public List<Result> Query(Query query)
{
@@ -81,7 +95,7 @@ namespace Wox.Plugin.CMD
private List<Result> GetHistoryCmds(string cmd, Result result)
{
IEnumerable<Result> history = _settings.CMDHistory.Where(o => o.Key.Contains(cmd))
IEnumerable<Result> history = _settings.Count.Where(o => o.Key.Contains(cmd))
.OrderByDescending(o => o.Value)
.Select(m =>
{
@@ -127,7 +141,7 @@ namespace Wox.Plugin.CMD
private List<Result> ResultsFromlHistory()
{
IEnumerable<Result> history = _settings.CMDHistory.OrderByDescending(o => o.Value)
IEnumerable<Result> history = _settings.Count.OrderByDescending(o => o.Value)
.Select(m => new Result
{
Title = m.Key,

View File

@@ -5,9 +5,9 @@ namespace Wox.Plugin.CMD
{
public partial class CMDSetting : UserControl
{
private readonly CMDStorage _settings;
private readonly CMDHistory _settings;
public CMDSetting(CMDStorage settings)
public CMDSetting(CMDHistory settings)
{
InitializeComponent();
_settings = settings;
@@ -21,24 +21,20 @@ namespace Wox.Plugin.CMD
cbLeaveCmdOpen.Checked += (o, e) =>
{
_settings.LeaveCmdOpen = true;
_settings.Save();
};
cbLeaveCmdOpen.Unchecked += (o, e) =>
{
_settings.LeaveCmdOpen = false;
_settings.Save();
};
cbReplaceWinR.Checked += (o, e) =>
{
_settings.ReplaceWinR = true;
_settings.Save();
};
cbReplaceWinR.Unchecked += (o, e) =>
{
_settings.ReplaceWinR = false;
_settings.Save();
};
}
}

View File

@@ -1,40 +1,26 @@
using System.Collections.Generic;
using System.ComponentModel;
using Newtonsoft.Json;
using Wox.Infrastructure.Storage;
namespace Wox.Plugin.CMD
{
public class CMDStorage : JsonStrorage<CMDStorage>
public class CMDHistory
{
[JsonProperty]
public bool ReplaceWinR { get; set; }
[JsonProperty]
public bool ReplaceWinR { get; set; } = true;
public bool LeaveCmdOpen { get; set; }
[JsonProperty]
public Dictionary<string, int> CMDHistory = new Dictionary<string, int>();
protected override string FileName { get; } = "CMDHistory";
protected override CMDStorage LoadDefault()
{
ReplaceWinR = true;
return this;
}
public Dictionary<string, int> Count = new Dictionary<string, int>();
public void AddCmdHistory(string cmdName)
{
if (CMDHistory.ContainsKey(cmdName))
if (Count.ContainsKey(cmdName))
{
CMDHistory[cmdName] += 1;
Count[cmdName] += 1;
}
else
{
CMDHistory.Add(cmdName, 1);
Count.Add(cmdName, 1);
}
Save();
}
}
}