Skip to content

Commit da8a690

Browse files
committed
Improve code quality
1 parent c41d021 commit da8a690

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

Flow.Launcher.Core/Resource/LocalizedDescriptionAttribute.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
using System.ComponentModel;
2+
using CommunityToolkit.Mvvm.DependencyInjection;
3+
using Flow.Launcher.Plugin;
24

35
namespace Flow.Launcher.Core.Resource
46
{
57
public class LocalizedDescriptionAttribute : DescriptionAttribute
68
{
7-
private readonly Internationalization _translator;
9+
// We should not initialize API in static constructor because it will create another API instance
10+
private static IPublicAPI api = null;
11+
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
12+
813
private readonly string _resourceKey;
914

1015
public LocalizedDescriptionAttribute(string resourceKey)
1116
{
12-
_translator = InternationalizationManager.Instance;
1317
_resourceKey = resourceKey;
1418
}
1519

1620
public override string Description
1721
{
1822
get
1923
{
20-
string description = _translator.GetTranslation(_resourceKey);
24+
string description = API.GetTranslation(_resourceKey);
2125
return string.IsNullOrWhiteSpace(description) ?
2226
string.Format("[[{0}]]", _resourceKey) : description;
2327
}
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
using System;
22
using System.Globalization;
33
using System.Windows.Data;
4+
using CommunityToolkit.Mvvm.DependencyInjection;
5+
using Flow.Launcher.Plugin;
46

57
namespace Flow.Launcher.Core.Resource
68
{
79
public class TranslationConverter : IValueConverter
810
{
11+
// We should not initialize API in static constructor because it will create another API instance
12+
private static IPublicAPI api = null;
13+
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
14+
915
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
1016
{
1117
var key = value.ToString();
12-
if (String.IsNullOrEmpty(key))
13-
return key;
14-
return InternationalizationManager.Instance.GetTranslation(key);
18+
if (string.IsNullOrEmpty(key)) return key;
19+
return API.GetTranslation(key);
1520
}
1621

17-
public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException();
22+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) =>
23+
throw new InvalidOperationException();
1824
}
1925
}

Flow.Launcher/SettingPages/ViewModels/SettingsPanePluginStoreViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private async Task RefreshExternalPluginsAsync()
3232
public bool SatisfiesFilter(PluginStoreItemViewModel plugin)
3333
{
3434
return string.IsNullOrEmpty(FilterText) ||
35-
StringMatcher.FuzzySearch(FilterText, plugin.Name).IsSearchPrecisionScoreMet() ||
36-
StringMatcher.FuzzySearch(FilterText, plugin.Description).IsSearchPrecisionScoreMet();
35+
App.API.FuzzySearch(FilterText, plugin.Name).IsSearchPrecisionScoreMet() ||
36+
App.API.FuzzySearch(FilterText, plugin.Description).IsSearchPrecisionScoreMet();
3737
}
3838
}

Flow.Launcher/SettingPages/ViewModels/SettingsPanePluginsViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public SettingsPanePluginsViewModel(Settings settings)
106106
public List<PluginViewModel> FilteredPluginViewModels => PluginViewModels
107107
.Where(v =>
108108
string.IsNullOrEmpty(FilterText) ||
109-
StringMatcher.FuzzySearch(FilterText, v.PluginPair.Metadata.Name).IsSearchPrecisionScoreMet() ||
110-
StringMatcher.FuzzySearch(FilterText, v.PluginPair.Metadata.Description).IsSearchPrecisionScoreMet()
109+
App.API.FuzzySearch(FilterText, v.PluginPair.Metadata.Name).IsSearchPrecisionScoreMet() ||
110+
App.API.FuzzySearch(FilterText, v.PluginPair.Metadata.Description).IsSearchPrecisionScoreMet()
111111
)
112112
.ToList();
113113

Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
using Flow.Launcher.Core.Resource;
2-
using Flow.Launcher.Infrastructure;
3-
using Flow.Launcher.Plugin.SharedCommands;
4-
using System;
1+
using System;
52
using System.IO;
63
using System.Linq;
74
using System.Threading.Tasks;
8-
using Flow.Launcher.Plugin.Explorer.Search.Everything;
9-
using System.Windows.Input;
10-
using Path = System.IO.Path;
115
using System.Windows.Controls;
6+
using System.Windows.Input;
7+
using Flow.Launcher.Plugin.Explorer.Search.Everything;
128
using Flow.Launcher.Plugin.Explorer.Views;
9+
using Flow.Launcher.Plugin.SharedCommands;
1310
using Peter;
11+
using Path = System.IO.Path;
1412

1513
namespace Flow.Launcher.Plugin.Explorer.Search
1614
{
@@ -66,7 +64,7 @@ public static Result CreateResult(Query query, SearchResult result)
6664
CreateFolderResult(Path.GetFileName(result.FullPath), result.FullPath, result.FullPath, query, result.Score, result.WindowsIndexed),
6765
ResultType.File =>
6866
CreateFileResult(result.FullPath, query, result.Score, result.WindowsIndexed),
69-
_ => throw new ArgumentOutOfRangeException()
67+
_ => throw new ArgumentOutOfRangeException(null)
7068
};
7169
}
7270

@@ -99,7 +97,7 @@ internal static Result CreateFolderResult(string title, string subtitle, string
9997
IcoPath = path,
10098
SubTitle = subtitle,
10199
AutoCompleteText = GetAutoCompleteText(title, query, path, ResultType.Folder),
102-
TitleHighlightData = StringMatcher.FuzzySearch(query.Search, title).MatchData,
100+
TitleHighlightData = Context.API.FuzzySearch(query.Search, title).MatchData,
103101
CopyText = path,
104102
Preview = new Result.PreviewInfo
105103
{
@@ -164,7 +162,7 @@ internal static Result CreateFolderResult(string title, string subtitle, string
164162
return false;
165163
},
166164
Score = score,
167-
TitleToolTip = InternationalizationManager.Instance.GetTranslation("plugin_explorer_plugin_ToolTipOpenDirectory"),
165+
TitleToolTip = Main.Context.API.GetTranslation("plugin_explorer_plugin_ToolTipOpenDirectory"),
168166
SubTitleToolTip = path,
169167
ContextData = new SearchResult { Type = ResultType.Folder, FullPath = path, WindowsIndexed = windowsIndexed }
170168
};
@@ -286,7 +284,7 @@ internal static Result CreateFileResult(string filePath, Query query, int score
286284
FilePath = filePath,
287285
},
288286
AutoCompleteText = GetAutoCompleteText(title, query, filePath, ResultType.File),
289-
TitleHighlightData = StringMatcher.FuzzySearch(query.Search, title).MatchData,
287+
TitleHighlightData = Context.API.FuzzySearch(query.Search, title).MatchData,
290288
Score = score,
291289
CopyText = filePath,
292290
PreviewPanel = new Lazy<UserControl>(() => new PreviewPanel(Settings, filePath)),
@@ -319,7 +317,7 @@ internal static Result CreateFileResult(string filePath, Query query, int score
319317

320318
return true;
321319
},
322-
TitleToolTip = InternationalizationManager.Instance.GetTranslation("plugin_explorer_plugin_ToolTipOpenContainingFolder"),
320+
TitleToolTip = Main.Context.API.GetTranslation("plugin_explorer_plugin_ToolTipOpenContainingFolder"),
323321
SubTitleToolTip = filePath,
324322
ContextData = new SearchResult { Type = ResultType.File, FullPath = filePath, WindowsIndexed = windowsIndexed }
325323
};

0 commit comments

Comments
 (0)