Skip to content
Repos

DevPlaceApp

C++ 3.7 KB94 lines Opens in site

Directory Raw Download

#pragma once
// Spec: PG-04, FR-CREATE-01, INV-posts 4 (CreatePost screen and view model) and 10 (poll creation).
// Create and edit a post. Presented by other screens as a sheet:
//   Overlays::instance().sheet(makeCreatePostScreen(editing), SheetStyle::FullScreen);
// For a completion callback (Swift onComplete) build the class directly:
//   auto screen = std::make_shared<CreatePostScreen>(editing); screen->setOnComplete([...]{...});
// Create mode keeps drafts (SettingsStore draftPostTitle / draftPostMessage, written on every change); edit mode
// prefills topic, title and content, hides attachments and poll, never touches drafts. Unsubmitted attachments are
// deleted when the screen disappears or is destroyed.
// DIFF: the topic and project pickers show only the selected value after the "Topic" / "Link to Project" label.
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "model/PostDetail.hpp"
#include "model/PostTopic.hpp"
#include "model/Project.hpp"
#include "screens/components/AttachmentUploader.hpp"
#include "screens/components/CreatePoll.hpp"
#include "ui/Navigation.hpp"
#include "ui/TextInput.hpp"

namespace dp {

class CreatePostScreen : public ui::Screen {
 public:
  explicit CreatePostScreen(std::optional<PostDetail> editing = std::nullopt);
  ~CreatePostScreen() override;

  // ---- ui::Screen ----
  ui::ViewPtr body() override;
  std::string title() const override;
  TitleMode titleMode() const override { return TitleMode::Inline; }
  ui::ViewPtr toolbarLeading() override;
  ui::ViewPtr toolbarTrailing() override;
  void onAppear() override;
  void onDisappear() override;
  bool busy() const override { return isLoading_ || projectsLoading_ || uploader_.isUploading(); }

  // ---- view model ----
  bool isEditing() const { return editing_.has_value(); }
  bool isLoading() const { return isLoading_; }
  bool canSubmit() const;
  void submit();
  void setTitle(const std::string& value);      // writes the draft in create mode
  void setMessage(const std::string& value);
  void setTopic(PostTopic topic) { topic_ = topic; }
  void setSelectedProject(std::optional<std::string> id) { selectedProjectId_ = std::move(id); }
  void addPoll();
  void removePoll();
  void loadProjects();
  void setOnComplete(std::function<void()> fn) { onComplete_ = std::move(fn); }

  const std::string& postTitle() const { return title_; }
  const std::string& message() const { return message_; }
  PostTopic topic() const { return topic_; }
  bool isPollAdded() const { return isPollAdded_; }
  CreatePoll& poll() { return poll_; }
  AttachmentUploader& uploader() { return uploader_; }
  const std::vector<Project::Data>& projects() const { return projects_; }
  const std::optional<std::string>& selectedProject() const { return selectedProjectId_; }

 private:
  ui::ViewPtr topicArea();
  ui::ViewPtr titleArea();
  ui::ViewPtr messageArea();
  ui::ViewPtr attachmentsArea();
  ui::ViewPtr pollArea();
  ui::ViewPtr projectArea();
  void resetFields();
  void dismiss();
  void cleanup();

  std::optional<PostDetail> editing_;
  PostTopic topic_ = PostTopic::Random;
  std::string title_;
  std::string message_;
  bool isPollAdded_ = false;
  CreatePoll poll_;
  AttachmentUploader uploader_;
  std::vector<Project::Data> projects_;
  std::optional<std::string> selectedProjectId_;
  bool isLoading_ = false;
  bool projectsLoading_ = false;
  bool projectsRequested_ = false;
  std::function<void()> onComplete_;
  std::shared_ptr<ui::TextInputHandle> titleHandle_ = std::make_shared<ui::TextInputHandle>();
  std::shared_ptr<ui::TextInputHandle> messageHandle_ = std::make_shared<ui::TextInputHandle>();
  std::shared_ptr<bool> alive_ = std::make_shared<bool>(true);
};

}  // namespace dp
48.7ms