Skip to content

Commit b2dc128

Browse files
committed
Use dependency injection to fix issue
1 parent a2372d8 commit b2dc128

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

Plugins/Flow.Launcher.Plugin.Sys/ThemeSelector.cs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
43
using System.Linq;
4+
using CommunityToolkit.Mvvm.DependencyInjection;
55
using Flow.Launcher.Core.Resource;
6+
using Flow.Launcher.Infrastructure.UserSettings;
67

78
namespace Flow.Launcher.Plugin.Sys
89
{
910
public class ThemeSelector : IDisposable
1011
{
1112
public const string Keyword = "fltheme";
1213

13-
private readonly PluginInitContext context;
14+
private readonly Settings _settings;
15+
private readonly Theme _theme;
16+
private readonly PluginInitContext _context;
17+
1418
private IEnumerable<string> themes;
1519

1620
public ThemeSelector(PluginInitContext context)
1721
{
18-
this.context = context;
22+
_context = context;
23+
_theme = Ioc.Default.GetRequiredService<Theme>();
24+
_settings = Ioc.Default.GetRequiredService<Settings>();
1925
context.API.VisibilityChanged += OnVisibilityChanged;
2026
}
2127

28+
~ThemeSelector()
29+
{
30+
Dispose(false);
31+
}
32+
2233
public List<Result> Query(Query query)
2334
{
2435
if (query.IsReQuery)
@@ -31,34 +42,34 @@ public List<Result> Query(Query query)
3142
if (string.IsNullOrWhiteSpace(search))
3243
{
3344
return themes.Select(CreateThemeResult)
34-
.OrderBy(x => x.Title)
35-
.ToList();
45+
.OrderBy(x => x.Title)
46+
.ToList();
3647
}
3748

38-
return themes.Select(theme => (theme, matchResult: context.API.FuzzySearch(search, theme)))
39-
.Where(x => x.matchResult.IsSearchPrecisionScoreMet())
40-
.Select(x => CreateThemeResult(x.theme, x.matchResult.Score, x.matchResult.MatchData))
41-
.OrderBy(x => x.Title)
42-
.ToList();
49+
return themes.Select(theme => (theme, matchResult: _context.API.FuzzySearch(search, theme)))
50+
.Where(x => x.matchResult.IsSearchPrecisionScoreMet())
51+
.Select(x => CreateThemeResult(x.theme, x.matchResult.Score, x.matchResult.MatchData))
52+
.OrderBy(x => x.Title)
53+
.ToList();
4354
}
4455

4556
private void OnVisibilityChanged(object sender, VisibilityChangedEventArgs args)
4657
{
47-
if (args.IsVisible && !context.CurrentPluginMetadata.Disabled)
58+
if (args.IsVisible && !_context.CurrentPluginMetadata.Disabled)
4859
{
4960
LoadThemes();
5061
}
5162
}
5263

5364
private void LoadThemes()
54-
=> themes = ThemeManager.Instance.LoadAvailableThemes().Select(x => x.FileNameWithoutExtension);
65+
=> themes = _theme.LoadAvailableThemes().Select(x => x.FileNameWithoutExtension);
5566

56-
private static Result CreateThemeResult(string theme) => CreateThemeResult(theme, 0, null);
67+
private Result CreateThemeResult(string theme) => CreateThemeResult(theme, 0, null);
5768

58-
private static Result CreateThemeResult(string theme, int score, IList<int> highlightData)
69+
private Result CreateThemeResult(string theme, int score, IList<int> highlightData)
5970
{
6071
string title;
61-
if (theme == ThemeManager.Instance.Settings.Theme)
72+
if (theme == _settings.Theme)
6273
{
6374
title = $"{theme} ★";
6475
score = 2000;
@@ -76,7 +87,7 @@ private static Result CreateThemeResult(string theme, int score, IList<int> high
7687
Score = score,
7788
Action = c =>
7889
{
79-
ThemeManager.Instance.ChangeTheme(theme);
90+
_theme.ChangeTheme(theme);
8091
return true;
8192
}
8293
};
@@ -90,19 +101,16 @@ protected virtual void Dispose(bool disposing)
90101
if (disposing)
91102
{
92103
// Dispose managed resources
93-
if (context?.API != null)
104+
if (_context?.API != null)
94105
{
95-
context.API.VisibilityChanged -= OnVisibilityChanged;
106+
_context.API.VisibilityChanged -= OnVisibilityChanged;
96107
}
97108
}
98109
// Free unmanaged resources
99110
disposed = true;
100111
}
101112
}
102-
~ThemeSelector()
103-
{
104-
Dispose(false);
105-
}
113+
106114
public void Dispose()
107115
{
108116
Dispose(true);

0 commit comments

Comments
 (0)