Skip to content

Commit

Permalink
feat(view): 🚧 use Dictionary for collection of CommandArguments
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNZL committed May 15, 2023
1 parent 091f5be commit f801621
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 29 deletions.
89 changes: 66 additions & 23 deletions src/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,87 @@ public class Settings
internal const string EditProjectFlag = "-p";
internal const string TimeSpanFlag = "-t";

internal static readonly List<CommandArgument> ViewDurationArguments = new List<CommandArgument>
internal enum ViewDurationKeys
{
Day,
Week,
Month,
Year,
}
internal static readonly Dictionary<Settings.ViewDurationKeys, CommandArgument> ViewDurationArguments = new Dictionary<Settings.ViewDurationKeys, CommandArgument>
{
new CommandArgument
{
Argument = "day",
Interpolation = "today",
Settings.ViewDurationKeys.Day,
new CommandArgument
{
Argument = "day",
Interpolation = "today",
Score = 400
}
},
new CommandArgument
{
Argument = "week",
Interpolation = "this week",
Settings.ViewDurationKeys.Week,
new CommandArgument
{
Argument = "week",
Interpolation = "this week",
Score = 300
}
},
new CommandArgument
{
Argument = "month",
Interpolation = "this month",
Settings.ViewDurationKeys.Month,
new CommandArgument
{
Argument = "month",
Interpolation = "this month",
Score = 200
}
},
new CommandArgument
{
Argument = "year",
Interpolation = "this year",
Settings.ViewDurationKeys.Year,
new CommandArgument
{
Argument = "year",
Interpolation = "this year",
Score = 100
}
},
};
internal static readonly List<CommandArgument> ViewGroupingArguments = new List<CommandArgument>

internal enum ViewGroupingKeys
{
Entries,
Projects,
Clients,
}
internal static readonly Dictionary<Settings.ViewGroupingKeys, CommandArgument> ViewGroupingArguments = new Dictionary<Settings.ViewGroupingKeys, CommandArgument>
{
new CommandArgument
{
Argument = "entries",
Interpolation = "View tracked time entries",
Settings.ViewGroupingKeys.Entries,
new CommandArgument
{
Argument = "entries",
Interpolation = "View tracked time entries",
Score = 300
}
},
new CommandArgument
{
Argument = "projects",
Interpolation = "View tracked time grouped by project",
Settings.ViewGroupingKeys.Projects,
new CommandArgument
{
Argument = "projects",
Interpolation = "View tracked time grouped by project",
Score = 200
}
},
new CommandArgument
{
Argument = "clients",
Interpolation = "View tracked time grouped by client",
Settings.ViewGroupingKeys.Clients,
new CommandArgument
{
Argument = "clients",
Interpolation = "View tracked time grouped by client",
Score = 100
}
},
};

Expand All @@ -72,6 +114,7 @@ public class CommandArgument
#nullable disable
public string Argument { get; set; }
public string Interpolation { get; set; }
public int Score { get; set; }
#nullable enable
}
}
12 changes: 6 additions & 6 deletions src/TogglTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,17 +1339,17 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
/*
* Report duration selection --- tgl view [day | week | month | year]
*/
if (query.SearchTerms.Length == 1 || !Settings.ViewDurationArguments.Exists(duration => duration.Argument == query.SearchTerms[1]))
if (query.SearchTerms.Length == 1 || !Settings.ViewDurationArguments.Values.ToList().Exists(duration => duration.Argument == query.SearchTerms[1]))
{
var durations = Settings.ViewDurationArguments.ConvertAll(duration =>
var durations = Settings.ViewDurationArguments.Values.ToList().ConvertAll(duration =>
{
return new Result
{
Title = duration.Argument,
SubTitle = $"View tracked time report for {duration.Interpolation}",
IcoPath = "view.png",
AutoCompleteText = $"{query.ActionKeyword} {Settings.ViewCommand} {duration.Argument} ",
Score = Settings.ViewDurationArguments.Count - Settings.ViewDurationArguments.IndexOf(duration),
Score = duration.Score,
Action = c =>
{
this._context.API.ChangeQuery($"{query.ActionKeyword} {Settings.ViewCommand} {duration.Argument} ", true);
Expand All @@ -1370,17 +1370,17 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
* Report groupinging selection --- tgl view [duration] [entries | projects | clients]
"View tracked time report by Project"
*/
if (query.SearchTerms.Length == 2 || !Settings.ViewGroupingArguments.Exists(grouping => grouping.Argument == query.SearchTerms[2]))
if (query.SearchTerms.Length == 2 || !Settings.ViewGroupingArguments.Values.ToList().Exists(grouping => grouping.Argument == query.SearchTerms[2]))
{
var groupings = Settings.ViewGroupingArguments.ConvertAll(grouping =>
var groupings = Settings.ViewGroupingArguments.Values.ToList().ConvertAll(grouping =>
{
return new Result
{
Title = grouping.Argument,
SubTitle = grouping.Interpolation,
IcoPath = "view.png",
AutoCompleteText = $"{query.ActionKeyword} {Settings.ViewCommand} {query.SearchTerms[1]} {grouping.Argument} ",
Score = Settings.ViewGroupingArguments.Count - Settings.ViewGroupingArguments.IndexOf(grouping),
Score = grouping.Score,
Action = c =>
{
this._context.API.ChangeQuery($"{query.ActionKeyword} {Settings.ViewCommand} {query.SearchTerms[1]} {grouping.Argument} ", true);
Expand Down

0 comments on commit f801621

Please sign in to comment.