From 7a093920809394f7f31b75c444fe6009d1de7005 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 26 Jul 2024 15:48:13 +0200 Subject: ui: tracker implement loading UI --- core/include/turns/core/turn_order.hpp | 1 + core/src/turn_order.cpp | 48 +++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 13 deletions(-) (limited to 'core') diff --git a/core/include/turns/core/turn_order.hpp b/core/include/turns/core/turn_order.hpp index 5dbc6c6..a617312 100644 --- a/core/include/turns/core/turn_order.hpp +++ b/core/include/turns/core/turn_order.hpp @@ -64,6 +64,7 @@ namespace turns::core auto stop() -> void; /** Serialization */ + auto load(nlohmann::json const & from) -> void; auto serialize() -> nlohmann::json; private: diff --git a/core/src/turn_order.cpp b/core/src/turn_order.cpp index 6442d37..3fb25d1 100644 --- a/core/src/turn_order.cpp +++ b/core/src/turn_order.cpp @@ -8,9 +8,12 @@ #include #include +#include #include #include +using namespace std::placeholders; + namespace turns::core { @@ -36,19 +39,7 @@ namespace turns::core turn_order::turn_order(nlohmann::json const & from) : turn_order{} { - m_round_number = from.value("round", invalid_round_number); - m_data = from.value("participants", std::vector{}) | - std::views::transform([](auto const & j) { return participant::create(j); }) | std::ranges::to(); - - auto active = std::ranges::find_if(m_data, [](auto participant) { return participant->is_active(); }); - if (active != std::ranges::end(m_data)) - { - m_active = std::ranges::distance(std::ranges::begin(m_data), active); - } - - m_is_empty = m_data.empty(); - m_has_next = !m_is_empty; - m_has_previous = m_round_number > 0 || m_active > 0; + load(from); } auto turn_order::create() -> Glib::RefPtr @@ -204,6 +195,37 @@ namespace turns::core } /** Serialization */ + auto turn_order::load(nlohmann::json const & from) -> void + { + auto old_size = get_n_items(); + + this->freeze_notify(); + + m_round_number = from.value("round", invalid_round_number); + + m_data.clear(); + auto participants = from.value("participants", std::vector{}); + auto factory = [](auto s) { + return participant::create(s); + }; + auto inserter = std::bind(&turn_order::insert, this, _1); + std::ranges::for_each(participants | std::views::transform(factory), inserter); + + auto active = std::ranges::find_if(m_data, [](auto participant) { return participant->is_active(); }); + if (active != std::ranges::end(m_data)) + { + m_active = std::ranges::distance(std::ranges::begin(m_data), active); + } + + m_is_empty = m_data.empty(); + m_has_next = !m_is_empty; + m_has_previous = m_round_number > 0 || m_active > 0; + + this->thaw_notify(); + + items_changed(0, old_size, get_n_items()); + } + auto turn_order::serialize() -> nlohmann::json { auto serialized = nlohmann::json{}; -- cgit v1.2.3