Skip to content

Commit 88b678e

Browse files
committed
Refactor view models
1 parent 9ee9ae8 commit 88b678e

6 files changed

Lines changed: 59 additions & 37 deletions

File tree

Flow.Launcher.Plugin.OneNote/UI/RelayCommand.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,53 @@ namespace Flow.Launcher.Plugin.OneNote.UI
55
{
66
public sealed class RelayCommand : ICommand
77
{
8-
private readonly Action<object?> execute;
9-
private readonly Predicate<object?>? canExecute;
8+
private readonly Action execute;
9+
private readonly Func<bool>? canExecute;
1010

1111
public event EventHandler? CanExecuteChanged
1212
{
1313
add => CommandManager.RequerySuggested += value;
1414
remove => CommandManager.RequerySuggested -= value;
1515
}
1616

17-
public RelayCommand(Action<object?> execute, Predicate<object?>? canExecute = null)
17+
public RelayCommand(Action execute, Func<bool>? canExecute = null)
1818
{
1919
this.execute = execute;
2020
this.canExecute = canExecute;
2121
}
2222

2323
public bool CanExecute(object? parameter)
2424
{
25-
return canExecute?.Invoke(parameter) != false;
25+
return canExecute?.Invoke() != false;
2626
}
2727

2828
public void Execute(object? parameter)
2929
{
30-
execute(parameter);
30+
execute();
3131
}
3232
}
33+
34+
public sealed class RelayCommand<T> : ICommand
35+
{
36+
private readonly Action<T?> execute;
37+
private readonly Predicate<T?>? canExecute;
38+
39+
public event EventHandler? CanExecuteChanged
40+
{
41+
add => CommandManager.RequerySuggested += value;
42+
remove => CommandManager.RequerySuggested -= value;
43+
}
44+
45+
public RelayCommand(Action<T?> execute, Predicate<T?>? canExecute = null)
46+
{
47+
this.execute = execute;
48+
this.canExecute = canExecute;
49+
}
50+
51+
public bool CanExecute(T? parameter) => canExecute?.Invoke(parameter) != false;
52+
public void Execute(T? parameter) => execute(parameter);
53+
54+
public bool CanExecute(object? parameter) => CanExecute((T?)parameter);
55+
public void Execute(object? parameter) => Execute((T?)parameter);
56+
}
3357
}

Flow.Launcher.Plugin.OneNote/UI/ViewModels/ChangeKeywordViewModel.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ public ChangeKeywordViewModel(SettingsViewModel settingsViewModel, PluginInitCon
1919
closeAction = close;
2020
keywords = settingsViewModel.Keywords;
2121
SelectedKeyword = settingsViewModel.SelectedKeyword!;
22-
ChangeKeywordCommand = new RelayCommand(
23-
keyword => ChangeKeyword((string)keyword),
24-
keyword => CanChangeKeyword((string)keyword));
25-
CloseCommand = new RelayCommand(_ => closeAction.Invoke());
22+
ChangeKeywordCommand = new RelayCommand<string>(ChangeKeyword!, CanChangeKeyword);
23+
CloseCommand = new RelayCommand(closeAction.Invoke);
2624
}
2725
public KeywordViewModel SelectedKeyword { get; }
2826

@@ -36,7 +34,7 @@ public string? ErrorMessage
3634
private set => SetProperty(ref errorMessage, value);
3735
}
3836

39-
private bool CanChangeKeyword(string newKeyword)
37+
private bool CanChangeKeyword(string? newKeyword)
4038
{
4139
if (string.IsNullOrWhiteSpace(newKeyword))
4240
{
@@ -66,6 +64,7 @@ private void ChangeKeyword(string newKeyword)
6664
{
6765
SelectedKeyword.Keyword.ChangeKeyword(newKeyword.Trim());
6866
context.API.SaveSettingJsonStorage<Settings>();
67+
context.API.ReQuery();
6968
closeAction.Invoke();
7069
}
7170
}
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
using System;
2-
using System.Linq;
32
using Flow.Launcher.Plugin.OneNote.Icons;
43

54
namespace Flow.Launcher.Plugin.OneNote.UI.ViewModels
65
{
76
public class IconThemeViewModel
87
{
9-
private IconThemeViewModel(IconTheme iconTheme, PluginInitContext context)
8+
public IconThemeViewModel(IconTheme iconTheme, PluginInitContext context)
109
{
1110
IconTheme = iconTheme;
1211
if (iconTheme == IconTheme.System)
1312
{
1413
Name = "FL Default";
15-
ImageUri = GetUri(IconTheme.Light.ToString(), context);
16-
ImageUri2 = GetUri(IconTheme.Dark.ToString(), context);
14+
ImageUri = GetUri(nameof(IconTheme.Light), context);
15+
ImageUri2 = GetUri(nameof(IconTheme.Dark), context);
1716
Tooltip = "Matches Flow Launcher's app theme";
1817
}
1918
else
2019
{
21-
Name = Enum.GetName(iconTheme);
20+
Name = iconTheme.ToString();
2221
ImageUri = GetUri(Name, context);
2322
}
2423
}
@@ -29,11 +28,7 @@ private static Uri GetUri(string theme, PluginInitContext context) =>
2928
public string Name { get; }
3029
public IconTheme IconTheme { get; }
3130
public Uri ImageUri { get; }
32-
public Uri ImageUri2 { get; }
33-
public string Tooltip { get; }
34-
35-
public static IconThemeViewModel[] GetIconThemeViewModels(PluginInitContext context) =>
36-
Enum.GetValues<IconTheme>().Select(iconTheme => new IconThemeViewModel(iconTheme, context)).ToArray();
31+
public Uri? ImageUri2 { get; }
32+
public string? Tooltip { get; }
3733
}
38-
3934
}

Flow.Launcher.Plugin.OneNote/UI/ViewModels/KeywordViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public KeywordViewModel(string keywordName, Keyword keyword)
1414
}
1515
};
1616
}
17-
public string Name { get; private init; }
17+
public string Name { get; }
1818
public Keyword Keyword { get; }
1919
}
2020
}

Flow.Launcher.Plugin.OneNote/UI/ViewModels/NewOneNotePageViewModel.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ namespace Flow.Launcher.Plugin.OneNote.UI.ViewModels
77
{
88
public class NewOneNotePageViewModel : Model
99
{
10-
private string pageTitle;
11-
private string pageContent;
10+
private string pageTitle = string.Empty;
11+
private string pageContent = string.Empty;
1212
private readonly OneNoteSection section;
1313
private readonly PluginInitContext context;
1414

@@ -17,8 +17,8 @@ public NewOneNotePageViewModel(PluginInitContext context, OneNoteSection section
1717
this.context = context;
1818
this.section = section;
1919
PageTitle = pageTitle;
20-
CreateCommand = new RelayCommand(_ => CreatePage(false));
21-
CreateAndOpenCommand = new RelayCommand(_ => CreatePage(true));
20+
CreateCommand = new RelayCommand(() => CreatePage(false));
21+
CreateAndOpenCommand = new RelayCommand(() => CreatePage(true));
2222
}
2323

2424
private void CreatePage(bool openImmediately)
@@ -29,7 +29,7 @@ private void CreatePage(bool openImmediately)
2929
var xmlWrap = $"<one:Outline><one:Position x=\"36.0\" y=\"86.4000015258789\" z=\"0\"/><one:Size width=\"72.0\" height=\"13.42771339416504\"/><one:OEChildren><one:OE alignment=\"left\"><one:T><![CDATA[{PageContent}]]></one:T></one:OE></one:OEChildren></one:Outline>";
3030
pageContentXml = pageContentXml.Insert(pageContentXml.IndexOf("</one:Page>", StringComparison.Ordinal), xmlWrap);
3131
OneNoteApplication.UpdatePageContent(pageContentXml);
32-
Main.ForceReQuery();
32+
context.API.ReQuery();
3333
if (openImmediately)
3434
{
3535
page.OpenInOneNote();
@@ -43,6 +43,7 @@ private void CreatePage(bool openImmediately)
4343
$"{context.CurrentPluginMetadata.PluginDirectory}/{IconProvider.Logo}");
4444
}
4545
}
46+
4647
public string PageTitle
4748
{
4849
get => pageTitle;

Flow.Launcher.Plugin.OneNote/UI/ViewModels/SettingsViewModel.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using System.Reflection;
34
using System.Threading.Tasks;
45
using System.Windows.Input;
@@ -11,7 +12,7 @@ namespace Flow.Launcher.Plugin.OneNote.UI.ViewModels
1112
public class SettingsViewModel : Model
1213
{
1314
private readonly IconProvider iconProvider;
14-
private KeywordViewModel selectedKeyword;
15+
private KeywordViewModel? selectedKeyword;
1516

1617
public SettingsViewModel(PluginInitContext context, Settings settings, IconProvider iconProvider)
1718
{
@@ -22,18 +23,20 @@ public SettingsViewModel(PluginInitContext context, Settings settings, IconProvi
2223
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
2324
.Select(p => new KeywordViewModel(p.Name.Humanize(LetterCasing.Title), (Keyword)p.GetValue(settings.Keywords)!))
2425
.ToArray();
25-
IconThemes = IconThemeViewModel.GetIconThemeViewModels(context);
26+
IconThemes = Enum.GetValues<IconTheme>()
27+
.Select(iconTheme => new IconThemeViewModel(iconTheme, context))
28+
.ToArray();
2629

2730
EditCommand = new RelayCommand(
28-
_ => new Views.ChangeKeywordWindow(this, context).ShowDialog(), //Avert your eyes! This is not MVVM!
29-
_ => SelectedKeyword != null);
31+
() => new Views.ChangeKeywordWindow(this, context).ShowDialog(), //Avert your eyes! This is not MVVM!
32+
() => SelectedKeyword != null);
3033

3134
OpenGeneratedIconsFolderCommand = new RelayCommand(
32-
_ => context.API.OpenDirectory(iconProvider.GeneratedImagesDirectoryInfo.FullName));
35+
() => context.API.OpenDirectory(iconProvider.GeneratedImagesDirectoryInfo.FullName));
3336

3437
ClearCachedIconsCommand = new RelayCommand(
35-
async _ => await ClearCachedIcons(),
36-
_ => iconProvider.CachedIconCount > 0);
38+
async () => await ClearCachedIcons(),
39+
() => iconProvider.CachedIconCount > 0);
3740

3841
iconProvider.PropertyChanged += (_, args) =>
3942
{
@@ -47,7 +50,7 @@ public SettingsViewModel(PluginInitContext context, Settings settings, IconProvi
4750
{
4851
if (args.PropertyName == nameof(Settings.IconTheme))
4952
{
50-
Main.ForceReQuery();
53+
context.API.ReQuery();
5154
}
5255
};
5356
SelectedKeyword = Keywords[0];
@@ -64,7 +67,7 @@ public SettingsViewModel(PluginInitContext context, Settings settings, IconProvi
6467
.Bytes()
6568
.Humanize();
6669

67-
public KeywordViewModel SelectedKeyword
70+
public KeywordViewModel? SelectedKeyword
6871
{
6972
get => selectedKeyword;
7073
set => SetProperty(ref selectedKeyword, value);

0 commit comments

Comments
 (0)