Skip to content

Commit 0034036

Browse files
committed
Remove statics
1 parent 9140de7 commit 0034036

File tree

4 files changed

+83
-68
lines changed

4 files changed

+83
-68
lines changed

Main.cs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55

66
namespace Flow.Launcher.Plugin.OneNote
77
{
8-
public class OneNote : IPlugin, IContextMenu
8+
public class OneNotePlugin : IPlugin, IContextMenu
99
{
10-
private static PluginInitContext context;
10+
private PluginInitContext context;
1111
private bool hasOneNote;
1212
private readonly int recentPagesCount = 5;
13-
private static IOneNoteExtNotebook lastSelectedNotebook;
14-
private static IOneNoteExtSection lastSelectedSection;
13+
public IOneNoteExtNotebook lastSelectedNotebook;
14+
public IOneNoteExtSection lastSelectedSection;
1515

16-
public static PluginInitContext Context => context;
17-
public static IOneNoteExtNotebook LastSelectedNotebook { get => lastSelectedNotebook; set => lastSelectedNotebook = value; }
18-
public static IOneNoteExtSection LastSelectedSection { get => lastSelectedSection; set => lastSelectedSection = value; }
16+
private NotebookExplorer notebookExplorer;
17+
private ResultCreator rc;
1918

2019
public void Init(PluginInitContext context)
2120
{
22-
OneNote.context = context;
21+
this.context = context;
2322
try
2423
{
2524
_ = OneNoteProvider.PageItems.Any();
@@ -30,6 +29,8 @@ public void Init(PluginInitContext context)
3029
hasOneNote = false;
3130
return;
3231
}
32+
rc = new ResultCreator(context, this);
33+
notebookExplorer = new NotebookExplorer(context, this, rc);
3334
}
3435

3536
public List<Result> Query(Query query)
@@ -52,25 +53,25 @@ public List<Result> Query(Query query)
5253
{
5354
Title = "Search OneNote pages",
5455
SubTitle = $"Type \"{Constants.StructureKeyword}\" to search by notebook structure or select this option",
55-
AutoCompleteText = $"{Context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}",
56+
AutoCompleteText = $"{context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}",
5657
IcoPath = Constants.LogoIconPath,
5758
Score = 2000,
5859
Action = c =>
5960
{
60-
Context.API.ChangeQuery($"{Context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}");
61+
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}");
6162
return false;
6263
},
6364
});
6465
results.Add(new Result
6566
{
6667
Title = "See recent pages",
6768
SubTitle = $"Type \"{Constants.RecentKeyword}\" to see last modified pages or select this option",
68-
AutoCompleteText = $"{Context.CurrentPluginMetadata.ActionKeyword} {Constants.RecentKeyword}",
69+
AutoCompleteText = $"{context.CurrentPluginMetadata.ActionKeyword} {Constants.RecentKeyword}",
6970
IcoPath = Constants.RecentIconPath,
7071
Score = -1000,
7172
Action = c =>
7273
{
73-
Context.API.ChangeQuery($"{Context.CurrentPluginMetadata.ActionKeyword} {Constants.RecentKeyword}");
74+
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeyword} {Constants.RecentKeyword}");
7475
return false;
7576
},
7677
});
@@ -98,7 +99,7 @@ public List<Result> Query(Query query)
9899
.Take(count)
99100
.Select(pg =>
100101
{
101-
Result result = pg.CreateResult();
102+
Result result = rc.CreatePageResult(pg);
102103
result.SubTitle = $"{GetLastEdited(DateTime.Now - pg.LastModified)}\t{result.SubTitle}";
103104
result.IcoPath = Constants.RecentPageIconPath;
104105
return result;
@@ -109,11 +110,11 @@ public List<Result> Query(Query query)
109110
//Search via notebook structure
110111
//NOTE: There is no nested sections i.e. there is nothing for the Section Group in the structure
111112
if (query.FirstSearch.StartsWith(Constants.StructureKeyword))
112-
return NotebookExplorer.Explore(query);
113+
return notebookExplorer.Explore(query);
113114

114115
//Default search
115116
return OneNoteProvider.FindPages(query.Search)
116-
.Select(page => page.CreateResult(Context.API.FuzzySearch(query.Search, page.Name).MatchData))
117+
.Select(pg => rc.CreatePageResult(pg,context.API.FuzzySearch(query.Search, pg.Name).MatchData))
117118
.ToList();
118119

119120
}
@@ -123,7 +124,7 @@ public List<Result> LoadContextMenus(Result selectedResult)
123124
switch (selectedResult.ContextData)
124125
{
125126
case IOneNoteExtNotebook notebook:
126-
Result result = notebook.CreateResult();
127+
Result result = rc.CreateNotebookResult(notebook);
127128
result.Title = "Open and sync notebook";
128129
result.SubTitle = notebook.Name;
129130
result.ContextData = null;
@@ -134,12 +135,12 @@ public List<Result> LoadContextMenus(Result selectedResult)
134135
.First()
135136
.OpenInOneNote();
136137
notebook.Sync();
137-
LastSelectedNotebook = null;
138+
lastSelectedNotebook = null;
138139
return true;
139140
};
140141
return new List<Result>{result};
141142
case IOneNoteExtSection section:
142-
Result sResult = section.CreateResult(LastSelectedNotebook);
143+
Result sResult = rc.CreateSectionResult(section, lastSelectedNotebook);
143144
sResult.Title = "Open and sync section";
144145
sResult.SubTitle = section.Name;
145146
sResult.ContextData = null;
@@ -149,22 +150,22 @@ public List<Result> LoadContextMenus(Result selectedResult)
149150
.First()
150151
.OpenInOneNote();
151152
section.Sync();
152-
LastSelectedNotebook = null;
153-
LastSelectedSection = null;
153+
lastSelectedNotebook = null;
154+
lastSelectedSection = null;
154155
return true;
155156
};
156-
Result nbResult = lastSelectedNotebook.CreateResult();
157+
Result nbResult = rc.CreateNotebookResult(lastSelectedNotebook);
157158
nbResult.Title = "Open and sync notebook";
158159
nbResult.SubTitle = lastSelectedNotebook.Name;
159160
nbResult.Action = c =>
160161
{
161-
LastSelectedNotebook.Sections.First().Pages
162+
lastSelectedNotebook.Sections.First().Pages
162163
.OrderByDescending(pg => pg.LastModified)
163164
.First()
164165
.OpenInOneNote();
165-
LastSelectedNotebook.Sync();
166-
LastSelectedNotebook = null;
167-
LastSelectedSection = null;
166+
lastSelectedNotebook.Sync();
167+
lastSelectedNotebook = null;
168+
lastSelectedSection = null;
168169
return true;
169170
};
170171
return new List<Result> { sResult, nbResult, };

NotebookExplorer.cs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@
55

66
namespace Flow.Launcher.Plugin.OneNote
77
{
8-
public static class NotebookExplorer
8+
public class NotebookExplorer
99
{
10-
public static List<Result> Explore(Query query)
10+
private PluginInitContext context;
11+
private OneNotePlugin oneNotePlugin;
12+
private ResultCreator rc;
13+
14+
public NotebookExplorer(PluginInitContext context, OneNotePlugin oneNotePlugin, ResultCreator resultCreator)
15+
{
16+
this.context = context;
17+
this.oneNotePlugin = oneNotePlugin;
18+
rc = resultCreator;
19+
}
20+
21+
public List<Result> Explore(Query query)
1122
{
1223
string[] searchStrings = query.Search.Split('\\', StringSplitOptions.None);
1324
string searchString;
@@ -21,17 +32,17 @@ public static List<Result> Explore(Query query)
2132

2233
if (string.IsNullOrWhiteSpace(searchString)) // Do a normall notebook search
2334
{
24-
OneNote.LastSelectedNotebook = null;
25-
return OneNoteProvider.NotebookItems.Select(nb => nb.CreateResult()).ToList();
35+
oneNotePlugin.lastSelectedNotebook = null;
36+
return OneNoteProvider.NotebookItems.Select(nb => rc.CreateNotebookResult(nb)).ToList();
2637
}
2738

2839
return OneNoteProvider.NotebookItems.Where(nb =>
2940
{
30-
if (OneNote.LastSelectedNotebook != null && nb.ID == OneNote.LastSelectedNotebook.ID)
41+
if (oneNotePlugin.lastSelectedNotebook != null && nb.ID == oneNotePlugin.lastSelectedNotebook.ID)
3142
return true;
3243
return TreeQuery(nb.Name, searchString, out highlightData);
3344
})
34-
.Select(nb => nb.CreateResult(highlightData))
45+
.Select(nb => rc.CreateNotebookResult(nb, highlightData))
3546
.ToList();
3647

3748
case 3://Full query for section not complete e.g nb\User Notebook\Happine
@@ -42,16 +53,16 @@ public static List<Result> Explore(Query query)
4253

4354
if (string.IsNullOrWhiteSpace(searchString))
4455
{
45-
OneNote.LastSelectedSection = null;
46-
return OneNote.LastSelectedNotebook.Sections.Select(s => s.CreateResult(OneNote.LastSelectedNotebook)).ToList();
56+
oneNotePlugin.lastSelectedSection = null;
57+
return oneNotePlugin.lastSelectedNotebook.Sections.Select(s => rc.CreateSectionResult(s,oneNotePlugin.lastSelectedNotebook)).ToList();
4758
}
48-
return OneNote.LastSelectedNotebook.Sections.Where(s =>
59+
return oneNotePlugin.lastSelectedNotebook.Sections.Where(s =>
4960
{
50-
if (OneNote.LastSelectedSection != null && s.ID == OneNote.LastSelectedSection.ID)
61+
if (oneNotePlugin.lastSelectedSection != null && s.ID == oneNotePlugin.lastSelectedSection.ID)
5162
return true;
5263
return TreeQuery(s.Name, searchString, out highlightData);
5364
})
54-
.Select(s => s.CreateResult(OneNote.LastSelectedNotebook, highlightData))
65+
.Select(s => rc.CreateSectionResult(s, oneNotePlugin.lastSelectedNotebook, highlightData))
5566
.ToList();
5667

5768
case 4://Searching pages in a section
@@ -64,10 +75,10 @@ public static List<Result> Explore(Query query)
6475
return new List<Result>();
6576

6677
if (string.IsNullOrWhiteSpace(searchString))
67-
return OneNote.LastSelectedSection.Pages.Select(pg => pg.CreateResult(OneNote.LastSelectedSection, OneNote.LastSelectedNotebook)).ToList();
78+
return oneNotePlugin.lastSelectedSection.Pages.Select(pg => rc.CreatePageResult(pg,oneNotePlugin.lastSelectedSection, oneNotePlugin.lastSelectedNotebook)).ToList();
6879

69-
return OneNote.LastSelectedSection.Pages.Where(pg => TreeQuery(pg.Name, searchString, out highlightData))
70-
.Select(pg => pg.CreateResult(OneNote.LastSelectedSection, OneNote.LastSelectedNotebook, highlightData))
80+
return oneNotePlugin.lastSelectedSection.Pages.Where(pg => TreeQuery(pg.Name, searchString, out highlightData))
81+
.Select(pg => rc.CreatePageResult(pg,oneNotePlugin.lastSelectedSection, oneNotePlugin.lastSelectedNotebook, highlightData))
7182
.ToList();
7283

7384
default:
@@ -76,34 +87,34 @@ public static List<Result> Explore(Query query)
7687

7788
}
7889

79-
private static bool ValidateNotebook(string notebookName)
90+
private bool ValidateNotebook(string notebookName)
8091
{
81-
if (OneNote.LastSelectedNotebook == null)
92+
if (oneNotePlugin.lastSelectedNotebook == null)
8293
{
8394
var notebook = OneNoteProvider.NotebookItems.FirstOrDefault(nb => nb.Name == notebookName);
8495
if (notebook == null)
8596
return false;
86-
OneNote.LastSelectedNotebook = notebook;
97+
oneNotePlugin.lastSelectedNotebook = notebook;
8798
return true;
8899
}
89100
return true;
90101
}
91102

92-
private static bool ValidateSection(string sectionName)
103+
private bool ValidateSection(string sectionName)
93104
{
94-
if (OneNote.LastSelectedSection == null) //Check if section is valid
105+
if (oneNotePlugin.lastSelectedSection == null) //Check if section is valid
95106
{
96-
var section = OneNote.LastSelectedNotebook.Sections.FirstOrDefault(s => s.Name == sectionName);
107+
var section = oneNotePlugin.lastSelectedNotebook.Sections.FirstOrDefault(s => s.Name == sectionName);
97108
if (section == null)
98109
return false;
99-
OneNote.LastSelectedSection = section;
110+
oneNotePlugin.lastSelectedSection = section;
100111
return true;
101112
}
102113
return true;
103114
}
104-
private static bool TreeQuery(string itemName, string searchString, out List<int> highlightData)
115+
private bool TreeQuery(string itemName, string searchString, out List<int> highlightData)
105116
{
106-
var matchResult = OneNote.Context.API.FuzzySearch(searchString, itemName);
117+
var matchResult = context.API.FuzzySearch(searchString, itemName);
107118
highlightData = matchResult.MatchData;
108119
return matchResult.IsSearchPrecisionScoreMet();
109120
}

OneNoteItemInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace Flow.Launcher.Plugin.OneNote
99
{
1010
public class OneNoteItemInfo
1111
{
12-
private readonly Dictionary<Color, string> icons;
13-
private readonly string iconDirectory;
14-
private readonly string baseIconPath;
12+
private Dictionary<Color, string> icons;
13+
private string iconDirectory;
14+
private string baseIconPath;
1515

1616
public OneNoteItemInfo(string folderName, string iconName, PluginInitContext context)
1717
{

ResultCreator.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,25 @@
44

55
namespace Flow.Launcher.Plugin.OneNote
66
{
7-
public static class ResultCreator
7+
public class ResultCreator
88
{
9-
private static OneNoteItemInfo notebookInfo;
10-
private static OneNoteItemInfo sectionInfo;
9+
private PluginInitContext context;
10+
private OneNotePlugin oneNotePlugin;
11+
private OneNoteItemInfo notebookInfo;
12+
private OneNoteItemInfo sectionInfo;
1113

12-
static ResultCreator()
14+
public ResultCreator(PluginInitContext context, OneNotePlugin oneNotePlugin)
1315
{
14-
notebookInfo = new OneNoteItemInfo("NotebookIcons", "notebook.png", OneNote.Context);
15-
sectionInfo = new OneNoteItemInfo("SectionIcons", "section.png", OneNote.Context);
16+
this.context = context;
17+
notebookInfo = new OneNoteItemInfo("NotebookIcons", "notebook.png", context);
18+
sectionInfo = new OneNoteItemInfo("SectionIcons", "section.png", context);
1619
}
17-
public static Result CreateResult(this IOneNoteExtPage page, List<int> highlightingData = null)
20+
public Result CreatePageResult(IOneNoteExtPage page, List<int> highlightingData = null)
1821
{
19-
return CreateResult(page, page.Section, page.Notebook, highlightingData);
22+
return CreatePageResult(page, page.Section, page.Notebook, highlightingData);
2023
}
2124

22-
public static Result CreateResult(this IOneNotePage page, IOneNoteSection section, IOneNoteNotebook notebook, List<int> highlightingData = null)
25+
public Result CreatePageResult(IOneNotePage page, IOneNoteSection section, IOneNoteNotebook notebook, List<int> highlightingData = null)
2326
{
2427
var sectionPath = section.Path;
2528
var index = sectionPath.IndexOf(notebook.Name);
@@ -37,15 +40,15 @@ public static Result CreateResult(this IOneNotePage page, IOneNoteSection sectio
3740
TitleHighlightData = highlightingData,
3841
Action = c =>
3942
{
40-
OneNote.LastSelectedNotebook = null;
41-
OneNote.LastSelectedSection = null;
43+
oneNotePlugin.lastSelectedNotebook = null;
44+
oneNotePlugin.lastSelectedSection = null;
4245
page.OpenInOneNote();
4346
return true;
4447
},
4548
};
4649
}
4750

48-
public static Result CreateResult(this IOneNoteExtSection section, IOneNoteExtNotebook notebook, List<int> highlightData = null)
51+
public Result CreateSectionResult(IOneNoteExtSection section, IOneNoteExtNotebook notebook, List<int> highlightData = null)
4952
{
5053
var sectionPath = section.Path;
5154
var index = sectionPath.IndexOf(notebook.Name);
@@ -62,14 +65,14 @@ public static Result CreateResult(this IOneNoteExtSection section, IOneNoteExtNo
6265
IcoPath = sectionInfo.GetIcon(section.Color.Value),
6366
Action = c =>
6467
{
65-
OneNote.LastSelectedSection = section;
66-
OneNote.Context.API.ChangeQuery($"{OneNote.Context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}{OneNote.LastSelectedNotebook.Name}\\{section.Name}\\");
68+
oneNotePlugin.lastSelectedSection = section;
69+
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}{oneNotePlugin.lastSelectedNotebook.Name}\\{section.Name}\\");
6770
return false;
6871
},
6972
};
7073
}
7174

72-
public static Result CreateResult(this IOneNoteExtNotebook notebook, List<int> highlightData = null)
75+
public Result CreateNotebookResult(IOneNoteExtNotebook notebook, List<int> highlightData = null)
7376
{
7477
return new Result
7578
{
@@ -79,8 +82,8 @@ public static Result CreateResult(this IOneNoteExtNotebook notebook, List<int> h
7982
IcoPath = notebookInfo.GetIcon(notebook.Color.Value),
8083
Action = c =>
8184
{
82-
OneNote.LastSelectedNotebook = notebook;
83-
OneNote.Context.API.ChangeQuery($"{OneNote.Context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}{notebook.Name}\\");
85+
oneNotePlugin.lastSelectedNotebook = notebook;
86+
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}{notebook.Name}\\");
8487
return false;
8588
},
8689
};

0 commit comments

Comments
 (0)