#pragma once // Spec: INV-social 11.6 (MessagesView.ViewModel): inbox loading and reload of the conversation list. #include #include #include #include #include "api/DevPlaceApi.hpp" #include "core/AlertMessage.hpp" #include "model/MessagesInbox.hpp" namespace dp { class MessagesModel { public: using AlertSink = std::function; explicit MessagesModel(DevPlaceApi& api, AlertSink alertSink = nullptr); ~MessagesModel() { *alive_ = false; } MessagesModel(const MessagesModel&) = delete; MessagesModel& operator=(const MessagesModel&) = delete; const std::optional& inbox() const { return inbox_; } const std::vector& conversations() const; bool isReloading() const { return isReloading_; } bool isBusy() const { return pending_ > 0; } // load(): no-op that clears the inbox when logged out; errors raise the alert and keep the old data. void load(); // reload(): like load() but re-entrancy guarded and flagged by isReloading(). void reload(); private: void fetch(bool reloading); DevPlaceApi& api_; AlertSink alertSink_; std::optional inbox_; bool isReloading_ = false; int pending_ = 0; std::shared_ptr alive_ = std::make_shared(true); }; } // namespace dp