Skip to content
Repos

DevPlaceApp

C++ 2.8 KB72 lines Opens in site

Directory Raw Download

#pragma once
// Spec: INV-social 18 (AttachmentUploaderView + ViewModel), FR-UP-01. Object oriented uploader.
//
//   AttachmentUploader up({isSmall, initial, onAttachmentsChange});
//   ViewPtr view();                       rebuilt every frame by the owning screen
//   attachments()                          current list (UploadResponse)
//   isUploading()                          true while an upload or a delete is in flight (isBusy)
//   deleteUnsubmittedAttachments()         api.deleteAttachment for each, then clears the list (screen disappear)
//   forget()                               clears the list without deleting (after a successful submit)
//
// Sources: file dialog (Files / Photo Library = media filter, SDL dialog thread marshalled to the UI thread), upload
// by URL (alert with a text field). Cap: spec::ModelLimits::kMaxAttachmentsCount (10); files over
// spec::ComposeSpec::kMaxUploadFileBytes are rejected with a message. No progress percentage (indeterminate spinner),
// as in Swift. DIFF: no camera roll; "Photo Library" opens the file dialog filtered to images and videos.
#include <functional>
#include <memory>
#include <string>
#include <vector>

#include "core/Result.hpp"
#include "model/UploadResponse.hpp"
#include "ui/Views.hpp"

namespace dp {

class AttachmentUploader {
 public:
  struct Options {
    bool isSmall = false;
    std::vector<UploadResponse> attachments;
    std::function<void(const std::vector<UploadResponse>&)> onAttachmentsChange;
  };

  AttachmentUploader() : AttachmentUploader(Options{}) {}
  explicit AttachmentUploader(Options options);
  ~AttachmentUploader();
  AttachmentUploader(const AttachmentUploader&) = delete;
  AttachmentUploader& operator=(const AttachmentUploader&) = delete;

  ui::ViewPtr view();
  const std::vector<UploadResponse>& attachments() const { return attachments_; }
  bool isUploading() const { return busy_; }
  bool canAddMore() const;

  void deleteUnsubmittedAttachments();
  void forget();

  // Entry points used by the view and by tests and scripts.
  void uploadFromUrl(const std::string& url);
  void uploadFile(const std::string& data, const std::string& filename, const std::string& mimeType);
  void uploadPath(const std::string& path);
  void remove(const UploadResponse& attachment);

  static std::string mimeTypeForFilename(const std::string& filename);
  static std::string iconFor(const UploadResponse& attachment);  // photo, film or doc

 private:
  void changed();
  void showAddChoice();
  void showUrlPrompt();
  void pickFile(bool mediaOnly);
  ui::ViewPtr addButton();
  ui::ViewPtr row(const UploadResponse& attachment);
  void finishUpload(Result<UploadResponse> result);

  Options options_;
  std::vector<UploadResponse> attachments_;
  bool busy_ = false;
  std::shared_ptr<bool> alive_;
};

}  // namespace dp
48.5ms