Skip to content

Commit

Permalink
feat(view): 🚧 implement foundations of tgl view command (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNZL committed May 14, 2023
1 parent f7c301c commit e2f4286
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ public async Task<List<Result>> 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 =>
{
Expand Down
39 changes: 39 additions & 0 deletions src/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;

namespace Flow.Launcher.Plugin.TogglTrack
{
/// <Summary>
Expand All @@ -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<ViewDuration> ViewDurationArguments = new List<ViewDuration>
{
new ViewDuration
(
"day",
"today's"
),
new ViewDuration
(
"week",
"this week's"
),
new ViewDuration
(
"month",
"this month's"
),
new ViewDuration
(
"year",
"this year's"
),
};

/// <Summary>
/// Toggl Track API Token.
/// </Summary>
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;
}
}
}
81 changes: 81 additions & 0 deletions src/TogglTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,19 @@ internal async ValueTask<List<Result>> 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",
Expand Down Expand Up @@ -1291,5 +1304,73 @@ internal async ValueTask<List<Result>> RequestContinueEntry(CancellationToken to
return this._context.API.FuzzySearch(query.SecondToEndSearch, result.Title).Score > 0;
});
}

internal async ValueTask<List<Result>> RequestViewReports(CancellationToken token, Query query)
{
if (token.IsCancellationRequested)
{
return new List<Result>();
}

// var me = await this._GetMe();
// if (me is null)
// {
// return this.NotifyUnknownError();
// }

var timeEntries = await this._GetTimeEntries();
if (timeEntries is null)
{
return new List<Result>
{
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<Result>();

return (string.IsNullOrWhiteSpace(query.SecondToEndSearch))
? reports
: reports.FindAll(result =>
{
return this._context.API.FuzzySearch(query.SecondToEndSearch, result.Title).Score > 0;
});
}
}
}

0 comments on commit e2f4286

Please sign in to comment.