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

Commit

Permalink
insert default workspace items before all other items
Browse files Browse the repository at this point in the history
  • Loading branch information
skel35 committed Jun 14, 2021
1 parent 5ba7f47 commit e05bc85
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 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
54 changes: 43 additions & 11 deletions src/related_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,13 @@ void RelatedData::projectAutocompleteItems(
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 {

poco_check_ptr(list);

auto default_workspace_items_inserted = 0;

for (std::vector<Project *>::const_iterator it =
Projects.begin();
it != Projects.end(); ++it) {
Expand Down Expand Up @@ -460,31 +463,47 @@ void RelatedData::projectAutocompleteItems(
}
}
} else {
list->push_back(autocomplete_item);
auto is_default_workspace = p->WID() == defaultWID;
if (is_default_workspace) {
list->insert(list->begin() + default_workspace_items_inserted, autocomplete_item);
++default_workspace_items_inserted;
}
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);
if (is_default_workspace) {
list->insert(list->begin() + default_workspace_items_inserted, ac);
++default_workspace_items_inserted;
}
else {
list->push_back(ac);
}
}
}
}
}
}

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 +512,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 +532,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 +551,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
13 changes: 8 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 Down Expand Up @@ -131,7 +131,8 @@ class TOGGL_INTERNAL_EXPORT RelatedData {
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 +141,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 e05bc85

Please sign in to comment.