Skip to content

Commit

Permalink
feat(view): 🚸 display selected clients's projects
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNZL committed May 17, 2023
1 parent 6f4293e commit 8bd7bd9
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 31 deletions.
13 changes: 10 additions & 3 deletions src/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,34 @@ public enum ViewGroupingKeys
Clients,
Entries,
}
private const string ViewGroupingProjectsArgument = "projects";
private const string ViewGroupingClientsArgument = "clients";
private const string ViewGroupingEntriesArgument = "entries";
internal static readonly List<ViewGroupingCommandArgument> ViewGroupingArguments = new List<ViewGroupingCommandArgument>
{
new ViewGroupingCommandArgument
{
Argument = "projects",
Argument = Settings.ViewGroupingProjectsArgument,
Interpolation = "View tracked time grouped by project",
Score = 300,
Grouping = Settings.ViewGroupingKeys.Projects,
SubArgument = null,
},
new ViewGroupingCommandArgument
{
Argument = "clients",
Argument = Settings.ViewGroupingClientsArgument,
Interpolation = "View tracked time grouped by client",
Score = 200,
Grouping = Settings.ViewGroupingKeys.Clients,
SubArgument = Settings.ViewGroupingProjectsArgument,
},
new ViewGroupingCommandArgument
{
Argument = "entries",
Argument = Settings.ViewGroupingEntriesArgument,
Interpolation = "View tracked time entries",
Score = 100,
Grouping = Settings.ViewGroupingKeys.Entries,
SubArgument = null,
},
};

Expand Down Expand Up @@ -128,6 +134,7 @@ public class ViewGroupingCommandArgument : CommandArgument
{
#nullable disable
public Settings.ViewGroupingKeys Grouping { get; init; }
public string SubArgument { get; init; }
#nullable enable
}
}
118 changes: 90 additions & 28 deletions src/TogglTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal class TogglTrack
private MemoryCache _cache = MemoryCache.Default;

private long? _selectedProjectId = -1;
private long? _selectedClientId = -1;

private enum EditProjectState
{
Expand Down Expand Up @@ -262,6 +263,7 @@ internal List<Result> NotifyUnknownError()
internal async ValueTask<List<Result>> GetDefaultHotKeys()
{
this._selectedProjectId = -1;
this._selectedClientId = -1;
this._editProjectState = TogglTrack.EditProjectState.NoProjectChange;

var results = new List<Result>
Expand Down Expand Up @@ -1398,6 +1400,7 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
if (token.IsCancellationRequested)
{
this._selectedProjectId = -1;
this._selectedClientId = -1;
return new List<Result>();
}

Expand Down Expand Up @@ -1425,6 +1428,12 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
});
}

else if (query.SearchTerms.Length == ArgumentIndices.GroupingName)
{
this._selectedProjectId = -1;
this._selectedClientId = -1;
}

/*
* Report span selection --- tgl view [day | week | month | year]
*/
Expand Down Expand Up @@ -1534,11 +1543,6 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
{
case (Settings.ViewGroupingKeys.Projects):
{
if (query.SearchTerms.Length == ArgumentIndices.GroupingName)
{
this._selectedProjectId = -1;
}

if (runningTimeEntry is not null)
{
// Perform deep copy of summary so the cache is not mutated
Expand Down Expand Up @@ -1729,35 +1733,93 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
}
}

results.AddRange(
summary.groups.ConvertAll(group =>
{
var client = me.clients?.Find(client => client.id == group.id);
var elapsed = TimeSpan.FromSeconds(group.seconds);
if (this._selectedClientId == -1)
{
results.AddRange(
summary.groups.ConvertAll(group =>
{
var client = me.clients?.Find(client => client.id == group.id);
var elapsed = TimeSpan.FromSeconds(group.seconds);
var highestProjectId = group.sub_groups?.MaxBy(subGroup => subGroup.seconds)?.id;
var highestProject = me.projects?.Find(project => project.id == highestProjectId);
return new Result
{
Title = client?.name ?? "No Client",
SubTitle = $"{elapsed.Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Hour)} ({(int)elapsed.TotalHours}:{elapsed.ToString(@"mm\:ss")})",
IcoPath = (highestProject?.color is not null)
? new ColourIcon(this._context, highestProject.color, "view.png").GetColourIcon()
: "view.png",
AutoCompleteText = $"{query.ActionKeyword} {Settings.ViewCommand} {spanConfiguration.Argument} {groupingConfiguration.Argument} ",
Score = (int)group.seconds,
Action = c =>
{
this._selectedClientId = client?.id;
this._context.API.ChangeQuery($"{query.ActionKeyword} {Settings.ViewCommand} {spanConfiguration.Argument} {groupingConfiguration.Argument} {client?.name?.Kebaberize() ?? "No Client"} ", true);
return false;
}
};
})
);
break;
}

var highestProjectId = group.sub_groups?.MaxBy(subGroup => subGroup.seconds)?.id;
var highestProject = me.projects?.Find(project => project.id == highestProjectId);
var selectedClientGroup = summary.groups.Find(group => group.id == this._selectedClientId);

if (selectedClientGroup?.sub_groups is null)
{
break;
}

var client = me.clients?.Find(client => client.id == selectedClientGroup.id);

return new Result
var subResults = selectedClientGroup.sub_groups.ConvertAll(subGroup =>
{
var project = me.projects?.Find(project => project.id == subGroup.id);
var elapsed = TimeSpan.FromSeconds(subGroup.seconds);
return new Result
{
Title = project?.name ?? "No Project",
SubTitle = $"{((client?.id is not null) ? $"{client?.name} | " : string.Empty)}{elapsed.Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Hour)} ({(int)elapsed.TotalHours}:{elapsed.ToString(@"mm\:ss")})",
IcoPath = (project?.color is not null)
? new ColourIcon(this._context, project.color, "view.png").GetColourIcon()
: "view.png",
AutoCompleteText = $"{query.ActionKeyword} {Settings.ViewCommand} {spanConfiguration.Argument} {groupingConfiguration.Argument} ",
Score = (int)subGroup.seconds,
Action = c =>
{
Title = client?.name ?? "No Client",
SubTitle = $"{elapsed.Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Hour)} ({(int)elapsed.TotalHours}:{elapsed.ToString(@"mm\:ss")})",
IcoPath = (highestProject?.color is not null)
? new ColourIcon(this._context, highestProject.color, "view.png").GetColourIcon()
: "view.png",
AutoCompleteText = $"{query.ActionKeyword} {Settings.ViewCommand} {spanConfiguration.Argument} {groupingConfiguration.Argument} ",
Score = (int)group.seconds,
Action = c =>
this._selectedClientId = -1;
this._selectedProjectId = project?.id;
if (string.IsNullOrEmpty(groupingConfiguration.SubArgument))
{
// this._selectedProjectId = project?.id;
this._context.API.ChangeQuery($"{query.ActionKeyword} {Settings.ViewCommand} {spanConfiguration.Argument} {groupingConfiguration.Argument} {client?.name?.Kebaberize() ?? "No Client"} ", true);
return false;
throw new Exception("Invalid ViewGroupingCommandArgument configuration: Missing 'SubArgument' field.");
}
};
})
);
break;
this._context.API.ChangeQuery($"{query.ActionKeyword} {Settings.ViewCommand} {spanConfiguration.Argument} {groupingConfiguration.SubArgument} {project?.name?.Kebaberize() ?? "No Project"} ", true);
return false;
}
};
});

var subTotal = TimeSpan.FromSeconds(selectedClientGroup.seconds);
subResults.Add(new Result
{
Title = $"{subTotal.Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Hour)} tracked {spanConfiguration.Interpolation} ({(int)subTotal.TotalHours}:{subTotal.ToString(@"mm\:ss")})",
IcoPath = "view.png",
AutoCompleteText = $"{query.ActionKeyword} {query.Search} ",
Score = (int)subTotal.TotalSeconds,
});

string subNameQuery = Main.ExtractFromQuery(query, ArgumentIndices.SubGroupingName);
return (string.IsNullOrWhiteSpace(subNameQuery))
? subResults
: subResults.FindAll(result =>
{
return this._context.API.FuzzySearch(subNameQuery, result.Title).Score > 0;
});
}
case (Settings.ViewGroupingKeys.Entries):
{
Expand Down

0 comments on commit 8bd7bd9

Please sign in to comment.