diff --git a/src/Main.cs b/src/Main.cs index 27cfa6a..ca30e82 100644 --- a/src/Main.cs +++ b/src/Main.cs @@ -72,13 +72,14 @@ public async Task> QueryAsync(Query query, CancellationToken token) return await this._togglTrack.GetDefaultHotKeys(); } - return query.FirstSearch.ToLower() switch + return (query.FirstSearch.ToLower()) switch { Settings.StartCommand => await this._togglTrack.RequestStartEntry(token, query), Settings.EditCommand => await this._togglTrack.RequestEditEntry(token, query), Settings.StopCommand => await this._togglTrack.RequestStopEntry(token, query), Settings.DeleteCommand => await this._togglTrack.RequestDeleteEntry(token), Settings.ContinueCommand => await this._togglTrack.RequestContinueEntry(token, query), + Settings.ViewCommand => await this._togglTrack.RequestViewReports(token, query), _ => (await this._togglTrack.GetDefaultHotKeys()) .FindAll(result => { diff --git a/src/Settings.cs b/src/Settings.cs index ad7633c..6ae96ed 100644 --- a/src/Settings.cs +++ b/src/Settings.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; + namespace Flow.Launcher.Plugin.TogglTrack { /// @@ -10,15 +12,52 @@ public class Settings internal const string StopCommand = "stop"; internal const string DeleteCommand = "delete"; internal const string ContinueCommand = "continue"; + internal const string ViewCommand = "view"; internal const string BrowserCommand = "browser"; internal const string RefreshCommand = "refresh"; internal const string EditProjectFlag = "-p"; internal const string TimeSpanFlag = "-t"; + internal static readonly List ViewDurationArguments = new List + { + new ViewDuration + ( + "day", + "today's" + ), + new ViewDuration + ( + "week", + "this week's" + ), + new ViewDuration + ( + "month", + "this month's" + ), + new ViewDuration + ( + "year", + "this year's" + ), + }; + /// /// Toggl Track API Token. /// public string ApiToken { get; set; } = string.Empty; } + + public class ViewDuration + { + public string argument; + public string spanString; + + public ViewDuration(string argument, string spanString) + { + this.argument = argument; + this.spanString = spanString; + } + } } \ No newline at end of file diff --git a/src/TogglTrack.cs b/src/TogglTrack.cs index 0dfe9eb..0a40521 100644 --- a/src/TogglTrack.cs +++ b/src/TogglTrack.cs @@ -241,6 +241,19 @@ internal async ValueTask> GetDefaultHotKeys() }, }, new Result + { + Title = Settings.ViewCommand, + SubTitle = "View tracked time reports", + IcoPath = "view.png", + AutoCompleteText = $"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.ViewCommand} ", + Score = 5, + Action = c => + { + this._context.API.ChangeQuery($"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.ViewCommand} "); + return false; + }, + }, + new Result { Title = Settings.BrowserCommand, SubTitle = "Open Toggl Track in browser", @@ -1291,5 +1304,73 @@ internal async ValueTask> RequestContinueEntry(CancellationToken to return this._context.API.FuzzySearch(query.SecondToEndSearch, result.Title).Score > 0; }); } + + internal async ValueTask> RequestViewReports(CancellationToken token, Query query) + { + if (token.IsCancellationRequested) + { + return new List(); + } + + // var me = await this._GetMe(); + // if (me is null) + // { + // return this.NotifyUnknownError(); + // } + + var timeEntries = await this._GetTimeEntries(); + if (timeEntries is null) + { + return new List + { + new Result + { + Title = $"No previous time entries", + SubTitle = "There are no time entries to report on.", + IcoPath = this._context.CurrentPluginMetadata.IcoPath, + Action = c => + { + return true; + }, + }, + }; + } + + if (query.SearchTerms.Length == 1 || !Settings.ViewDurationArguments.Exists(duration => duration.argument == query.SearchTerms[1])) + { + var results = Settings.ViewDurationArguments.ConvertAll(duration => + { + return new Result + { + Title = duration.argument, + SubTitle = $"View {duration.spanString} tracked time report", + IcoPath = "view.png", + AutoCompleteText = $"{query.ActionKeyword} {Settings.ViewCommand} {duration.argument} ", + Score = Settings.ViewDurationArguments.Count - Settings.ViewDurationArguments.IndexOf(duration), + Action = c => + { + this._context.API.ChangeQuery($"{query.ActionKeyword} {Settings.ViewCommand} {duration.argument} ", true); + return false; + }, + }; + }); + + return (string.IsNullOrWhiteSpace(query.SecondToEndSearch)) + ? results + : results.FindAll(result => + { + return this._context.API.FuzzySearch(query.SecondToEndSearch, result.Title).Score > 0; + }); + } + + var reports = new List(); + + return (string.IsNullOrWhiteSpace(query.SecondToEndSearch)) + ? reports + : reports.FindAll(result => + { + return this._context.API.FuzzySearch(query.SecondToEndSearch, result.Title).Score > 0; + }); + } } } \ No newline at end of file