Skip to content
Repos

DevPlaceApp

C++ 3.6 KB100 lines Opens in site

Directory Raw Download

#include "screens/components/PollViewModel.hpp"

#include <cmath>

#include "screens/AppContext.hpp"
#include "screens/components/AlertPresenter.hpp"
#include "screens/post/ModelCache.hpp"
#include "spec/PostsSpec.hpp"
#include "store/AppState.hpp"

namespace dp {

namespace {
ModelCache<PollViewModel>& cache() {
  static ModelCache<PollViewModel> c;
  return c;
}

bool samePoll(const Poll& a, const Poll& b) {
  if (a.id != b.id || a.question != b.question || a.total != b.total || a.myChoice != b.myChoice || a.voted != b.voted) return false;
  if (a.options.size() != b.options.size()) return false;
  for (std::size_t i = 0; i < a.options.size(); ++i) {
    const PollOption& x = a.options[i];
    const PollOption& y = b.options[i];
    if (x.id != y.id || x.label != y.label || x.count != y.count || x.votes != y.votes || x.pct != y.pct) return false;
  }
  return true;
}
}  // namespace

PollViewModel::PollViewModel(const Poll& poll)
    : pollId_(poll.id), question_(poll.question), options_(poll.options), total_(poll.total), myChoice_(poll.myChoice), prop_(poll) {}

std::shared_ptr<PollViewModel> PollViewModel::shared(const Poll& poll) {
  auto model = cache().get(poll.id, [&] {
    auto m = std::make_shared<PollViewModel>(poll);
    m->self_ = m;
    return m;
  });
  model->syncProps(poll);
  return model;
}

void PollViewModel::clearCache() { cache().clear(); }

void PollViewModel::syncProps(const Poll& poll) {
  if (samePoll(poll, prop_)) return;
  prop_ = poll;
  options_ = poll.options;
  total_ = poll.total;
  myChoice_ = poll.myChoice;
}

bool PollViewModel::userCanVote() const { return AppState::instance().isLoggedIn(); }

std::string PollViewModel::votesLabel() const {
  return std::to_string(total_) + " " + (total_ == 1 ? spec::PostsText::kVoteSingular : spec::PostsText::kVotePlural);
}

PollViewModel::Recomputed PollViewModel::recompute(const std::vector<PollOption>& options, std::int64_t total,
                                                   const std::optional<std::string>& previousChoice,
                                                   const std::optional<std::string>& newChoice) {
  Recomputed out;
  out.total = total;
  if (!previousChoice && newChoice) out.total += 1;
  else if (previousChoice && !newChoice) out.total -= 1;
  for (const PollOption& option : options) {
    PollOption next = option;
    if (previousChoice && option.id == *previousChoice) next.count -= 1;
    if (newChoice && option.id == *newChoice) next.count += 1;
    next.pct = out.total > 0 ? static_cast<std::int64_t>(std::round(static_cast<double>(next.count) / static_cast<double>(out.total) * 100.0)) : 0;
    out.options.push_back(std::move(next));
  }
  return out;
}

void PollViewModel::select(const std::string& optionId) {
  if (loading_ || !userCanVote()) return;
  std::optional<std::string> newChoice;
  if (!(myChoice_ && *myChoice_ == optionId)) newChoice = optionId;
  loading_ = true;
  const std::optional<std::string> previousChoice = myChoice_;
  const std::vector<PollOption> previousOptions = options_;
  const std::int64_t previousTotal = total_;
  Recomputed updated = recompute(options_, total_, previousChoice, newChoice);
  options_ = std::move(updated.options);
  total_ = updated.total;
  myChoice_ = newChoice;
  auto keep = self_.lock();
  AppContext::api().submitPollChoice(pollId_, optionId, [this, keep, previousChoice, previousOptions, previousTotal](Result<Unit> r) {
    loading_ = false;
    if (r.ok()) return;
    options_ = previousOptions;
    total_ = previousTotal;
    myChoice_ = previousChoice;
    AlertPresenter::showError(r.error());
  });
}

}  // namespace dp
53.4ms