Skip to content

Commit

Permalink
MVVM refactoring for web search plugin, part 1
Browse files Browse the repository at this point in the history
1. #486
2. fix #778 #763 #742
3. MVVM refactoring
4. remove IMultipleActionKeywords interface, use PluginManager directly
  • Loading branch information
bao-qian committed Jun 20, 2016
1 parent d739920 commit 32fe69c
Show file tree
Hide file tree
Showing 30 changed files with 480 additions and 503 deletions.
3 changes: 2 additions & 1 deletion Plugins/Wox.Plugin.WebSearch/Languages/de.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
<system:String x:Key="wox_plugin_websearch_delete">Löschen</system:String>
<system:String x:Key="wox_plugin_websearch_edit">Bearbeiten</system:String>
<system:String x:Key="wox_plugin_websearch_add">Hinzufügen</system:String>
<system:String x:Key="wox_plugin_websearch_confirm">Confirm</system:String>
<system:String x:Key="wox_plugin_websearch_action_keyword">Aktionsschlüsselwort</system:String>
<system:String x:Key="wox_plugin_websearch_url">URL</system:String>
<system:String x:Key="wox_plugin_websearch_search">Suche</system:String>
<system:String x:Key="wox_plugin_websearch_enable_suggestion">Aktiviere Suchvorschläge</system:String>
<system:String x:Key="wox_plugin_websearch_pls_select_web_search">Bitte wähle einen Suchdienst</system:String>
<system:String x:Key="wox_plugin_websearch_delete_warning">Willst du wirklich {0} löschen?</system:String>


<!--web search edit-->
<system:String x:Key="wox_plugin_websearch_title">Titel</system:String>
<system:String x:Key="wox_plugin_websearch_enable">Aktivieren</system:String>
Expand Down
3 changes: 2 additions & 1 deletion Plugins/Wox.Plugin.WebSearch/Languages/en.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
<system:String x:Key="wox_plugin_websearch_delete">Delete</system:String>
<system:String x:Key="wox_plugin_websearch_edit">Edit</system:String>
<system:String x:Key="wox_plugin_websearch_add">Add</system:String>
<system:String x:Key="wox_plugin_websearch_confirm">Confirm</system:String>
<system:String x:Key="wox_plugin_websearch_action_keyword">Action Keyword</system:String>
<system:String x:Key="wox_plugin_websearch_url">URL</system:String>
<system:String x:Key="wox_plugin_websearch_search">Search</system:String>
<system:String x:Key="wox_plugin_websearch_enable_suggestion">Enable search suggestions</system:String>
<system:String x:Key="wox_plugin_websearch_pls_select_web_search">Please select a web search</system:String>
<system:String x:Key="wox_plugin_websearch_delete_warning">Are you sure you want to delete {0}?</system:String>

<!--web search edit-->
<system:String x:Key="wox_plugin_websearch_title">Title</system:String>
<system:String x:Key="wox_plugin_websearch_enable">Enable</system:String>
Expand Down
3 changes: 2 additions & 1 deletion Plugins/Wox.Plugin.WebSearch/Languages/pl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
<system:String x:Key="wox_plugin_websearch_delete">Usuń</system:String>
<system:String x:Key="wox_plugin_websearch_edit">Edytuj</system:String>
<system:String x:Key="wox_plugin_websearch_add">Dodaj</system:String>
<system:String x:Key="wox_plugin_websearch_confirm">Confirm</system:String>
<system:String x:Key="wox_plugin_websearch_action_keyword">Wyzwalacz</system:String>
<system:String x:Key="wox_plugin_websearch_url">Adres URL</system:String>
<system:String x:Key="wox_plugin_websearch_search">Szukaj</system:String>
<system:String x:Key="wox_plugin_websearch_enable_suggestion">Pokazuj podpowiedzi wyszukiwania</system:String>
<system:String x:Key="wox_plugin_websearch_pls_select_web_search">Musisz wybrać coś z listy</system:String>
<system:String x:Key="wox_plugin_websearch_delete_warning">Czy jesteś pewnie że chcesz usunąć {0}?</system:String>

<!--web search edit-->
<system:String x:Key="wox_plugin_websearch_title">Tytuł</system:String>
<system:String x:Key="wox_plugin_websearch_enable">Aktywne</system:String>
Expand Down
1 change: 1 addition & 0 deletions Plugins/Wox.Plugin.WebSearch/Languages/zh-cn.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<system:String x:Key="wox_plugin_websearch_delete">删除</system:String>
<system:String x:Key="wox_plugin_websearch_edit">编辑</system:String>
<system:String x:Key="wox_plugin_websearch_add">添加</system:String>
<system:String x:Key="wox_plugin_websearch_confirm">确认</system:String>
<system:String x:Key="wox_plugin_websearch_action_keyword">触发关键字</system:String>
<system:String x:Key="wox_plugin_websearch_url">URL</system:String>
<system:String x:Key="wox_plugin_websearch_search">搜索</system:String>
Expand Down
3 changes: 2 additions & 1 deletion Plugins/Wox.Plugin.WebSearch/Languages/zh-tw.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<system:String x:Key="wox_plugin_websearch_delete">刪除</system:String>
<system:String x:Key="wox_plugin_websearch_edit">編輯</system:String>
<system:String x:Key="wox_plugin_websearch_add">添加</system:String>
<system:String x:Key="wox_plugin_websearch_confirm">确认</system:String>
<system:String x:Key="wox_plugin_websearch_action_keyword">觸發關鍵字</system:String>
<system:String x:Key="wox_plugin_websearch_url">URL</system:String>
<system:String x:Key="wox_plugin_websearch_search">搜索</system:String>
Expand All @@ -28,5 +29,5 @@

<system:String x:Key="wox_plugin_websearch_plugin_name">網頁搜索</system:String>
<system:String x:Key="wox_plugin_websearch_plugin_description">提供網頁搜索能力</system:String>

</ResourceDictionary>
92 changes: 36 additions & 56 deletions Plugins/Wox.Plugin.WebSearch/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,26 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using JetBrains.Annotations;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Plugin.WebSearch.SuggestionSources;

namespace Wox.Plugin.WebSearch
{
public class Main : IPlugin, ISettingProvider, IPluginI18n, IMultipleActionKeywords, ISavable, IResultUpdated
public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable, IResultUpdated
{
public PluginInitContext Context { get; private set; }
private PluginInitContext _context;

private PluginJsonStorage<Settings> _storage;
private Settings _settings;
private readonly Settings _settings;
private readonly SettingsViewModel _viewModel;
private CancellationTokenSource _updateSource;
private CancellationToken _updateToken;

public const string Images = "Images";
public static string ImagesDirectory;
public static readonly string ImagesDirectory;

public void Save()
{
_storage.Save();
_viewModel.Save();
}

public List<Result> Query(Query query)
Expand All @@ -36,21 +34,21 @@ public List<Result> Query(Query query)
_updateSource = new CancellationTokenSource();
_updateToken = _updateSource.Token;

WebSearch webSearch =
_settings.WebSearches.FirstOrDefault(o => o.ActionKeyword == query.ActionKeyword && o.Enabled);
SearchSource searchSource =
_settings.SearchSources.FirstOrDefault(o => o.ActionKeyword == query.ActionKeyword && o.Enabled);

if (webSearch != null)
if (searchSource != null)
{
string keyword = query.Search;
string title = keyword;
string subtitle = Context.API.GetTranslation("wox_plugin_websearch_search") + " " + webSearch.Title;
string subtitle = _context.API.GetTranslation("wox_plugin_websearch_search") + " " + searchSource.Title;
if (string.IsNullOrEmpty(keyword))
{
var result = new Result
{
Title = subtitle,
SubTitle = string.Empty,
IcoPath = webSearch.IconPath
IcoPath = searchSource.IconPath
};
return new List<Result> { result };
}
Expand All @@ -62,15 +60,15 @@ public List<Result> Query(Query query)
Title = title,
SubTitle = subtitle,
Score = 6,
IcoPath = webSearch.IconPath,
IcoPath = searchSource.IconPath,
Action = c =>
{
Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(keyword)));
Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(keyword)));
return true;
}
};
results.Add(result);
UpdateResultsFromSuggestion(results, keyword, subtitle, webSearch, query);
UpdateResultsFromSuggestion(results, keyword, subtitle, searchSource, query);
return results;
}
}
Expand All @@ -80,14 +78,15 @@ public List<Result> Query(Query query)
}
}

private void UpdateResultsFromSuggestion(List<Result> results, string keyword, string subtitle, WebSearch webSearch, Query query)
private void UpdateResultsFromSuggestion(List<Result> results, string keyword, string subtitle,
SearchSource searchSource, Query query)
{
if (_settings.EnableWebSearchSuggestion)
if (_settings.EnableSuggestion)
{
const int waittime = 300;
var task = Task.Run(async () =>
{
var suggestions = await Suggestions(keyword, subtitle, webSearch);
var suggestions = await Suggestions(keyword, subtitle, searchSource);
results.AddRange(suggestions);
}, _updateToken);

Expand All @@ -102,21 +101,21 @@ private void UpdateResultsFromSuggestion(List<Result> results, string keyword, s
}
}

private async Task<IEnumerable<Result>> Suggestions(string keyword, string subtitle, WebSearch webSearch)
private async Task<IEnumerable<Result>> Suggestions(string keyword, string subtitle, SearchSource searchSource)
{
var source = SuggestionSource.GetSuggestionSource(_settings.WebSearchSuggestionSource);
var source = _settings.SelectedSuggestion;
if (source != null)
{
var suggestions = await source.GetSuggestions(keyword);
var suggestions = await source.Suggestions(keyword);
var resultsFromSuggestion = suggestions.Select(o => new Result
{
Title = o,
SubTitle = subtitle,
Score = 5,
IcoPath = webSearch.IconPath,
IcoPath = searchSource.IconPath,
Action = c =>
{
Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(o)));
Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)));
return true;
}
});
Expand All @@ -127,18 +126,21 @@ private async Task<IEnumerable<Result>> Suggestions(string keyword, string subti

static Main()
{
var plugins = Infrastructure.Constant.Plugins;
var plugins = Constant.Plugins;
var assemblyName = typeof(Main).Assembly.GetName().Name;
var pluginDirectory = Path.Combine(Infrastructure.Constant.SettingsPath, plugins, assemblyName);
var pluginDirectory = Path.Combine(Constant.SettingsPath, plugins, assemblyName);
ImagesDirectory = Path.Combine(pluginDirectory, Images);
}

public void Init(PluginInitContext context)
public Main()
{
Context = context;
_viewModel = new SettingsViewModel();
_settings = _viewModel.Settings;
}

_storage = new PluginJsonStorage<Settings>();
_settings = _storage.Load();
public void Init(PluginInitContext context)
{
_context = context;

var pluginDirectory = context.CurrentPluginMetadata.PluginDirectory;
var bundledImagesDirectory = Path.Combine(pluginDirectory, Images);
Expand All @@ -149,43 +151,21 @@ public void Init(PluginInitContext context)

public Control CreateSettingPanel()
{
return new WebSearchesSetting(this, _settings);
return new SettingsControl(_context, _viewModel);
}

#endregion

public string GetTranslatedPluginTitle()
{
return Context.API.GetTranslation("wox_plugin_websearch_plugin_name");
return _context.API.GetTranslation("wox_plugin_websearch_plugin_name");
}

public string GetTranslatedPluginDescription()
{
return Context.API.GetTranslation("wox_plugin_websearch_plugin_description");
}

public bool IsInstantQuery(string query) => false;

[NotifyPropertyChangedInvocator]
public void NotifyActionKeywordsUpdated(string oldActionKeywords, string newActionKeywords)
{
ActionKeywordsChanged?.Invoke(this, new ActionKeywordsChangedEventArgs
{
OldActionKeyword = oldActionKeywords,
NewActionKeyword = newActionKeywords
});
}

[NotifyPropertyChangedInvocator]
public void NotifyActionKeywordsAdded(string newActionKeywords)
{
ActionKeywordsChanged?.Invoke(this, new ActionKeywordsChangedEventArgs
{
NewActionKeyword = newActionKeywords
});
return _context.API.GetTranslation("wox_plugin_websearch_plugin_description");
}

public event ActionKeywordsChangedEventHandler ActionKeywordsChanged;
public event ResultUpdatedEventHandler ResultsUpdated;
}
}
}
7 changes: 7 additions & 0 deletions Plugins/Wox.Plugin.WebSearch/SearchSourceViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Wox.Plugin.WebSearch
{
public class SearchSourceViewModel
{
public SearchSource SearchSource { get; set; }
}
}
Loading

0 comments on commit 32fe69c

Please sign in to comment.