Skip to content

Commit

Permalink
(core): Update core's documentation
Browse files Browse the repository at this point in the history
* Expand Card constructor;
* Assert Card's invariants;
* Method renaming;
  • Loading branch information
smolBlackCat committed Oct 29, 2024
1 parent 220f64d commit 311a0af
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 137 deletions.
66 changes: 33 additions & 33 deletions src/core/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,31 @@ Board BoardBackend::load() {
card_element->GetLineNum())};
}

Card cur_card{cur_card_name,
cur_card_color
? string_to_color(cur_card_color)
: NO_COLOR,
cur_card_complete};
Date date{};

if (cur_card_due_date) {
const std::regex date_r{"\\d\\d\\d\\d-\\d\\d-\\d\\d"};

if (!std::regex_match(cur_card_due_date, date_r)) {
throw board_parse_error{
std::format("Invalid due date at Card {}",
cur_card.get_name())};
throw board_parse_error{std::format(
"Invalid due date at Card {}", cur_card_name)};
}
std::chrono::sys_seconds secs;

std::istringstream{std::string{cur_card_due_date}} >>
std::chrono::parse("%F", secs);
Date date{std::chrono::floor<std::chrono::days>(secs)};
cur_card.set_due_date(date);
} else {
// This is bad.
cur_card.set_due_date(Date{});
date = date;
}

Card cur_card{
cur_card_name,
date,
cur_card_complete,
cur_card_color ? string_to_color(cur_card_color)
: NO_COLOR,
};

auto task_element = card_element->FirstChildElement("task");
while (task_element) {
cur_card.add_task(
Expand Down Expand Up @@ -237,12 +237,12 @@ bool BoardBackend::save_xml(Board& board) {
board_element->SetAttribute("background", board.get_background().c_str());
doc->InsertEndChild(board_element);

for (auto& cardlist : board.get_cardlist_vector()) {
for (auto& cardlist : board.get_cardlists()) {
tinyxml2::XMLElement* list_element = doc->NewElement("list");
list_element->SetAttribute("name", cardlist->get_name().c_str());
cardlist->set_modified(false);

for (auto& card : cardlist->get_card_vector()) {
for (auto& card : cardlist->get_cards()) {
tinyxml2::XMLElement* card_element = doc->NewElement("card");
card_element->SetAttribute("name", card->get_name().c_str());
if (card->is_color_set())
Expand Down Expand Up @@ -314,7 +314,7 @@ BackgroundType Board::set_background(const std::string& other, bool modify) {
const std::string& Board::get_background() const { return background; }

std::shared_ptr<CardList> Board::add_cardlist(const CardList& cardlist) {
for (auto& ccardlist : cardlist_vector) {
for (auto& ccardlist : cardlists) {
if (*ccardlist == cardlist) {
return nullptr;
}
Expand All @@ -323,7 +323,7 @@ std::shared_ptr<CardList> Board::add_cardlist(const CardList& cardlist) {
try {
std::shared_ptr<CardList> new_cardlist =
std::make_shared<CardList>(cardlist);
cardlist_vector.push_back(new_cardlist);
cardlists.push_back(new_cardlist);
modified = true;
return new_cardlist;
} catch (std::bad_alloc& err) {
Expand All @@ -332,9 +332,9 @@ std::shared_ptr<CardList> Board::add_cardlist(const CardList& cardlist) {
}

bool Board::remove_cardlist(const CardList& cardlist) {
for (size_t i = 0; i < cardlist_vector.size(); i++) {
if (cardlist == (*cardlist_vector.at(i))) {
cardlist_vector.erase(cardlist_vector.begin() + i);
for (size_t i = 0; i < cardlists.size(); i++) {
if (cardlist == (*cardlists.at(i))) {
cardlists.erase(cardlists.begin() + i);
modified = true;
return true;
}
Expand All @@ -346,10 +346,10 @@ void Board::reorder_cardlist(const CardList& next, const CardList& sibling) {
ssize_t next_i = -1;
ssize_t sibling_i = -1;

for (ssize_t i = 0; i < cardlist_vector.size(); i++) {
if (*cardlist_vector[i] == next) {
for (ssize_t i = 0; i < cardlists.size(); i++) {
if (*cardlists[i] == next) {
next_i = i;
} else if (*cardlist_vector[i] == sibling) {
} else if (*cardlists[i] == sibling) {
sibling_i = i;
}
}
Expand All @@ -361,34 +361,34 @@ void Board::reorder_cardlist(const CardList& next, const CardList& sibling) {
return;
}

auto next_it = std::next(cardlist_vector.begin(), next_i);
std::shared_ptr<CardList> temp_v = cardlist_vector[next_i];
cardlist_vector.erase(next_it);
auto next_it = std::next(cardlists.begin(), next_i);
std::shared_ptr<CardList> temp_v = cardlists[next_i];
cardlists.erase(next_it);

// Support for right to left drags and drops
if (next_i < sibling_i) {
sibling_i -= 1;
}

if (sibling_i == cardlist_vector.size() - 1) {
cardlist_vector.push_back(temp_v);
if (sibling_i == cardlists.size() - 1) {
cardlists.push_back(temp_v);
} else {
auto sibling_it = std::next(cardlist_vector.begin(), sibling_i + 1);
cardlist_vector.insert(sibling_it, temp_v);
auto sibling_it = std::next(cardlists.begin(), sibling_i + 1);
cardlists.insert(sibling_it, temp_v);
}
modified = true;
}

bool Board::save() { return backend.save(*this); }

const std::vector<std::shared_ptr<CardList>>& Board::get_cardlist_vector() {
return cardlist_vector;
const std::vector<std::shared_ptr<CardList>>& Board::get_cardlists() {
return cardlists;
}

void Board::set_modified(bool modified) {
Item::set_modified(modified);

for (auto& cardlist : cardlist_vector) {
for (auto& cardlist : cardlists) {
cardlist->set_modified(modified);
}
}
Expand All @@ -398,7 +398,7 @@ time_point<system_clock, seconds> Board::get_last_modified() const {
}

bool Board::get_modified() {
for (auto& cardlist : cardlist_vector) {
for (auto& cardlist : cardlists) {
if (cardlist->get_modified()) {
modified = true;
break;
Expand Down
16 changes: 9 additions & 7 deletions src/core/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ class BoardBackend {
};

/**
* @class Board
*
* @brief A class representing the kanban-style board of the application,
* @brief Kanban Board
*/
class Board : public Item {
public:
Expand Down Expand Up @@ -113,7 +111,8 @@ class Board : public Item {
*
* @param cardlist CardList object
*
* @returns a CardList pointer to the newly allocated object.
* @returns a CardList pointer to the newly allocated object. nullptr may be
* returned if the cardlist to be added is already in cardlists
*/
std::shared_ptr<CardList> add_cardlist(const CardList& cardlist);

Expand All @@ -128,7 +127,7 @@ class Board : public Item {
bool remove_cardlist(const CardList& cardlist);

/**
* @brief Reorders the next card after sibling
* @brief Reorders cardlist "next" after cardlist "sibling"
*/
void reorder_cardlist(const CardList& next, const CardList& sibling);

Expand All @@ -137,7 +136,10 @@ class Board : public Item {
*/
bool save();

const std::vector<std::shared_ptr<CardList>>& get_cardlist_vector();
/**
* @brief Access the underlying cardlists collection
*/
const std::vector<std::shared_ptr<CardList>>& get_cardlists();

void set_modified(bool modified) override;

Expand All @@ -164,6 +166,6 @@ class Board : public Item {

protected:
std::string background;
std::vector<std::shared_ptr<CardList>> cardlist_vector;
std::vector<std::shared_ptr<CardList>> cardlists;
time_point<system_clock, seconds> last_modified;
};
56 changes: 26 additions & 30 deletions src/core/card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

#include <numeric>

Card::Card(const std::string& name, const Color& color, bool complete)
: Item{name}, complete{complete} {
set_color(color);
modified = false;
Card::Card(const std::string& name, const Date& date, bool complete,
const Color& color)
: Item{name}, complete{complete}, due_date{date} {
this->color = color;
}

Card::Card(const std::string& name, const Color& color)
: Card{name, Date{}, false, color} {}

void Card::set_color(const Color& color) {
this->color = color;
modified = true;
Expand Down Expand Up @@ -38,20 +41,20 @@ void Card::set_notes(const std::string& notes) {
}

double Card::get_completion() const {
if (tasks.empty()) {
return 0;
}

auto tasks_completed_n =
double tasks_completed_n =
std::accumulate(tasks.begin(), tasks.end(), 0,
[](int acc, std::shared_ptr<Task> value) {
[](double acc, std::shared_ptr<Task> value) {
return value->get_done() ? acc + 1 : acc;
});

return (tasks_completed_n * 100) / tasks.size();
}

std::shared_ptr<Task> Card::add_task(const Task& task) {
for (auto& ctask : tasks) {
if (task == *ctask) return nullptr;
}

std::shared_ptr<Task> new_task = std::make_shared<Task>(task);
tasks.push_back(new_task);

Expand All @@ -60,9 +63,9 @@ std::shared_ptr<Task> Card::add_task(const Task& task) {
return new_task;
}

bool Card::remove_task(std::shared_ptr<Task> task) {
bool Card::remove_task(const Task& task) {
for (size_t i = 0; i < tasks.size(); i++) {
if (tasks[i] == task) {
if (*tasks[i] == task) {
tasks.erase(tasks.begin() + i);
modified = true;
return true;
Expand Down Expand Up @@ -114,31 +117,24 @@ void Card::reorder_task(const Task& next, const Task& sibling) {
bool Card::past_due_date() {
using namespace std::chrono;

Date current_date =
year_month_day{std::chrono::floor<days>(system_clock::now())};
return current_date > due_date && (due_date.ok());
Date today = year_month_day{std::chrono::floor<days>(system_clock::now())};
return (due_date.ok()) && today > due_date;
};

void Card::set_due_date(const Date& date) {
due_date = date;
modified = true;
if (date.ok()) {
due_date = date;
modified = true;
}
};

Date Card::get_due_date() const { return due_date; };

bool Card::get_complete() const {
if (due_date.ok()) {
return complete;
}

return true;
}
bool Card::get_complete() const { return due_date.ok() ? complete : true; }

void Card::set_complete(bool complete) {
if (due_date.ok())
if (due_date.ok()) {
this->complete = complete;
else
this->complete = true;

modified = true;
}
modified = true;
}
}
Loading

0 comments on commit 311a0af

Please sign in to comment.