Skip to content
Repos

DevPlaceApp

C++ 1.3 KB44 lines Opens in site

Directory Raw Download

#pragma once
// Spec: INV-social 11.6 (MessagesView.ViewModel): inbox loading and reload of the conversation list.
#include <functional>
#include <memory>
#include <optional>
#include <vector>

#include "api/DevPlaceApi.hpp"
#include "core/AlertMessage.hpp"
#include "model/MessagesInbox.hpp"

namespace dp {

class MessagesModel {
 public:
  using AlertSink = std::function<void(const AlertMessage&)>;

  explicit MessagesModel(DevPlaceApi& api, AlertSink alertSink = nullptr);
  ~MessagesModel() { *alive_ = false; }
  MessagesModel(const MessagesModel&) = delete;
  MessagesModel& operator=(const MessagesModel&) = delete;

  const std::optional<MessagesInbox>& inbox() const { return inbox_; }
  const std::vector<Conversation>& 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<MessagesInbox> inbox_;
  bool isReloading_ = false;
  int pending_ = 0;
  std::shared_ptr<bool> alive_ = std::make_shared<bool>(true);
};

}  // namespace dp
42.1ms