Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4855 from toggl-open-source/improve/show-default-…
Browse files Browse the repository at this point in the history
…workspace-as-the-first

Show default workspace as the first one in the description and project autocomplete list
  • Loading branch information
skel35 authored Jun 15, 2021
2 parents 5ba7f47 + 567d58c commit 35833aa
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 81 deletions.
12 changes: 6 additions & 6 deletions src/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ void Context::updateUI(const UIElements &what) {
if (what.display_time_entry_autocomplete) {
if (what.first_load) {
if (user_) {
user_->related.TimeEntryAutocompleteItems(&time_entry_autocompletes);
user_->related.TimeEntryAutocompleteItems(&time_entry_autocompletes, user_->DefaultWID());
}
UI()->DisplayTimeEntryAutocomplete(&time_entry_autocompletes);
} else {
Expand All @@ -945,7 +945,7 @@ void Context::updateUI(const UIElements &what) {
if (what.display_mini_timer_autocomplete) {
if (what.first_load) {
if (user_) {
user_->related.MinitimerAutocompleteItems(&minitimer_autocompletes);
user_->related.MinitimerAutocompleteItems(&minitimer_autocompletes, user_->DefaultWID());
}
UI()->DisplayMinitimerAutocomplete(&minitimer_autocompletes);
} else {
Expand Down Expand Up @@ -1000,7 +1000,7 @@ void Context::updateUI(const UIElements &what) {
if (what.display_project_autocomplete) {
if (what.first_load) {
if (user_) {
user_->related.ProjectAutocompleteItems(&project_autocompletes);
user_->related.ProjectAutocompleteItems(&project_autocompletes, user_->DefaultWID());
}
UI()->DisplayProjectAutocomplete(&project_autocompletes);
} else {
Expand Down Expand Up @@ -1119,23 +1119,23 @@ void Context::Sync() {
void Context::onTimeEntryAutocompletes(Poco::Util::TimerTask&) { // NOLINT
std::vector<view::Autocomplete> time_entry_autocompletes;
if (user_) {
user_->related.TimeEntryAutocompleteItems(&time_entry_autocompletes);
user_->related.TimeEntryAutocompleteItems(&time_entry_autocompletes, user_->DefaultWID());
}
UI()->DisplayTimeEntryAutocomplete(&time_entry_autocompletes);
}

void Context::onMiniTimerAutocompletes(Poco::Util::TimerTask&) { // NOLINT
std::vector<view::Autocomplete> minitimer_autocompletes;
if (user_) {
user_->related.MinitimerAutocompleteItems(&minitimer_autocompletes);
user_->related.MinitimerAutocompleteItems(&minitimer_autocompletes, user_->DefaultWID());
}
UI()->DisplayMinitimerAutocomplete(&minitimer_autocompletes);
}

void Context::onProjectAutocompletes(Poco::Util::TimerTask&) { // NOLINT
std::vector<view::Autocomplete> project_autocompletes;
if (user_) {
user_->related.ProjectAutocompleteItems(&project_autocompletes);
user_->related.ProjectAutocompleteItems(&project_autocompletes, user_->DefaultWID());
}
UI()->DisplayProjectAutocomplete(&project_autocompletes);
}
Expand Down
173 changes: 103 additions & 70 deletions src/related_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -395,96 +395,116 @@ void RelatedData::taskAutocompleteItems(
}
}

// Add projects, in format:
// Project. Client
void RelatedData::projectAutocompleteItems(
std::set<std::string> *unique_names,
std::map<Poco::UInt64, std::string> *ws_names,
std::vector<view::Autocomplete> *list,
std::map<std::string, std::vector<view::Autocomplete> > *items,
std::map<Poco::UInt64, std::vector<view::Autocomplete> > *task_items) const {
void RelatedData::projectAutocompleteItem(
std::set<std::string>* unique_names,
std::map<Poco::UInt64, std::string>* ws_names,
std::vector<view::Autocomplete>* list,
std::map<std::string, std::vector<view::Autocomplete> >* items,
std::map<Poco::UInt64, std::vector<view::Autocomplete> >* task_items,
Project *p) const
{
if (!p->Active()) {
return;
}

poco_check_ptr(list);
Client* c = clientByProject(p);

for (std::vector<Project *>::const_iterator it =
Projects.begin();
it != Projects.end(); ++it) {
Project *p = *it;
std::string text = Formatter::JoinTaskName(nullptr, p);
if (text.empty()) {
return;
}

if (!p->Active()) {
continue;
{
std::stringstream ss;
ss << p->WID() << "/" << text;
std::string uniq_name = ss.str();
if (unique_names->find(uniq_name) != unique_names->end()) {
return;
}
unique_names->insert(uniq_name);
}

Client *c = clientByProject(p);

std::string text = Formatter::JoinTaskName(nullptr, p);
if (text.empty()) {
continue;
view::Autocomplete autocomplete_item;
autocomplete_item.Text = text;
autocomplete_item.ProjectAndTaskLabel = text;
autocomplete_item.ProjectLabel = p->Name();
if (c) {
autocomplete_item.ClientLabel = c->Name();
autocomplete_item.ClientID = c->ID();
}
autocomplete_item.ProjectID = p->ID();
autocomplete_item.ProjectGUID = p->GUID();
autocomplete_item.ProjectColor = p->ColorCode();
if (ws_names) {
autocomplete_item.WorkspaceName = (*ws_names)[p->WID()];
}
autocomplete_item.WorkspaceID = p->WID();
autocomplete_item.Type = kAutocompleteItemProject;

if (items && !autocomplete_item.WorkspaceName.empty()) {
(*items)[autocomplete_item.WorkspaceName].push_back(autocomplete_item);
if (task_items) {
for (std::vector<view::Autocomplete>::const_iterator it =
(*task_items)[autocomplete_item.ProjectID].begin();
it != (*task_items)[autocomplete_item.ProjectID].end(); ++it) {
view::Autocomplete ac = *it;
(*items)[autocomplete_item.WorkspaceName].push_back(ac);
}
}
}
else {
list->push_back(autocomplete_item);

{
std::stringstream ss;
ss << p->WID() << "/" << text;
std::string uniq_name = ss.str();
if (unique_names->find(uniq_name) != unique_names->end()) {
continue;
if (task_items) {
for (std::vector<view::Autocomplete>::const_iterator it =
(*task_items)[autocomplete_item.ProjectID].begin();
it != (*task_items)[autocomplete_item.ProjectID].end(); ++it) {
view::Autocomplete ac = *it;
list->push_back(ac);
}
unique_names->insert(uniq_name);
}
}
}

view::Autocomplete autocomplete_item;
autocomplete_item.Text = text;
autocomplete_item.ProjectAndTaskLabel = text;
autocomplete_item.ProjectLabel = p->Name();
if (c) {
autocomplete_item.ClientLabel = c->Name();
autocomplete_item.ClientID = c->ID();
}
autocomplete_item.ProjectID = p->ID();
autocomplete_item.ProjectGUID = p->GUID();
autocomplete_item.ProjectColor = p->ColorCode();
if (ws_names) {
autocomplete_item.WorkspaceName = (*ws_names)[p->WID()];
// Add projects, in format:
// Project. Client
void RelatedData::projectAutocompleteItems(
std::set<std::string> *unique_names,
std::map<Poco::UInt64, std::string> *ws_names,
std::vector<view::Autocomplete> *list,
std::map<std::string, std::vector<view::Autocomplete> > *items,
std::map<Poco::UInt64, std::vector<view::Autocomplete> > *task_items,
const Poco::UInt64 defaultWID) const {

poco_check_ptr(list);

for (auto p : Projects) {
if (p->WID() == defaultWID) {
projectAutocompleteItem(unique_names, ws_names, list, items, task_items, p);
}
autocomplete_item.WorkspaceID = p->WID();
autocomplete_item.Type = kAutocompleteItemProject;
}

if (items && !autocomplete_item.WorkspaceName.empty()) {
(*items)[autocomplete_item.WorkspaceName].push_back(autocomplete_item);
if (task_items) {
for (std::vector<view::Autocomplete>::const_iterator it =
(*task_items)[autocomplete_item.ProjectID].begin();
it != (*task_items)[autocomplete_item.ProjectID].end(); ++it) {
view::Autocomplete ac = *it;
(*items)[autocomplete_item.WorkspaceName].push_back(ac);
}
}
} else {
list->push_back(autocomplete_item);
if (task_items) {
for (std::vector<view::Autocomplete>::const_iterator it =
(*task_items)[autocomplete_item.ProjectID].begin();
it != (*task_items)[autocomplete_item.ProjectID].end(); ++it) {
view::Autocomplete ac = *it;
list->push_back(ac);
}
}
for (auto p : Projects) {
if (p->WID() != defaultWID) {
projectAutocompleteItem(unique_names, ws_names, list, items, task_items, p);
}
}
}

void RelatedData::TimeEntryAutocompleteItems(
std::vector<view::Autocomplete> *result) const {
std::vector<view::Autocomplete> *result,
const Poco::UInt64 defaultWID) const {
std::set<std::string> unique_names;
std::map<Poco::UInt64, std::string> ws_names;
std::map<std::string, std::vector<view::Autocomplete> > items;
workspaceAutocompleteItems(&unique_names, &ws_names, result);
timeEntryAutocompleteItems(&unique_names, &ws_names, result, &items);
mergeGroupedAutocompleteItems(result, &items);
mergeGroupedAutocompleteItems(result, &items, &ws_names, defaultWID);
}

void RelatedData::MinitimerAutocompleteItems(
std::vector<view::Autocomplete> *result) const {
std::vector<view::Autocomplete> *result,
const Poco::UInt64 defaultWID) const {
std::set<std::string> unique_names;
std::map<Poco::UInt64, std::string> ws_names;
std::map<std::string, std::vector<view::Autocomplete> > items;
Expand All @@ -493,14 +513,16 @@ void RelatedData::MinitimerAutocompleteItems(
workspaceAutocompleteItems(&unique_names, &ws_names, result);
timeEntryAutocompleteItems(&unique_names, &ws_names, result, &items);
taskAutocompleteItems(&unique_names, &ws_names, result, &task_items);
projectAutocompleteItems(&unique_names, &ws_names, result, &items, &task_items);
projectAutocompleteItems(&unique_names, &ws_names, result, &items, &task_items, defaultWID);

mergeGroupedAutocompleteItems(result, &items);
mergeGroupedAutocompleteItems(result, &items, &ws_names, defaultWID);
}

void RelatedData::mergeGroupedAutocompleteItems(
std::vector<view::Autocomplete> *result,
std::map<std::string, std::vector<view::Autocomplete> > *items) const {
std::map<std::string, std::vector<view::Autocomplete> > *items,
std::map<Poco::UInt64, std::string> *ws_names,
const Poco::UInt64 defaultWID) const {
// Join created workspace maps to a single vector
Poco::UInt64 total_size = 0;
for(std::map<std::string, std::vector<view::Autocomplete> >::iterator iter =
Expand All @@ -511,6 +533,16 @@ void RelatedData::mergeGroupedAutocompleteItems(

result->reserve(total_size);

if (defaultWID) {
// add items from the default workspace to the top of the list
auto defaultWorkspaceName = (*ws_names)[defaultWID];
auto defaultWorkspaceItems = items->find(defaultWorkspaceName);
if (defaultWorkspaceItems != items->end()) {
result->insert(result->end(), defaultWorkspaceItems->second.begin(), defaultWorkspaceItems->second.end());
items->erase(defaultWorkspaceItems);
}
}

for(std::map<std::string, std::vector<view::Autocomplete> >::iterator iter =
items->begin(); iter != items->end(); ++iter)
{
Expand All @@ -520,13 +552,14 @@ void RelatedData::mergeGroupedAutocompleteItems(


void RelatedData::ProjectAutocompleteItems(
std::vector<view::Autocomplete> *result) const {
std::vector<view::Autocomplete> *result,
const Poco::UInt64 defaultWID) const {
std::set<std::string> unique_names;
std::map<Poco::UInt64, std::string> ws_names;
std::map<Poco::UInt64, std::vector<view::Autocomplete> > task_items;
workspaceAutocompleteItems(&unique_names, &ws_names, result);
taskAutocompleteItems(&unique_names, &ws_names, result, &task_items);
projectAutocompleteItems(&unique_names, &ws_names, result, nullptr, &task_items);
projectAutocompleteItems(&unique_names, &ws_names, result, nullptr, &task_items, defaultWID);
}

void RelatedData::workspaceAutocompleteItems(
Expand Down
18 changes: 13 additions & 5 deletions src/related_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ class TOGGL_INTERNAL_EXPORT RelatedData {
std::string end_time,
const Poco::UInt8 days_of_week);

void TimeEntryAutocompleteItems(std::vector<view::Autocomplete> *) const;
void MinitimerAutocompleteItems(std::vector<view::Autocomplete> *) const;
void ProjectAutocompleteItems(std::vector<view::Autocomplete> *) const;
void TimeEntryAutocompleteItems(std::vector<view::Autocomplete> *, const Poco::UInt64 defaultWID) const;
void MinitimerAutocompleteItems(std::vector<view::Autocomplete> *, const Poco::UInt64 defaultWID) const;
void ProjectAutocompleteItems(std::vector<view::Autocomplete> *, const Poco::UInt64 defaultWID) const;

void ProjectLabelAndColorCode(
TimeEntry * const te,
Expand All @@ -125,13 +125,19 @@ class TOGGL_INTERNAL_EXPORT RelatedData {
std::map<Poco::UInt64, std::string> *ws_names,
std::vector<view::Autocomplete> *list,
std::map<Poco::UInt64, std::vector<view::Autocomplete> > *items) const;
void projectAutocompleteItem(std::set<std::string>* unique_names,
std::map<Poco::UInt64, std::string>* ws_names,
std::vector<view::Autocomplete>* list,
std::map<std::string, std::vector<view::Autocomplete>>* items,
std::map<Poco::UInt64, std::vector<view::Autocomplete>>* task_items, Project* p) const;

void projectAutocompleteItems(
std::set<std::string> *unique_names,
std::map<Poco::UInt64, std::string> *ws_names,
std::vector<view::Autocomplete> *list,
std::map<std::string, std::vector<view::Autocomplete> > *items,
std::map<Poco::UInt64, std::vector<view::Autocomplete> > *task_items) const;
std::map<Poco::UInt64, std::vector<view::Autocomplete> > *task_items,
const Poco::UInt64 defaultWID) const;

void workspaceAutocompleteItems(
std::set<std::string> *unique_names,
Expand All @@ -140,7 +146,9 @@ class TOGGL_INTERNAL_EXPORT RelatedData {

void mergeGroupedAutocompleteItems(
std::vector<view::Autocomplete> *result,
std::map<std::string, std::vector<view::Autocomplete> > *items) const;
std::map<std::string, std::vector<view::Autocomplete> > *items,
std::map<Poco::UInt64, std::string> *ws_names,
const Poco::UInt64 defaultWID) const;
};

template<> inline TimeEntry *RelatedData::ModelByID<TimeEntry>(Poco::UInt64 id) { return TimeEntryByID(id); }
Expand Down

0 comments on commit 35833aa

Please sign in to comment.