Skip to content

Commit 9ee9ae8

Browse files
committed
Refactor keywords
1 parent aafa81f commit 9ee9ae8

5 files changed

Lines changed: 68 additions & 42 deletions

File tree

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,48 @@
1-
namespace Flow.Launcher.Plugin.OneNote
1+
using System;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
5+
namespace Flow.Launcher.Plugin.OneNote
26
{
7+
38
public class Keywords
49
{
510
public const string NotebookExplorerSeparator = "\\";
6-
public string NotebookExplorer { get; set; } = $"nb:{NotebookExplorerSeparator}";
7-
public string RecentPages { get; set; } = "rp:";
8-
public string TitleSearch { get; set; } = "*";
9-
public string ScopedSearch { get; set; } = ">";
11+
public Keyword NotebookExplorer { get; set; } = new($"nb:{NotebookExplorerSeparator}");
12+
public Keyword RecentPages { get; set; } = new ("rp:");
13+
public Keyword TitleSearch { get; set; } = new ("*");
14+
public Keyword ScopedSearch { get; set; } = new (">");
15+
1016
}
17+
18+
[JsonConverter(typeof(KeywordJsonConverter))]
19+
public class Keyword : BaseModel
20+
{
21+
public Keyword(string value)
22+
{
23+
Value = value;
24+
}
25+
public string Value { get; private set; }
26+
27+
public void ChangeKeyword(string newValue)
28+
{
29+
Value = newValue;
30+
OnPropertyChanged(nameof(Value));
31+
}
32+
33+
public int Length => Value.Length;
34+
public static implicit operator string(Keyword keyword) => keyword.Value;
35+
public override string ToString() => Value;
36+
}
37+
38+
//Needed for legacy as keywords where just saved as a string
39+
public class KeywordJsonConverter : JsonConverter<Keyword>
40+
{
41+
public override Keyword Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
42+
=> new(JsonSerializer.Deserialize<string>(ref reader, options));
43+
44+
public override void Write(Utf8JsonWriter writer, Keyword value, JsonSerializerOptions options)
45+
=> JsonSerializer.Serialize(writer, value.Value, options);
46+
}
47+
1148
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using System.Windows.Input;
43

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,27 @@ public class ChangeKeywordViewModel : Model
1010
private readonly KeywordViewModel[] keywords;
1111
private readonly Action closeAction;
1212

13-
private string errorMessage;
13+
private string? errorMessage;
1414

15+
1516
public ChangeKeywordViewModel(SettingsViewModel settingsViewModel, PluginInitContext context, Action close)
1617
{
1718
this.context = context;
1819
closeAction = close;
1920
keywords = settingsViewModel.Keywords;
20-
SelectedKeyword = settingsViewModel.SelectedKeyword;
21+
SelectedKeyword = settingsViewModel.SelectedKeyword!;
2122
ChangeKeywordCommand = new RelayCommand(
2223
keyword => ChangeKeyword((string)keyword),
2324
keyword => CanChangeKeyword((string)keyword));
24-
CloseCommand = new RelayCommand( _=> closeAction?.Invoke());
25+
CloseCommand = new RelayCommand(_ => closeAction.Invoke());
2526
}
2627
public KeywordViewModel SelectedKeyword { get; }
2728

2829
public ICommand CloseCommand { get; }
2930

3031
public ICommand ChangeKeywordCommand { get; }
3132

32-
public string ErrorMessage
33+
public string? ErrorMessage
3334
{
3435
get => errorMessage;
3536
private set => SetProperty(ref errorMessage, value);
@@ -50,7 +51,7 @@ private bool CanChangeKeyword(string newKeyword)
5051
return false;
5152
}
5253

53-
var alreadySetKeyword = keywords.FirstOrDefault(k => k.Keyword == newKeyword);
54+
KeywordViewModel? alreadySetKeyword = keywords.FirstOrDefault(k => k.Keyword == newKeyword);
5455
if (alreadySetKeyword != null)
5556
{
5657
ErrorMessage = $"The new keyword is already set for {alreadySetKeyword.Name}.";
@@ -63,9 +64,9 @@ private bool CanChangeKeyword(string newKeyword)
6364

6465
private void ChangeKeyword(string newKeyword)
6566
{
66-
SelectedKeyword.Keyword = newKeyword.Trim();
67+
SelectedKeyword.Keyword.ChangeKeyword(newKeyword.Trim());
6768
context.API.SaveSettingJsonStorage<Settings>();
68-
closeAction?.Invoke();
69+
closeAction.Invoke();
6970
}
7071
}
7172

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,20 @@
1-
using System.Reflection;
2-
using System.Linq;
3-
using Humanizer;
4-
5-
namespace Flow.Launcher.Plugin.OneNote.UI.ViewModels
1+
namespace Flow.Launcher.Plugin.OneNote.UI.ViewModels
62
{
73
public class KeywordViewModel : BaseModel
84
{
9-
private object Instance { get; init; }
10-
private PropertyInfo PropertyInfo { get; init; }
11-
public string Name { get; private init; }
12-
13-
public string Keyword
5+
public KeywordViewModel(string keywordName, Keyword keyword)
146
{
15-
get => (string)PropertyInfo.GetValue(Instance);
16-
set
7+
Name = keywordName;
8+
Keyword = keyword;
9+
keyword.PropertyChanged += (_, args) =>
1710
{
18-
PropertyInfo.SetValue(Instance, value, null);
19-
OnPropertyChanged();
20-
}
21-
}
22-
23-
public static KeywordViewModel[] GetKeywordViewModels(Keywords keywords)
24-
{
25-
return keywords.GetType()
26-
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
27-
.Select(p => new KeywordViewModel
28-
{
29-
Instance = keywords,
30-
PropertyInfo = p,
31-
Name = p.Name.Humanize(LetterCasing.Title)
32-
})
33-
.ToArray();
11+
if (args.PropertyName == nameof(Keyword.Value))
12+
{
13+
OnPropertyChanged(nameof(Keyword));
14+
}
15+
};
3416
}
17+
public string Name { get; private init; }
18+
public Keyword Keyword { get; }
3519
}
3620
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Linq;
2+
using System.Reflection;
23
using System.Threading.Tasks;
34
using System.Windows.Input;
45
using Flow.Launcher.Plugin.OneNote.Icons;
@@ -16,7 +17,11 @@ public SettingsViewModel(PluginInitContext context, Settings settings, IconProvi
1617
{
1718
this.iconProvider = iconProvider;
1819
Settings = settings;
19-
Keywords = KeywordViewModel.GetKeywordViewModels(settings.Keywords);
20+
Keywords = settings.Keywords
21+
.GetType()
22+
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
23+
.Select(p => new KeywordViewModel(p.Name.Humanize(LetterCasing.Title), (Keyword)p.GetValue(settings.Keywords)!))
24+
.ToArray();
2025
IconThemes = IconThemeViewModel.GetIconThemeViewModels(context);
2126

2227
EditCommand = new RelayCommand(

0 commit comments

Comments
 (0)