summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2024-07-26 15:48:13 +0200
committerFelix Morgner <felix.morgner@gmail.com>2024-07-26 15:48:13 +0200
commit7a093920809394f7f31b75c444fe6009d1de7005 (patch)
treeb348b9731a6f771209fa5d1ca2330aae27402b2d /core/src
parent3d02c5f637f47493d15be3f18cc295010f08c727 (diff)
downloadturns-7a093920809394f7f31b75c444fe6009d1de7005.tar.xz
turns-7a093920809394f7f31b75c444fe6009d1de7005.zip
ui: tracker implement loading UI
Diffstat (limited to 'core/src')
-rw-r--r--core/src/turn_order.cpp48
1 files changed, 35 insertions, 13 deletions
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 <nlohmann/json.hpp>
#include <algorithm>
+#include <functional>
#include <ranges>
#include <typeinfo>
+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<nlohmann::json>{}) |
- std::views::transform([](auto const & j) { return participant::create(j); }) | std::ranges::to<std::vector>();
-
- 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<turn_order>
@@ -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<nlohmann::json>{});
+ 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{};