Skip to content

Commit

Permalink
Time-slots settings made work across 12 AM boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
khrykin committed May 1, 2020
1 parent 7b4d88e commit 4d4dbdb
Show file tree
Hide file tree
Showing 55 changed files with 2,352 additions and 1,637 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ if (APPLE)
models/apple/SGCalendarManager.h
models/apple/SGCalendarExporter.h
models/apple/SGCalendarExporter.mm
)
models/color.mm)
endif ()

set(MODELS ${MODELS_PLATFORM_FILES}
Expand Down Expand Up @@ -96,7 +96,9 @@ set(MODELS ${MODELS_PLATFORM_FILES}
models/event.cpp
models/event.h
models/color.cpp
models/color.h)
models/color.h
models/theme.cpp
models/theme.h)

set(MODELS_TESTS
models/tests/activity_tests.cpp
Expand Down
2 changes: 0 additions & 2 deletions deployment/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,5 @@
<string>Strategr would be able to export activity sessions as events to the Calendar</string>
<key>SUFeedURL</key>
<string>${APPCAST_URL}</string>
<key>SUPublicEDKey</key>
<string>G0A4GIM5nGFE2ANNKzogtjCjWHwItD6oy56yL1qJngQ=</string>
</dict>
</plist>
160 changes: 81 additions & 79 deletions models/activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,90 +4,92 @@
#include "activity.h"
#include "activityinvalidpropertyexception.h"

const std::vector<stg::activity::color_info_t> &stg::activity::default_colors() {
const static std::vector<stg::activity::color_info_t> colors = {
{"#FF6562", "Red"},
{"#FFB700", "Orange"},
{"#FFD600", "Yellow"},
{"#A463F2", "Purple"},
{"#D5008F", "Indigo"},
{"#19A974", "Green"},
{"#357EDD", "Blue"},
{"#000000", "Black"},
{"#777777", "Gray"}
namespace stg {
auto activity::default_colors() -> const std::vector<stg::activity::color_info_t> & {
const static std::vector<activity::color_info_t> colors = {
{"#FF6562", "Red"},
{"#FFB700", "Orange"},
{"#FFD600", "Yellow"},
{"#A463F2", "Purple"},
{"#D5008F", "Indigo"},
{"#19A974", "Green"},
{"#357EDD", "Blue"},
{"#000000", "Black"},
{"#777777", "Gray"}
};

return colors;
};

return colors;
};
activity::activity(name_t name, color_t color) : _color(std::move(color)) {
if (!is_valid(name)) {
throw empty_name_exception();
}

stg::activity::activity(name_t name, color_t color) : _color(std::move(color)) {
if (!is_valid(name)) {
throw empty_name_exception();
_name = std::move(name);
}

_name = std::move(name);
}

bool stg::activity::is_valid(const activity::name_t &name) {
bool white_spaces_only = name.find_first_not_of(" \t\n\v\f\r") == std::string::npos;
return !white_spaces_only;
}

stg::activity::invalid_property_exception stg::activity::empty_name_exception() {
const auto message = "activity name can't be empty";
return invalid_property_exception(message);
}

const stg::activity::name_t &stg::activity::name() const {
return _name;
}

const stg::activity::color_t &stg::activity::color() const {
return _color;
}

bool stg::operator==(const stg::activity &lhs, const stg::activity &rhs) {
return lhs.name() == rhs.name() &&
lhs.color() == rhs.color();
}

bool stg::operator!=(const stg::activity &lhs, const stg::activity &rhs) {
return !(lhs == rhs);
}

std::ostream &stg::operator<<(std::ostream &os,
const stg::activity &activity) {
os << "activity("
<< activity.name()
<< ", "
<< activity.color()
<< ")";

return os;
}

stg::activity stg::activity::copy_changing_name(const name_t &name) const {
return activity(name, _color);
}

stg::activity stg::activity::copy_changing_color(const color_t &color) const {
return activity(_name, color);
}

nlohmann::json stg::activity::to_json() {
nlohmann::json j;
j[keys::name] = this->name();
j[keys::color] = this->color();
return j;
}

stg::activity stg::activity::from_json(const nlohmann::json &j) {
auto name = j[keys::name];

stg::color color = activity::default_color;
if (j.count(keys::color) && !j[keys::color].is_null()) {
color = j[keys::color];
auto activity::is_valid(const activity::name_t &name) -> bool {
bool white_spaces_only = name.find_first_not_of(" \t\n\v\f\r") == std::string::npos;
return !white_spaces_only;
}

return activity{name, color};
auto activity::empty_name_exception() -> activity::invalid_property_exception {
const auto *message = "activity name can't be empty";
return invalid_property_exception(message);
}

auto activity::name() const -> const activity::name_t & {
return _name;
}

auto activity::color() const -> const activity::color_t & {
return _color;
}

auto operator==(const stg::activity &lhs, const stg::activity &rhs) -> bool {
return lhs.name() == rhs.name() &&
lhs.color() == rhs.color();
}

auto operator!=(const stg::activity &lhs, const stg::activity &rhs) -> bool {
return !(lhs == rhs);
}

auto operator<<(std::ostream &os,
const stg::activity &activity) -> std::ostream & {
os << "activity("
<< activity.name()
<< ", "
<< activity.color()
<< ")";

return os;
}

auto activity::with_name(const name_t &name) const -> activity {
return activity(name, _color);
}

auto activity::with_color(const color_t &color) const -> activity {
return activity(_name, color);
}

auto activity::to_json() const -> nlohmann::json {
nlohmann::json j;
j[keys::name] = this->name();
j[keys::color] = this->color();
return j;
}

auto activity::from_json(const nlohmann::json &j) -> activity {
auto name = j[keys::name];

stg::color color = activity::default_color;
if (j.count(keys::color) && !j[keys::color].is_null()) {
color = j[keys::color];
}

return activity{name, color};
}
}
51 changes: 38 additions & 13 deletions models/activity.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,49 @@ namespace stg {
class invalid_property_exception;

static constexpr auto default_color = "#000000";
static const std::vector<color_info_t> &default_colors();
static auto default_colors() -> const std::vector<color_info_t> &;

explicit activity(name_t name, color_t color = default_color) noexcept(false);

const name_t &name() const;
const color_t &color() const;
auto name() const -> const name_t &;
auto color() const -> const color_t &;

activity copy_changing_name(const name_t &name) const;
activity copy_changing_color(const color_t &color) const;
auto light_color() const -> color_t {
auto clr = color();
clr.set_alpha_component(0.15);

friend bool operator==(const activity &lhs, const activity &rhs);
friend bool operator!=(const activity &lhs, const activity &rhs);
return clr;
}

friend std::ostream &operator<<(std::ostream &os,
const activity &activity);
auto desaturated_light_color() const -> color_t {
auto clr = color();
clr.set_hsl(clr.hue(), 0.3, 0.75);
clr.set_alpha_component(0.2);

nlohmann::json to_json();
static activity from_json(const nlohmann::json &j);
return clr;
}

auto desaturated_dark_color() const -> color_t {
auto clr = color_t();

if (color().lightness() < 0.2)
clr = color_t(0xffffffff);

clr.set_alpha_component(0.1);
return clr;
}

auto with_name(const name_t &name) const -> activity;
auto with_color(const color_t &color) const -> activity;

friend auto operator==(const activity &lhs, const activity &rhs) -> bool;
friend auto operator!=(const activity &lhs, const activity &rhs) -> bool;

friend auto operator<<(std::ostream &os,
const activity &activity) -> std::ostream &;

auto to_json() const -> nlohmann::json;
static auto from_json(const nlohmann::json &j) -> activity;

private:
name_t _name;
Expand All @@ -45,9 +70,9 @@ namespace stg {
static constexpr auto color = "color";
};

static invalid_property_exception empty_name_exception();
static auto empty_name_exception() -> invalid_property_exception;

static bool is_valid(const name_t &name);
static auto is_valid(const name_t &name) -> bool;
};
}

Expand Down
14 changes: 8 additions & 6 deletions models/activityinvalidpropertyexception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
#include "activityinvalidpropertyexception.h"
#include <utility>

const char *stg::activity::invalid_property_exception::what() const noexcept {
return message.c_str();
}
namespace stg {
activity::invalid_property_exception::invalid_property_exception(std::string message) :
std::exception(),
message(std::move(message)) {}

stg::activity::invalid_property_exception::invalid_property_exception(std::string message) :
std::exception(),
message(std::move(message)) {}
auto activity::invalid_property_exception::what() const noexcept -> const char * {
return message.c_str();
}
}
2 changes: 1 addition & 1 deletion models/activityinvalidpropertyexception.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace stg {
class activity::invalid_property_exception : public std::exception {
public:
explicit invalid_property_exception(std::string message);
const char *what() const noexcept override;
auto what() const noexcept -> const char * override;

private:
std::string message;
Expand Down
Loading

0 comments on commit 4d4dbdb

Please sign in to comment.