Skip to content
Repos

DevPlaceApp

C++ 4.8 KB105 lines Opens in site

Directory Raw Download

#include "screens/components/ReportScreen.hpp"

#include "core/TextCount.hpp"
#include "spec/Layout.hpp"
#include "spec/PostsSpec.hpp"
#include "ui/Controls.hpp"
#include "ui/Overlays.hpp"
#include "ui/Palette.hpp"
#include "ui/TextInput.hpp"

namespace dp {

using namespace ui;
using spec::PostsText;

ReportScreen::ReportScreen(std::shared_ptr<ReportViewModel> model) : model_(std::move(model)) {}

ReportScreen::~ReportScreen() { *alive_ = false; }

void ReportScreen::onAppear() { model_->loadReasons(); }

ViewPtr ReportScreen::toolbarLeading() {
  return ToolbarTextButton(PostsText::kCancel, [this] { Overlays::instance().dismissSheetContaining(this); });
}

ViewPtr ReportScreen::toolbarTrailing() {
  if (model_->isSubmitting()) return ProgressSpinner(20);
  ViewPtr button = ToolbarTextButton(PostsText::kReportConfirm, [this] { confirm(); })->weight(Weight::Semibold);
  return model_->canSubmit() ? button : button->disabled();
}

void ReportScreen::confirm() {
  if (!model_->canSubmit()) return;
  auto alive = alive_;
  const std::string noun = model_->targetNoun();
  model_->submit([this, alive, noun](bool ok) {
    if (!ok) return;
    if (*alive) Overlays::instance().dismissSheetContaining(this);
    showThankYou(noun);
  });
}

void ReportScreen::showThankYou(const std::string& noun) {
  AlertSpec spec;
  spec.title = PostsText::kThankYouTitle;
  spec.message = std::string(PostsText::kThankYouBodyPrefix) + noun + PostsText::kThankYouBodySuffix;
  spec.buttons.push_back({PostsText::kOk, AlertButton::Role::Cancel, nullptr});
  Overlays::instance().alert(std::move(spec));
}

ViewPtr ReportScreen::body() {
  if (!model_->reasons().empty()) return reasonsForm();
  if (model_->didFailToLoadReasons()) return failedState();
  return ProgressSpinner(28)->maxSize();
}

ViewPtr ReportScreen::reasonRow(const ReportReason& reason) {
  const bool selected = model_->selectedReasonKey() && *model_->selectedReasonKey() == reason.key;
  std::vector<ViewPtr> parts{Text(reason.label)->foreground(Palette::fg1())->maxWidth(Alignment::leading())};
  if (selected) parts.push_back(Icon("checkmark", 17)->weight(Weight::Semibold)->foreground(Palette::accent()));
  const std::string key = reason.key;
  auto model = model_;
  return Button(HStack(parts, 8)->maxWidth()->frameFlex(std::nullopt, std::nullopt, std::nullopt, spec::Layout::kFormRowMinHeight, std::nullopt, std::nullopt),
                [model, key] { model->selectReason(key); }, ButtonStyles::form())
      ->id("reason-" + reason.key);
}

ViewPtr ReportScreen::reasonsForm() {
  auto header = [](const std::string& text) {
    return Text(text)->font(FontSpec::footnote().with(Weight::Medium))->foreground(Palette::fg2())->maxWidth(Alignment::leading())
        ->paddingH(spec::Layout::kFormSideInset)->paddingBottom(2);
  };
  std::vector<ViewPtr> reasonRows;
  for (const ReportReason& reason : model_->reasons()) reasonRows.push_back(FormRow(reasonRow(reason)));
  ViewPtr reasons = VStack({header(std::string(PostsText::kReasonsHeaderPrefix) + model_->targetNoun() + "?"),
                            GroupedCard(reasonRows, Palette::bg1())->maxWidth()}, 6, HAlign::Leading);

  auto model = model_;
  Binding detail{[model] { return model->detail; }, [model](const std::string& v) { model->detail = v; }};
  TextInputOptions opts;
  opts.placeholder = PostsText::kDetailsPlaceholder;
  opts.minLines = spec::PostsLayout::kReportDetailMinLines;
  opts.maxLines = spec::PostsLayout::kReportDetailMaxLines;
  ViewPtr field = FormRow(TextEditor(detail, opts)->paddingV(8));
  ViewPtr counter = HStack({Spacer(), CharacterCounter(TextCount::utf16(model_->detail), static_cast<int>(model_->detailLimit()))}, 0)
                        ->paddingH(spec::Layout::kFormSideInset);
  ViewPtr details = VStack({header(PostsText::kDetailsHeader), GroupedCard({field}, Palette::bg1())->maxWidth(), counter}, 6, HAlign::Leading);

  ViewPtr content = VStack({reasons, details}, 24, HAlign::Leading)->padding(Insets{8, 16, 24, 16});
  return ScrollView(ContentColumn(content->maxWidth(Alignment::top())), {})->maxSize();
}

ViewPtr ReportScreen::failedState() {
  auto model = model_;
  ViewPtr retry = Button(Text(PostsText::kRetry)->weight(Weight::Semibold)->foreground(Palette::accent())->paddingH(18)->paddingV(9)
                             ->background(Palette::accent().opacity(0.15f), Corners::all(100)),
                         [model] { model->loadReasons(); }, ButtonStyles::plain());
  return VStack({Icon("exclamationmark.triangle", 40)->foreground(Palette::fg2()),
                 Text(PostsText::kReasonsFailedTitle)->font(FontSpec::title2().with(Weight::Semibold)),
                 Text(PostsText::kReasonsFailedBody)->font(FontSpec::subheadline())->foreground(Palette::fg2())->multilineAlign(TextAlign::Center),
                 retry->paddingTop(8)}, 8)
      ->paddingH(24)->maxSize();
}

}  // namespace dp
29.1ms