Skip to content
Repos

DevPlaceApp

C++ 1.9 KB57 lines Opens in site

Directory Raw Download

#pragma once
// Spec: INV-posts 11 (ReportButton+ViewModel). Reasons loading, selection, detail text and submission.
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "core/Result.hpp"
#include "model/ReportReason.hpp"
#include "model/TargetType.hpp"

namespace dp {

class ReportViewModel {
 public:
  ReportViewModel(TargetType type, std::string targetId);

  // Cached per target so the loaded reasons survive between presentations.
  static std::shared_ptr<ReportViewModel> shared(TargetType type, const std::string& targetId);
  static void clearCache();

  TargetType targetType() const { return type_; }
  const std::string& targetId() const { return targetId_; }
  // "comment" for comments, "post" otherwise.
  std::string targetNoun() const;
  std::string sheetTitle() const;
  std::size_t detailLimit() const;

  const std::vector<ReportReason>& reasons() const { return reasons_; }
  bool didFailToLoadReasons() const { return failed_; }
  bool isSubmitting() const { return submitting_; }
  const std::optional<std::string>& selectedReasonKey() const { return selected_; }
  void selectReason(const std::string& key) { selected_ = key; }
  std::string detail;

  bool isDetailWithinLimit() const;
  bool canSubmit() const;
  void prepareForPresentation();
  // Only when reasons are empty and no load is running (RBVM.swift:55-66).
  void loadReasons();
  // Trims the detail (whitespace and newlines), nullopt when empty. `done(true)` on success.
  void submit(std::function<void(bool)> done);
  static std::optional<std::string> trimmedDetail(const std::string& detail);

 private:
  TargetType type_;
  std::string targetId_;
  std::vector<ReportReason> reasons_;
  bool loading_ = false;
  bool failed_ = false;
  bool submitting_ = false;
  std::optional<std::string> selected_;
  std::weak_ptr<ReportViewModel> self_;
};

}  // namespace dp
40.2ms