Skip to content

Commit 698217f

Browse files
committed
Improve code quality
1 parent 1e6bbdd commit 698217f

File tree

2 files changed

+45
-46
lines changed

2 files changed

+45
-46
lines changed

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
4+
using System.Collections.Specialized;
5+
using System.Diagnostics;
6+
using System.IO;
37
using System.Linq;
48
using System.Net;
9+
using System.Runtime.CompilerServices;
10+
using System.Threading;
511
using System.Threading.Tasks;
612
using System.Windows;
13+
using CommunityToolkit.Mvvm.DependencyInjection;
714
using Squirrel;
15+
using Flow.Launcher.Core;
816
using Flow.Launcher.Core.Plugin;
9-
using Flow.Launcher.Core.Resource;
1017
using Flow.Launcher.Helper;
1118
using Flow.Launcher.Infrastructure;
19+
using Flow.Launcher.Infrastructure.Http;
1220
using Flow.Launcher.Infrastructure.Hotkey;
1321
using Flow.Launcher.Infrastructure.Image;
22+
using Flow.Launcher.Infrastructure.Logger;
23+
using Flow.Launcher.Infrastructure.Storage;
24+
using Flow.Launcher.Infrastructure.UserSettings;
1425
using Flow.Launcher.Plugin;
15-
using Flow.Launcher.ViewModel;
1626
using Flow.Launcher.Plugin.SharedModels;
1727
using Flow.Launcher.Plugin.SharedCommands;
18-
using System.Threading;
19-
using System.IO;
20-
using Flow.Launcher.Infrastructure.Http;
28+
using Flow.Launcher.ViewModel;
2129
using JetBrains.Annotations;
22-
using System.Runtime.CompilerServices;
23-
using Flow.Launcher.Infrastructure.Logger;
24-
using Flow.Launcher.Infrastructure.Storage;
25-
using System.Collections.Concurrent;
26-
using System.Diagnostics;
27-
using System.Collections.Specialized;
28-
using CommunityToolkit.Mvvm.DependencyInjection;
29-
using Flow.Launcher.Core;
30-
using Flow.Launcher.Infrastructure.UserSettings;
3130

3231
namespace Flow.Launcher
3332
{
@@ -153,7 +152,7 @@ public void CopyToClipboard(string stringToCopy, bool directCopy = false, bool s
153152

154153
public void StopLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Collapsed;
155154

156-
public string GetTranslation(string key) => InternationalizationManager.Instance.GetTranslation(key);
155+
public string GetTranslation(string key) => App.API.GetTranslation(key);
157156

158157
public List<PluginPair> GetAllPlugins() => PluginManager.AllPlugins.ToList();
159158

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using CommunityToolkit.Mvvm.DependencyInjection;
1515
using CommunityToolkit.Mvvm.Input;
1616
using Flow.Launcher.Core.Plugin;
17-
using Flow.Launcher.Core.Resource;
1817
using Flow.Launcher.Infrastructure;
1918
using Flow.Launcher.Infrastructure.Hotkey;
2019
using Flow.Launcher.Infrastructure.Image;
@@ -47,8 +46,6 @@ public partial class MainViewModel : BaseModel, ISavable
4746
private CancellationTokenSource _updateSource;
4847
private CancellationToken _updateToken;
4948

50-
private readonly Internationalization _translator = InternationalizationManager.Instance;
51-
5249
private ChannelWriter<ResultsForUpdate> _resultsUpdateChannelWriter;
5350
private Task _resultsViewUpdateTask;
5451

@@ -180,9 +177,9 @@ private void RegisterViewUpdate()
180177
var resultUpdateChannel = Channel.CreateUnbounded<ResultsForUpdate>();
181178
_resultsUpdateChannelWriter = resultUpdateChannel.Writer;
182179
_resultsViewUpdateTask =
183-
Task.Run(updateAction).ContinueWith(continueAction, TaskContinuationOptions.OnlyOnFaulted);
180+
Task.Run(UpdateActionAsync).ContinueWith(continueAction, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Default);
184181

185-
async Task updateAction()
182+
async Task UpdateActionAsync()
186183
{
187184
var queue = new Dictionary<string, ResultsForUpdate>();
188185
var channelReader = resultUpdateChannel.Reader;
@@ -249,8 +246,8 @@ private async Task ReloadPluginDataAsync()
249246
Hide();
250247

251248
await PluginManager.ReloadDataAsync().ConfigureAwait(false);
252-
Notification.Show(InternationalizationManager.Instance.GetTranslation("success"),
253-
InternationalizationManager.Instance.GetTranslation("completedSuccessfully"));
249+
Notification.Show(App.API.GetTranslation("success"),
250+
App.API.GetTranslation("completedSuccessfully"));
254251
}
255252

256253
[RelayCommand]
@@ -272,14 +269,14 @@ public void ReQuery()
272269
{
273270
if (SelectedIsFromQueryResults())
274271
{
275-
QueryResults(isReQuery: true);
272+
_ = QueryResultsAsync(isReQuery: true);
276273
}
277274
}
278275

279276
public void ReQuery(bool reselect)
280277
{
281278
BackToQueryResults();
282-
QueryResults(isReQuery: true, reSelect: reselect);
279+
_ = QueryResultsAsync(isReQuery: true, reSelect: reselect);
283280
}
284281

285282
[RelayCommand]
@@ -387,11 +384,11 @@ private async Task OpenResultAsync(string index)
387384
}
388385

389386
var hideWindow = await result.ExecuteAsync(new ActionContext
390-
{
391-
// not null means pressing modifier key + number, should ignore the modifier key
392-
SpecialKeyState = index is not null ? SpecialKeyState.Default : GlobalHotkey.CheckModifiers()
393-
})
394-
.ConfigureAwait(false);
387+
{
388+
// not null means pressing modifier key + number, should ignore the modifier key
389+
SpecialKeyState = index is not null ? SpecialKeyState.Default : GlobalHotkey.CheckModifiers()
390+
})
391+
.ConfigureAwait(false);
395392

396393
if (SelectedIsFromQueryResults())
397394
{
@@ -455,7 +452,6 @@ private void SelectLastResult()
455452
SelectedResults.SelectLastResult();
456453
}
457454

458-
459455
[RelayCommand]
460456
private void SelectPrevPage()
461457
{
@@ -482,7 +478,6 @@ private void SelectPrevItem()
482478
{
483479
SelectedResults.SelectPrevResult();
484480
}
485-
486481
}
487482

488483
[RelayCommand]
@@ -559,7 +554,6 @@ private async Task RegisterClockAndDateUpdateAsync()
559554
public bool GameModeStatus { get; set; } = false;
560555

561556
private string _queryText;
562-
563557
public string QueryText
564558
{
565559
get => _queryText;
@@ -808,8 +802,8 @@ public bool InternalPreviewVisible
808802
throw new NotImplementedException("ResultAreaColumn should match ResultAreaColumnPreviewShown/ResultAreaColumnPreviewHidden value");
809803
#else
810804
Log.Error("MainViewModel", "ResultAreaColumnPreviewHidden/ResultAreaColumnPreviewShown int value not implemented", "InternalPreviewVisible");
811-
#endif
812805
return false;
806+
#endif
813807
}
814808
}
815809

@@ -897,7 +891,7 @@ private void CloseExternalPreview()
897891
ExternalPreviewVisible = false;
898892
}
899893

900-
private void SwitchExternalPreview(string path, bool sendFailToast = true)
894+
private static void SwitchExternalPreview(string path, bool sendFailToast = true)
901895
{
902896
_ = PluginManager.SwitchExternalPreviewAsync(path,sendFailToast).ConfigureAwait(false);
903897
}
@@ -979,7 +973,7 @@ public void Query(bool isReQuery = false)
979973
{
980974
if (SelectedIsFromQueryResults())
981975
{
982-
QueryResults(isReQuery);
976+
_ = QueryResultsAsync(isReQuery);
983977
}
984978
else if (ContextMenuSelected())
985979
{
@@ -1042,8 +1036,8 @@ private void QueryHistory()
10421036
var results = new List<Result>();
10431037
foreach (var h in _history.Items)
10441038
{
1045-
var title = _translator.GetTranslation("executeQuery");
1046-
var time = _translator.GetTranslation("lastExecuteTime");
1039+
var title = App.API.GetTranslation("executeQuery");
1040+
var time = App.API.GetTranslation("lastExecuteTime");
10471041
var result = new Result
10481042
{
10491043
Title = string.Format(title, h.Query),
@@ -1077,7 +1071,7 @@ private void QueryHistory()
10771071

10781072
private readonly IReadOnlyList<Result> _emptyResult = new List<Result>();
10791073

1080-
private async void QueryResults(bool isReQuery = false, bool reSelect = true)
1074+
private async Task QueryResultsAsync(bool isReQuery = false, bool reSelect = true)
10811075
{
10821076
_updateSource?.Cancel();
10831077

@@ -1155,7 +1149,7 @@ private async void QueryResults(bool isReQuery = false, bool reSelect = true)
11551149

11561150
var tasks = plugins.Select(plugin => plugin.Metadata.Disabled switch
11571151
{
1158-
false => QueryTask(plugin, reSelect),
1152+
false => QueryTaskAsync(plugin, reSelect),
11591153
true => Task.CompletedTask
11601154
}).ToArray();
11611155

@@ -1183,7 +1177,7 @@ private async void QueryResults(bool isReQuery = false, bool reSelect = true)
11831177
}
11841178

11851179
// Local function
1186-
async Task QueryTask(PluginPair plugin, bool reSelect = true)
1180+
async Task QueryTaskAsync(PluginPair plugin, bool reSelect = true)
11871181
{
11881182
// Since it is wrapped within a ThreadPool Thread, the synchronous context is null
11891183
// Task.Yield will force it to run in ThreadPool
@@ -1282,13 +1276,13 @@ private Result ContextMenuTopMost(Result result)
12821276
{
12831277
menu = new Result
12841278
{
1285-
Title = InternationalizationManager.Instance.GetTranslation("cancelTopMostInThisQuery"),
1279+
Title = App.API.GetTranslation("cancelTopMostInThisQuery"),
12861280
IcoPath = "Images\\down.png",
12871281
PluginDirectory = Constant.ProgramDirectory,
12881282
Action = _ =>
12891283
{
12901284
_topMostRecord.Remove(result);
1291-
App.API.ShowMsg(InternationalizationManager.Instance.GetTranslation("success"));
1285+
App.API.ShowMsg(App.API.GetTranslation("success"));
12921286
App.API.ReQuery();
12931287
return false;
12941288
}
@@ -1298,14 +1292,14 @@ private Result ContextMenuTopMost(Result result)
12981292
{
12991293
menu = new Result
13001294
{
1301-
Title = InternationalizationManager.Instance.GetTranslation("setAsTopMostInThisQuery"),
1295+
Title = App.API.GetTranslation("setAsTopMostInThisQuery"),
13021296
IcoPath = "Images\\up.png",
13031297
Glyph = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\xeac2"),
13041298
PluginDirectory = Constant.ProgramDirectory,
13051299
Action = _ =>
13061300
{
13071301
_topMostRecord.AddOrUpdate(result);
1308-
App.API.ShowMsg(InternationalizationManager.Instance.GetTranslation("success"));
1302+
App.API.ShowMsg(App.API.GetTranslation("success"));
13091303
App.API.ReQuery();
13101304
return false;
13111305
}
@@ -1315,10 +1309,10 @@ private Result ContextMenuTopMost(Result result)
13151309
return menu;
13161310
}
13171311

1318-
private Result ContextMenuPluginInfo(string id)
1312+
private static Result ContextMenuPluginInfo(string id)
13191313
{
13201314
var metadata = PluginManager.GetPluginForId(id).Metadata;
1321-
var translator = InternationalizationManager.Instance;
1315+
var translator = App.API;
13221316

13231317
var author = translator.GetTranslation("author");
13241318
var website = translator.GetTranslation("website");
@@ -1400,12 +1394,16 @@ public void Show()
14001394
});
14011395
}
14021396

1397+
#pragma warning disable VSTHRD100 // Avoid async void methods
1398+
14031399
public async void Hide()
14041400
{
14051401
lastHistoryIndex = 1;
14061402

14071403
if (ExternalPreviewVisible)
1404+
{
14081405
CloseExternalPreview();
1406+
}
14091407

14101408
if (!SelectedIsFromQueryResults())
14111409
{
@@ -1476,6 +1474,8 @@ public async void Hide()
14761474
VisibilityChanged?.Invoke(this, new VisibilityChangedEventArgs { IsVisible = false });
14771475
}
14781476

1477+
#pragma warning restore VSTHRD100 // Avoid async void methods
1478+
14791479
/// <summary>
14801480
/// Checks if Flow Launcher should ignore any hotkeys
14811481
/// </summary>

0 commit comments

Comments
 (0)