From 9d84d508969c2dc371bbaf4772b0920fa6edfab0 Mon Sep 17 00:00:00 2001 From: Sergey Magidovich Date: Tue, 8 Dec 2015 13:15:52 +0300 Subject: [PATCH] Add ReplaceExcludeTime method to TimeTable. --- editor/editor_tests/opening_hours_ui_test.cpp | 12 ++++++++++++ editor/opening_hours_ui.cpp | 16 ++++++++++++---- editor/opening_hours_ui.hpp | 3 ++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/editor/editor_tests/opening_hours_ui_test.cpp b/editor/editor_tests/opening_hours_ui_test.cpp index ec08b79f45..ede7c5b8f6 100644 --- a/editor/editor_tests/opening_hours_ui_test.cpp +++ b/editor/editor_tests/opening_hours_ui_test.cpp @@ -102,6 +102,18 @@ UNIT_TEST(TestTimeTable_ExcludeTime) tt.SetOpeningTime({8_h + 15_min, 15_h + 30_min}); TEST(!tt.AddExcludeTime({7_h, 14_h}), ()); } + { + auto tt = TimeTable::GetPredefinedTimeTable(); + tt.SetTwentyFourHours(false); + + tt.SetOpeningTime({8_h + 15_min, 18_h + 30_min}); + TEST(tt.AddExcludeTime({10_h, 11_h}), ()); + TEST(tt.AddExcludeTime({12_h, 13_h}), ()); + TEST(tt.AddExcludeTime({15_h, 17_h}), ()); + TEST(tt.ReplaceExcludeTime({13_h, 14_h}, 1), ()); + TEST(!tt.ReplaceExcludeTime({10_h + 30_min, 14_h}, 1), ()); + TEST_EQUAL(tt.GetExcludeTime().size(), 3, ()); + } } UNIT_TEST(TestAppendTimeTable) diff --git a/editor/opening_hours_ui.cpp b/editor/opening_hours_ui.cpp index 6f5618f6cb..008e83ff20 100644 --- a/editor/opening_hours_ui.cpp +++ b/editor/opening_hours_ui.cpp @@ -115,7 +115,7 @@ TimeTable TimeTable::GetPredefinedTimeTable() osmoh::Weekday::Saturday }; - tt.m_openingTime = tt.GetPredifinedOpeningTime(); + tt.m_openingTime = tt.GetPredefinedOpeningTime(); return tt; } @@ -152,11 +152,19 @@ bool TimeTable::SetOpeningTime(osmoh::Timespan const & span) bool TimeTable::AddExcludeTime(osmoh::Timespan const & span) { - if (IsTwentyFourHours()) + return ReplaceExcludeTime(span, GetExcludeTime().size()); +} + +bool TimeTable::ReplaceExcludeTime(osmoh::Timespan const & span, size_t const index) +{ + if (IsTwentyFourHours() || index > m_excludeTime.size()) return false; auto copy = m_excludeTime; - copy.push_back(span); + if (index == m_excludeTime.size()) + copy.push_back(span); + else + copy[index] = span; if (!FixTimeSpans(copy, m_openingTime)) return false; @@ -191,7 +199,7 @@ bool TimeTable::IsValid() const return true; } -osmoh::Timespan TimeTable::GetPredifinedOpeningTime() const +osmoh::Timespan TimeTable::GetPredefinedOpeningTime() const { using osmoh::operator""_h; return {10_h, 22_h}; diff --git a/editor/opening_hours_ui.hpp b/editor/opening_hours_ui.hpp index 8672427a37..7447f85f93 100644 --- a/editor/opening_hours_ui.hpp +++ b/editor/opening_hours_ui.hpp @@ -30,12 +30,13 @@ public: bool SetOpeningTime(osmoh::Timespan const & span); bool AddExcludeTime(osmoh::Timespan const & span); + bool ReplaceExcludeTime(osmoh::Timespan const & span, size_t const index); bool RemoveExcludeTime(size_t const index); osmoh::TTimespans const & GetExcludeTime() const { return m_excludeTime; } bool IsValid() const; - osmoh::Timespan GetPredifinedOpeningTime() const; + osmoh::Timespan GetPredefinedOpeningTime() const; osmoh::Timespan GetPredefinedExcludeTime() const; private: