From 00340368e75f478c41f4dbe14886d0d155e0699d Mon Sep 17 00:00:00 2001 From: Odotocodot <48138990+Odotocodot@users.noreply.github.com> Date: Sat, 10 Dec 2022 17:12:36 +0000 Subject: [PATCH] Remove statics --- Main.cs | 51 ++++++++++++++++++++-------------------- NotebookExplorer.cs | 57 +++++++++++++++++++++++++++------------------ OneNoteItemInfo.cs | 6 ++--- ResultCreator.cs | 37 +++++++++++++++-------------- 4 files changed, 83 insertions(+), 68 deletions(-) diff --git a/Main.cs b/Main.cs index c5d3ec6..6e78f49 100644 --- a/Main.cs +++ b/Main.cs @@ -5,21 +5,20 @@ namespace Flow.Launcher.Plugin.OneNote { - public class OneNote : IPlugin, IContextMenu + public class OneNotePlugin : IPlugin, IContextMenu { - private static PluginInitContext context; + private PluginInitContext context; private bool hasOneNote; private readonly int recentPagesCount = 5; - private static IOneNoteExtNotebook lastSelectedNotebook; - private static IOneNoteExtSection lastSelectedSection; + public IOneNoteExtNotebook lastSelectedNotebook; + public IOneNoteExtSection lastSelectedSection; - public static PluginInitContext Context => context; - public static IOneNoteExtNotebook LastSelectedNotebook { get => lastSelectedNotebook; set => lastSelectedNotebook = value; } - public static IOneNoteExtSection LastSelectedSection { get => lastSelectedSection; set => lastSelectedSection = value; } + private NotebookExplorer notebookExplorer; + private ResultCreator rc; public void Init(PluginInitContext context) { - OneNote.context = context; + this.context = context; try { _ = OneNoteProvider.PageItems.Any(); @@ -30,6 +29,8 @@ public void Init(PluginInitContext context) hasOneNote = false; return; } + rc = new ResultCreator(context, this); + notebookExplorer = new NotebookExplorer(context, this, rc); } public List Query(Query query) @@ -52,12 +53,12 @@ public List Query(Query query) { Title = "Search OneNote pages", SubTitle = $"Type \"{Constants.StructureKeyword}\" to search by notebook structure or select this option", - AutoCompleteText = $"{Context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}", + AutoCompleteText = $"{context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}", IcoPath = Constants.LogoIconPath, Score = 2000, Action = c => { - Context.API.ChangeQuery($"{Context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}"); + context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}"); return false; }, }); @@ -65,12 +66,12 @@ public List Query(Query query) { Title = "See recent pages", SubTitle = $"Type \"{Constants.RecentKeyword}\" to see last modified pages or select this option", - AutoCompleteText = $"{Context.CurrentPluginMetadata.ActionKeyword} {Constants.RecentKeyword}", + AutoCompleteText = $"{context.CurrentPluginMetadata.ActionKeyword} {Constants.RecentKeyword}", IcoPath = Constants.RecentIconPath, Score = -1000, Action = c => { - Context.API.ChangeQuery($"{Context.CurrentPluginMetadata.ActionKeyword} {Constants.RecentKeyword}"); + context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeyword} {Constants.RecentKeyword}"); return false; }, }); @@ -98,7 +99,7 @@ public List Query(Query query) .Take(count) .Select(pg => { - Result result = pg.CreateResult(); + Result result = rc.CreatePageResult(pg); result.SubTitle = $"{GetLastEdited(DateTime.Now - pg.LastModified)}\t{result.SubTitle}"; result.IcoPath = Constants.RecentPageIconPath; return result; @@ -109,11 +110,11 @@ public List Query(Query query) //Search via notebook structure //NOTE: There is no nested sections i.e. there is nothing for the Section Group in the structure if (query.FirstSearch.StartsWith(Constants.StructureKeyword)) - return NotebookExplorer.Explore(query); + return notebookExplorer.Explore(query); //Default search return OneNoteProvider.FindPages(query.Search) - .Select(page => page.CreateResult(Context.API.FuzzySearch(query.Search, page.Name).MatchData)) + .Select(pg => rc.CreatePageResult(pg,context.API.FuzzySearch(query.Search, pg.Name).MatchData)) .ToList(); } @@ -123,7 +124,7 @@ public List LoadContextMenus(Result selectedResult) switch (selectedResult.ContextData) { case IOneNoteExtNotebook notebook: - Result result = notebook.CreateResult(); + Result result = rc.CreateNotebookResult(notebook); result.Title = "Open and sync notebook"; result.SubTitle = notebook.Name; result.ContextData = null; @@ -134,12 +135,12 @@ public List LoadContextMenus(Result selectedResult) .First() .OpenInOneNote(); notebook.Sync(); - LastSelectedNotebook = null; + lastSelectedNotebook = null; return true; }; return new List{result}; case IOneNoteExtSection section: - Result sResult = section.CreateResult(LastSelectedNotebook); + Result sResult = rc.CreateSectionResult(section, lastSelectedNotebook); sResult.Title = "Open and sync section"; sResult.SubTitle = section.Name; sResult.ContextData = null; @@ -149,22 +150,22 @@ public List LoadContextMenus(Result selectedResult) .First() .OpenInOneNote(); section.Sync(); - LastSelectedNotebook = null; - LastSelectedSection = null; + lastSelectedNotebook = null; + lastSelectedSection = null; return true; }; - Result nbResult = lastSelectedNotebook.CreateResult(); + Result nbResult = rc.CreateNotebookResult(lastSelectedNotebook); nbResult.Title = "Open and sync notebook"; nbResult.SubTitle = lastSelectedNotebook.Name; nbResult.Action = c => { - LastSelectedNotebook.Sections.First().Pages + lastSelectedNotebook.Sections.First().Pages .OrderByDescending(pg => pg.LastModified) .First() .OpenInOneNote(); - LastSelectedNotebook.Sync(); - LastSelectedNotebook = null; - LastSelectedSection = null; + lastSelectedNotebook.Sync(); + lastSelectedNotebook = null; + lastSelectedSection = null; return true; }; return new List { sResult, nbResult, }; diff --git a/NotebookExplorer.cs b/NotebookExplorer.cs index 55f8ed6..409ce12 100644 --- a/NotebookExplorer.cs +++ b/NotebookExplorer.cs @@ -5,9 +5,20 @@ namespace Flow.Launcher.Plugin.OneNote { - public static class NotebookExplorer + public class NotebookExplorer { - public static List Explore(Query query) + private PluginInitContext context; + private OneNotePlugin oneNotePlugin; + private ResultCreator rc; + + public NotebookExplorer(PluginInitContext context, OneNotePlugin oneNotePlugin, ResultCreator resultCreator) + { + this.context = context; + this.oneNotePlugin = oneNotePlugin; + rc = resultCreator; + } + + public List Explore(Query query) { string[] searchStrings = query.Search.Split('\\', StringSplitOptions.None); string searchString; @@ -21,17 +32,17 @@ public static List Explore(Query query) if (string.IsNullOrWhiteSpace(searchString)) // Do a normall notebook search { - OneNote.LastSelectedNotebook = null; - return OneNoteProvider.NotebookItems.Select(nb => nb.CreateResult()).ToList(); + oneNotePlugin.lastSelectedNotebook = null; + return OneNoteProvider.NotebookItems.Select(nb => rc.CreateNotebookResult(nb)).ToList(); } return OneNoteProvider.NotebookItems.Where(nb => { - if (OneNote.LastSelectedNotebook != null && nb.ID == OneNote.LastSelectedNotebook.ID) + if (oneNotePlugin.lastSelectedNotebook != null && nb.ID == oneNotePlugin.lastSelectedNotebook.ID) return true; return TreeQuery(nb.Name, searchString, out highlightData); }) - .Select(nb => nb.CreateResult(highlightData)) + .Select(nb => rc.CreateNotebookResult(nb, highlightData)) .ToList(); case 3://Full query for section not complete e.g nb\User Notebook\Happine @@ -42,16 +53,16 @@ public static List Explore(Query query) if (string.IsNullOrWhiteSpace(searchString)) { - OneNote.LastSelectedSection = null; - return OneNote.LastSelectedNotebook.Sections.Select(s => s.CreateResult(OneNote.LastSelectedNotebook)).ToList(); + oneNotePlugin.lastSelectedSection = null; + return oneNotePlugin.lastSelectedNotebook.Sections.Select(s => rc.CreateSectionResult(s,oneNotePlugin.lastSelectedNotebook)).ToList(); } - return OneNote.LastSelectedNotebook.Sections.Where(s => + return oneNotePlugin.lastSelectedNotebook.Sections.Where(s => { - if (OneNote.LastSelectedSection != null && s.ID == OneNote.LastSelectedSection.ID) + if (oneNotePlugin.lastSelectedSection != null && s.ID == oneNotePlugin.lastSelectedSection.ID) return true; return TreeQuery(s.Name, searchString, out highlightData); }) - .Select(s => s.CreateResult(OneNote.LastSelectedNotebook, highlightData)) + .Select(s => rc.CreateSectionResult(s, oneNotePlugin.lastSelectedNotebook, highlightData)) .ToList(); case 4://Searching pages in a section @@ -64,10 +75,10 @@ public static List Explore(Query query) return new List(); if (string.IsNullOrWhiteSpace(searchString)) - return OneNote.LastSelectedSection.Pages.Select(pg => pg.CreateResult(OneNote.LastSelectedSection, OneNote.LastSelectedNotebook)).ToList(); + return oneNotePlugin.lastSelectedSection.Pages.Select(pg => rc.CreatePageResult(pg,oneNotePlugin.lastSelectedSection, oneNotePlugin.lastSelectedNotebook)).ToList(); - return OneNote.LastSelectedSection.Pages.Where(pg => TreeQuery(pg.Name, searchString, out highlightData)) - .Select(pg => pg.CreateResult(OneNote.LastSelectedSection, OneNote.LastSelectedNotebook, highlightData)) + return oneNotePlugin.lastSelectedSection.Pages.Where(pg => TreeQuery(pg.Name, searchString, out highlightData)) + .Select(pg => rc.CreatePageResult(pg,oneNotePlugin.lastSelectedSection, oneNotePlugin.lastSelectedNotebook, highlightData)) .ToList(); default: @@ -76,34 +87,34 @@ public static List Explore(Query query) } - private static bool ValidateNotebook(string notebookName) + private bool ValidateNotebook(string notebookName) { - if (OneNote.LastSelectedNotebook == null) + if (oneNotePlugin.lastSelectedNotebook == null) { var notebook = OneNoteProvider.NotebookItems.FirstOrDefault(nb => nb.Name == notebookName); if (notebook == null) return false; - OneNote.LastSelectedNotebook = notebook; + oneNotePlugin.lastSelectedNotebook = notebook; return true; } return true; } - private static bool ValidateSection(string sectionName) + private bool ValidateSection(string sectionName) { - if (OneNote.LastSelectedSection == null) //Check if section is valid + if (oneNotePlugin.lastSelectedSection == null) //Check if section is valid { - var section = OneNote.LastSelectedNotebook.Sections.FirstOrDefault(s => s.Name == sectionName); + var section = oneNotePlugin.lastSelectedNotebook.Sections.FirstOrDefault(s => s.Name == sectionName); if (section == null) return false; - OneNote.LastSelectedSection = section; + oneNotePlugin.lastSelectedSection = section; return true; } return true; } - private static bool TreeQuery(string itemName, string searchString, out List highlightData) + private bool TreeQuery(string itemName, string searchString, out List highlightData) { - var matchResult = OneNote.Context.API.FuzzySearch(searchString, itemName); + var matchResult = context.API.FuzzySearch(searchString, itemName); highlightData = matchResult.MatchData; return matchResult.IsSearchPrecisionScoreMet(); } diff --git a/OneNoteItemInfo.cs b/OneNoteItemInfo.cs index 91210e0..9366881 100644 --- a/OneNoteItemInfo.cs +++ b/OneNoteItemInfo.cs @@ -9,9 +9,9 @@ namespace Flow.Launcher.Plugin.OneNote { public class OneNoteItemInfo { - private readonly Dictionary icons; - private readonly string iconDirectory; - private readonly string baseIconPath; + private Dictionary icons; + private string iconDirectory; + private string baseIconPath; public OneNoteItemInfo(string folderName, string iconName, PluginInitContext context) { diff --git a/ResultCreator.cs b/ResultCreator.cs index e230206..740d1a1 100644 --- a/ResultCreator.cs +++ b/ResultCreator.cs @@ -4,22 +4,25 @@ namespace Flow.Launcher.Plugin.OneNote { - public static class ResultCreator + public class ResultCreator { - private static OneNoteItemInfo notebookInfo; - private static OneNoteItemInfo sectionInfo; + private PluginInitContext context; + private OneNotePlugin oneNotePlugin; + private OneNoteItemInfo notebookInfo; + private OneNoteItemInfo sectionInfo; - static ResultCreator() + public ResultCreator(PluginInitContext context, OneNotePlugin oneNotePlugin) { - notebookInfo = new OneNoteItemInfo("NotebookIcons", "notebook.png", OneNote.Context); - sectionInfo = new OneNoteItemInfo("SectionIcons", "section.png", OneNote.Context); + this.context = context; + notebookInfo = new OneNoteItemInfo("NotebookIcons", "notebook.png", context); + sectionInfo = new OneNoteItemInfo("SectionIcons", "section.png", context); } - public static Result CreateResult(this IOneNoteExtPage page, List highlightingData = null) + public Result CreatePageResult(IOneNoteExtPage page, List highlightingData = null) { - return CreateResult(page, page.Section, page.Notebook, highlightingData); + return CreatePageResult(page, page.Section, page.Notebook, highlightingData); } - public static Result CreateResult(this IOneNotePage page, IOneNoteSection section, IOneNoteNotebook notebook, List highlightingData = null) + public Result CreatePageResult(IOneNotePage page, IOneNoteSection section, IOneNoteNotebook notebook, List highlightingData = null) { var sectionPath = section.Path; var index = sectionPath.IndexOf(notebook.Name); @@ -37,15 +40,15 @@ public static Result CreateResult(this IOneNotePage page, IOneNoteSection sectio TitleHighlightData = highlightingData, Action = c => { - OneNote.LastSelectedNotebook = null; - OneNote.LastSelectedSection = null; + oneNotePlugin.lastSelectedNotebook = null; + oneNotePlugin.lastSelectedSection = null; page.OpenInOneNote(); return true; }, }; } - public static Result CreateResult(this IOneNoteExtSection section, IOneNoteExtNotebook notebook, List highlightData = null) + public Result CreateSectionResult(IOneNoteExtSection section, IOneNoteExtNotebook notebook, List highlightData = null) { var sectionPath = section.Path; var index = sectionPath.IndexOf(notebook.Name); @@ -62,14 +65,14 @@ public static Result CreateResult(this IOneNoteExtSection section, IOneNoteExtNo IcoPath = sectionInfo.GetIcon(section.Color.Value), Action = c => { - OneNote.LastSelectedSection = section; - OneNote.Context.API.ChangeQuery($"{OneNote.Context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}{OneNote.LastSelectedNotebook.Name}\\{section.Name}\\"); + oneNotePlugin.lastSelectedSection = section; + context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}{oneNotePlugin.lastSelectedNotebook.Name}\\{section.Name}\\"); return false; }, }; } - public static Result CreateResult(this IOneNoteExtNotebook notebook, List highlightData = null) + public Result CreateNotebookResult(IOneNoteExtNotebook notebook, List highlightData = null) { return new Result { @@ -79,8 +82,8 @@ public static Result CreateResult(this IOneNoteExtNotebook notebook, List h IcoPath = notebookInfo.GetIcon(notebook.Color.Value), Action = c => { - OneNote.LastSelectedNotebook = notebook; - OneNote.Context.API.ChangeQuery($"{OneNote.Context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}{notebook.Name}\\"); + oneNotePlugin.lastSelectedNotebook = notebook; + context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}{notebook.Name}\\"); return false; }, };