Use variable instead of global static method

1. introduce variable
2. part of #389
3. refactoring program suffix in program plugin
4. 全局变量一时爽,代码重构火葬场
This commit is contained in:
bao-qian
2016-03-28 03:09:57 +01:00
parent c596039453
commit b22a4501cc
36 changed files with 402 additions and 343 deletions

View File

@@ -10,13 +10,14 @@ namespace Wox.Plugin.WebSearch
{
public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery, IMultipleActionKeywords
{
private WebSearchStorage _settings = WebSearchStorage.Instance;
public PluginInitContext Context { get; private set; }
public List<Result> Query(Query query)
{
List<Result> results = new List<Result>();
WebSearch webSearch =
WebSearchStorage.Instance.WebSearches.FirstOrDefault(o => o.ActionKeyword == query.ActionKeyword && o.Enabled);
_settings.WebSearches.FirstOrDefault(o => o.ActionKeyword == query.ActionKeyword && o.Enabled);
if (webSearch != null)
{
@@ -42,7 +43,7 @@ namespace Wox.Plugin.WebSearch
};
results.Add(result);
if (WebSearchStorage.Instance.EnableWebSearchSuggestion && !string.IsNullOrEmpty(keyword))
if (_settings.EnableWebSearchSuggestion && !string.IsNullOrEmpty(keyword))
{
// todo use Task.Wait when .net upgraded
results.AddRange(ResultsFromSuggestions(keyword, subtitle, webSearch));
@@ -53,7 +54,7 @@ namespace Wox.Plugin.WebSearch
private IEnumerable<Result> ResultsFromSuggestions(string keyword, string subtitle, WebSearch webSearch)
{
ISuggestionSource sugg = SuggestionSourceFactory.GetSuggestionSource(WebSearchStorage.Instance.WebSearchSuggestionSource, Context);
ISuggestionSource sugg = SuggestionSourceFactory.GetSuggestionSource(_settings.WebSearchSuggestionSource, Context);
var suggestions = sugg?.GetSuggestions(keyword);
if (suggestions != null)
{
@@ -83,7 +84,7 @@ namespace Wox.Plugin.WebSearch
public Control CreateSettingPanel()
{
return new WebSearchesSetting(this);
return new WebSearchesSetting(this, _settings);
}
#endregion

View File

@@ -18,18 +18,20 @@ namespace Wox.Plugin.WebSearch
private WebSearch _updateWebSearch;
private readonly PluginInitContext _context;
private readonly WebSearchPlugin _plugin;
private WebSearchStorage _settings;
public WebSearchSetting(WebSearchesSetting settingWidow)
public WebSearchSetting(WebSearchesSetting settingWidow, WebSearchStorage settings)
{
_plugin = settingWidow.Plugin;
_context = settingWidow.Context;
_settingWindow = settingWidow;
InitializeComponent();
_settings = settings;
}
public void UpdateItem(WebSearch webSearch)
{
_updateWebSearch = WebSearchStorage.Instance.WebSearches.FirstOrDefault(o => o == webSearch);
_updateWebSearch = _settings.WebSearches.FirstOrDefault(o => o == webSearch);
if (_updateWebSearch == null || string.IsNullOrEmpty(_updateWebSearch.Url))
{
@@ -110,7 +112,7 @@ namespace Wox.Plugin.WebSearch
MessageBox.Show(exception.Message);
return;
}
WebSearchStorage.Instance.WebSearches.Add(new WebSearch
_settings.WebSearches.Add(new WebSearch
{
ActionKeyword = newActionKeyword,
Enabled = cbEnable.IsChecked ?? false,
@@ -120,7 +122,7 @@ namespace Wox.Plugin.WebSearch
});
}
WebSearchStorage.Instance.Save();
_settings.Save();
_settingWindow.ReloadWebSearchView();
Close();
}

View File

@@ -10,22 +10,24 @@ namespace Wox.Plugin.WebSearch
/// </summary>
public partial class WebSearchesSetting : UserControl
{
private WebSearchStorage _settings;
public PluginInitContext Context { get; }
public WebSearchPlugin Plugin { get; }
public WebSearchesSetting(WebSearchPlugin plugin)
public WebSearchesSetting(WebSearchPlugin plugin, WebSearchStorage settings)
{
Context = plugin.Context;
Plugin = plugin;
InitializeComponent();
Loaded += Setting_Loaded;
_settings = settings;
}
private void Setting_Loaded(object sender, RoutedEventArgs e)
{
webSearchView.ItemsSource = WebSearchStorage.Instance.WebSearches;
cbEnableWebSearchSuggestion.IsChecked = WebSearchStorage.Instance.EnableWebSearchSuggestion;
comboBoxSuggestionSource.Visibility = WebSearchStorage.Instance.EnableWebSearchSuggestion
webSearchView.ItemsSource = _settings.WebSearches;
cbEnableWebSearchSuggestion.IsChecked = _settings.EnableWebSearchSuggestion;
comboBoxSuggestionSource.Visibility = _settings.EnableWebSearchSuggestion
? Visibility.Visible
: Visibility.Collapsed;
@@ -34,7 +36,7 @@ namespace Wox.Plugin.WebSearch
new ComboBoxItem {Content = "Google"},
new ComboBoxItem {Content = "Baidu"}
};
ComboBoxItem selected = items.FirstOrDefault(o => o.Content.ToString() == WebSearchStorage.Instance.WebSearchSuggestionSource);
ComboBoxItem selected = items.FirstOrDefault(o => o.Content.ToString() == _settings.WebSearchSuggestionSource);
if (selected == null)
{
selected = items[0];
@@ -51,7 +53,7 @@ namespace Wox.Plugin.WebSearch
private void btnAddWebSearch_OnClick(object sender, RoutedEventArgs e)
{
WebSearchSetting webSearch = new WebSearchSetting(this);
WebSearchSetting webSearch = new WebSearchSetting(this, _settings);
webSearch.ShowDialog();
}
@@ -64,7 +66,7 @@ namespace Wox.Plugin.WebSearch
if (MessageBox.Show(msg, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
WebSearchStorage.Instance.WebSearches.Remove(selectedWebSearch);
_settings.WebSearches.Remove(selectedWebSearch);
webSearchView.Items.Refresh();
}
}
@@ -80,7 +82,7 @@ namespace Wox.Plugin.WebSearch
WebSearch selectedWebSearch = webSearchView.SelectedItem as WebSearch;
if (selectedWebSearch != null)
{
WebSearchSetting webSearch = new WebSearchSetting(this);
WebSearchSetting webSearch = new WebSearchSetting(this, _settings);
webSearch.UpdateItem(selectedWebSearch);
webSearch.ShowDialog();
}
@@ -94,23 +96,23 @@ namespace Wox.Plugin.WebSearch
private void CbEnableWebSearchSuggestion_OnChecked(object sender, RoutedEventArgs e)
{
comboBoxSuggestionSource.Visibility = Visibility.Visible;
WebSearchStorage.Instance.EnableWebSearchSuggestion = true;
WebSearchStorage.Instance.Save();
_settings.EnableWebSearchSuggestion = true;
_settings.Save();
}
private void CbEnableWebSearchSuggestion_OnUnchecked(object sender, RoutedEventArgs e)
{
comboBoxSuggestionSource.Visibility = Visibility.Collapsed;
WebSearchStorage.Instance.EnableWebSearchSuggestion = false;
WebSearchStorage.Instance.Save();
_settings.EnableWebSearchSuggestion = false;
_settings.Save();
}
private void ComboBoxSuggestionSource_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
WebSearchStorage.Instance.WebSearchSuggestionSource = ((ComboBoxItem)e.AddedItems[0]).Content.ToString();
WebSearchStorage.Instance.Save();
_settings.WebSearchSuggestionSource = ((ComboBoxItem)e.AddedItems[0]).Content.ToString();
_settings.Save();
}
}
}