Skip to content

Commit

Permalink
Remove statics
Browse files Browse the repository at this point in the history
  • Loading branch information
Odotocodot committed Dec 10, 2022
1 parent 9140de7 commit 0034036
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 68 deletions.
51 changes: 26 additions & 25 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<Result> Query(Query query)
Expand All @@ -52,25 +53,25 @@ public List<Result> 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;
},
});
results.Add(new Result
{
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;
},
});
Expand Down Expand Up @@ -98,7 +99,7 @@ public List<Result> 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;
Expand All @@ -109,11 +110,11 @@ public List<Result> 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();

}
Expand All @@ -123,7 +124,7 @@ public List<Result> 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;
Expand All @@ -134,12 +135,12 @@ public List<Result> LoadContextMenus(Result selectedResult)
.First()
.OpenInOneNote();
notebook.Sync();
LastSelectedNotebook = null;
lastSelectedNotebook = null;
return true;
};
return new List<Result>{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;
Expand All @@ -149,22 +150,22 @@ public List<Result> 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<Result> { sResult, nbResult, };
Expand Down
57 changes: 34 additions & 23 deletions NotebookExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@

namespace Flow.Launcher.Plugin.OneNote
{
public static class NotebookExplorer
public class NotebookExplorer
{
public static List<Result> 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<Result> Explore(Query query)
{
string[] searchStrings = query.Search.Split('\\', StringSplitOptions.None);
string searchString;
Expand All @@ -21,17 +32,17 @@ public static List<Result> 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
Expand All @@ -42,16 +53,16 @@ public static List<Result> 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
Expand All @@ -64,10 +75,10 @@ public static List<Result> Explore(Query query)
return new List<Result>();

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:
Expand All @@ -76,34 +87,34 @@ public static List<Result> 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<int> highlightData)
private bool TreeQuery(string itemName, string searchString, out List<int> highlightData)
{
var matchResult = OneNote.Context.API.FuzzySearch(searchString, itemName);
var matchResult = context.API.FuzzySearch(searchString, itemName);
highlightData = matchResult.MatchData;
return matchResult.IsSearchPrecisionScoreMet();
}
Expand Down
6 changes: 3 additions & 3 deletions OneNoteItemInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace Flow.Launcher.Plugin.OneNote
{
public class OneNoteItemInfo
{
private readonly Dictionary<Color, string> icons;
private readonly string iconDirectory;
private readonly string baseIconPath;
private Dictionary<Color, string> icons;
private string iconDirectory;
private string baseIconPath;

public OneNoteItemInfo(string folderName, string iconName, PluginInitContext context)
{
Expand Down
37 changes: 20 additions & 17 deletions ResultCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> highlightingData = null)
public Result CreatePageResult(IOneNoteExtPage page, List<int> 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<int> highlightingData = null)
public Result CreatePageResult(IOneNotePage page, IOneNoteSection section, IOneNoteNotebook notebook, List<int> highlightingData = null)
{
var sectionPath = section.Path;
var index = sectionPath.IndexOf(notebook.Name);
Expand All @@ -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<int> highlightData = null)
public Result CreateSectionResult(IOneNoteExtSection section, IOneNoteExtNotebook notebook, List<int> highlightData = null)
{
var sectionPath = section.Path;
var index = sectionPath.IndexOf(notebook.Name);
Expand All @@ -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<int> highlightData = null)
public Result CreateNotebookResult(IOneNoteExtNotebook notebook, List<int> highlightData = null)
{
return new Result
{
Expand All @@ -79,8 +82,8 @@ public static Result CreateResult(this IOneNoteExtNotebook notebook, List<int> 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;
},
};
Expand Down

0 comments on commit 0034036

Please sign in to comment.