Skip to content

Commit

Permalink
feat(ux): 💄 remove start/stop now options when time span is spe…
Browse files Browse the repository at this point in the history
…cified (#68)
  • Loading branch information
JamesNZL committed Jun 23, 2023
1 parent 3e4e2af commit 6a742d7
Showing 1 changed file with 84 additions and 82 deletions.
166 changes: 84 additions & 82 deletions src/TogglTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -679,13 +679,32 @@ internal async ValueTask<List<Result>> RequestStartEntry(CancellationToken token
string projectName = project?.WithClientName ?? "No Project";
string description = Main.ReplaceSearchBackslashes(Main.ExtractFromQuery(query, ArgumentIndices.Description));

var results = new List<Result>
var results = new List<Result>();

if (this._settings.ShowUsageTips && string.IsNullOrEmpty(description))
{
new Result
results.Add(new Result
{
Title = "Usage Tip",
SubTitle = $"Keep typing to specify the time entry description",
IcoPath = "tip.png",
AutoCompleteText = $"{query.ActionKeyword} {query.Search} ",
Score = 1000,
Action = c =>
{
this._context.API.ChangeQuery($"{query.ActionKeyword} {query.Search} ");
return false;
}
});
}

if (!query.SearchTerms.Contains(Settings.TimeSpanFlag))
{
results.Add(new Result
{
Title = $"Start {description}{((string.IsNullOrEmpty(description) ? string.Empty : " "))}now",
SubTitle = projectName,
IcoPath = this._colourIconProvider.GetColourIcon(project?.Colour, "start.png") ,
IcoPath = this._colourIconProvider.GetColourIcon(project?.Colour, "start.png"),
AutoCompleteText = $"{query.ActionKeyword} {query.Search}",
Score = 10000,
Action = c =>
Expand Down Expand Up @@ -740,28 +759,8 @@ internal async ValueTask<List<Result>> RequestStartEntry(CancellationToken token
return true;
},
},
};

if (this._settings.ShowUsageTips && string.IsNullOrEmpty(description))
{
results.Add(new Result
{
Title = "Usage Tip",
SubTitle = $"Keep typing to specify the time entry description",
IcoPath = "tip.png",
AutoCompleteText = $"{query.ActionKeyword} {query.Search} ",
Score = 1000,
Action = c =>
{
this._context.API.ChangeQuery($"{query.ActionKeyword} {query.Search} ");
return false;
}
});
}

if (!query.SearchTerms.Contains(Settings.TimeSpanFlag))
{
if (this._settings.ShowUsageTips)
{
results.Add(new Result
Expand Down Expand Up @@ -865,6 +864,8 @@ internal async ValueTask<List<Result>> RequestStartEntry(CancellationToken token
return true;
},
});

return results;
}
catch
{
Expand All @@ -886,6 +887,8 @@ internal async ValueTask<List<Result>> RequestStartEntry(CancellationToken token
}
});
}

return results;
}
}

Expand Down Expand Up @@ -991,13 +994,15 @@ internal async ValueTask<List<Result>> RequestStopEntry(CancellationToken token,

string projectName = runningTimeEntry.Project?.WithClientName ?? "No Project";

var results = new List<Result>
var results = new List<Result>();

if (!query.SearchTerms.Contains(Settings.TimeSpanEndFlag))
{
new Result
results.Add(new Result
{
Title = $"Stop {runningTimeEntry.GetDescription()} now",
SubTitle = $"{projectName} | {runningTimeEntry.HumanisedElapsed} ({runningTimeEntry.DetailedElapsed})",
IcoPath = this._colourIconProvider.GetColourIcon(runningTimeEntry.Project?.Colour, "stop.png") ,
IcoPath = this._colourIconProvider.GetColourIcon(runningTimeEntry.Project?.Colour, "stop.png"),
AutoCompleteText = $"{query.ActionKeyword} {Settings.StopCommand} {runningTimeEntry.GetDescription(escapePotentialFlags: true)} ",
Score = 10000,
Action = c =>
Expand Down Expand Up @@ -1032,11 +1037,8 @@ internal async ValueTask<List<Result>> RequestStopEntry(CancellationToken token,
return true;
},
},
};
});

if (!query.SearchTerms.Contains(Settings.TimeSpanEndFlag))
{
if (!this._settings.ShowUsageTips)
{
return results;
Expand Down Expand Up @@ -1419,58 +1421,6 @@ internal async ValueTask<List<Result>> RequestEditEntry(CancellationToken token,
: ArgumentIndices.DescriptionWithoutProject
));

results.Add(new Result
{
Title = (string.IsNullOrEmpty(description)) ? timeEntry.GetDescription() : description,
SubTitle = $"{projectName} | {timeEntry.HumanisedElapsed} ({timeEntry.DetailedElapsed})",
IcoPath = this._colourIconProvider.GetColourIcon(project?.Colour, "edit.png"),
AutoCompleteText = $"{query.ActionKeyword} {(string.IsNullOrEmpty(description) ? ($"{query.Search} {timeEntry.GetDescription(escapePotentialFlags: true)}") : query.Search)} ",
Score = 10000,
Action = c =>
{
Task.Run(async delegate
{
try
{
this._context.API.LogInfo("TogglTrack", $"{this._state.SelectedIds.Project}, {timeEntry.Id}, {timeEntry.Duration}, {timeEntry.Start}, {this._state.SelectedIds.Project}, {timeEntry.WorkspaceId}, {description}");
var editedTimeEntry = (await this._client.EditTimeEntry(
workspaceId: timeEntry.WorkspaceId,
projectId: this._state.SelectedIds.Project,
id: timeEntry.Id,
description: description,
duration: timeEntry.Duration,
tags: timeEntry.Tags,
billable: timeEntry.Billable
))?.ToTimeEntry(me);
if (editedTimeEntry?.Id is null)
{
throw new Exception("An API error was encountered.");
}
this.ShowSuccessMessage($"Edited {editedTimeEntry.GetRawDescription()}", $"{projectName} | {timeEntry.DetailedElapsed}", "edit.png");
// Update cached running time entry state
this.RefreshCache();
}
catch (Exception exception)
{
this._context.API.LogException("TogglTrack", "Failed to edit time entry", exception);
this.ShowErrorMessage("Failed to edit time entry.", exception.Message);
}
finally
{
this._state.SelectedIds.TimeEntry = -1;
this._state.SelectedIds.Project = -1;
this._state.EditProject = TogglTrack.EditProjectState.NoProjectChange;
}
});
return true;
},
});

if (this._settings.ShowUsageWarnings && string.IsNullOrEmpty(description) && !string.IsNullOrEmpty(timeEntry.GetRawDescription()))
{
results.Add(new Result
Expand All @@ -1494,6 +1444,58 @@ internal async ValueTask<List<Result>> RequestEditEntry(CancellationToken token,

if (!hasTimeSpanFlag && !hasTimeSpanEndFlag)
{
results.Add(new Result
{
Title = (string.IsNullOrEmpty(description)) ? timeEntry.GetDescription() : description,
SubTitle = $"{projectName} | {timeEntry.HumanisedElapsed} ({timeEntry.DetailedElapsed})",
IcoPath = this._colourIconProvider.GetColourIcon(project?.Colour, "edit.png"),
AutoCompleteText = $"{query.ActionKeyword} {(string.IsNullOrEmpty(description) ? ($"{query.Search} {timeEntry.GetDescription(escapePotentialFlags: true)}") : query.Search)} ",
Score = 10000,
Action = c =>
{
Task.Run(async delegate
{
try
{
this._context.API.LogInfo("TogglTrack", $"{this._state.SelectedIds.Project}, {timeEntry.Id}, {timeEntry.Duration}, {timeEntry.Start}, {this._state.SelectedIds.Project}, {timeEntry.WorkspaceId}, {description}");
var editedTimeEntry = (await this._client.EditTimeEntry(
workspaceId: timeEntry.WorkspaceId,
projectId: this._state.SelectedIds.Project,
id: timeEntry.Id,
description: description,
duration: timeEntry.Duration,
tags: timeEntry.Tags,
billable: timeEntry.Billable
))?.ToTimeEntry(me);
if (editedTimeEntry?.Id is null)
{
throw new Exception("An API error was encountered.");
}
this.ShowSuccessMessage($"Edited {editedTimeEntry.GetRawDescription()}", $"{projectName} | {timeEntry.DetailedElapsed}", "edit.png");
// Update cached running time entry state
this.RefreshCache();
}
catch (Exception exception)
{
this._context.API.LogException("TogglTrack", "Failed to edit time entry", exception);
this.ShowErrorMessage("Failed to edit time entry.", exception.Message);
}
finally
{
this._state.SelectedIds.TimeEntry = -1;
this._state.SelectedIds.Project = -1;
this._state.EditProject = TogglTrack.EditProjectState.NoProjectChange;
}
});
return true;
},
});

if (this._settings.ShowUsageTips)
{
if (!hasTimeSpanFlag)
Expand Down

0 comments on commit 6a742d7

Please sign in to comment.