#include "screens/comments/CommentsSession.hpp" #include #include "core/Log.hpp" #include "screens/AppContext.hpp" #include "spec/PostsSpec.hpp" #include "store/SettingsStore.hpp" namespace dp { CommentsSession::CommentsSession() = default; CommentsSession::~CommentsSession() = default; void CommentsSession::beginReply(const std::string& anchor) { discardAttachments(); activeReply_ = anchor; ++epoch_; } void CommentsSession::cancelReply() { discardAttachments(); activeReply_.reset(); } std::vector CommentsSession::takeAttachmentsAndClose() { std::vector taken = std::move(attachments_); attachments_.clear(); activeReply_.reset(); return taken; } void CommentsSession::setAttachments(std::vector list) { if (list.size() > spec::PostsLimits::kMaxPendingAttachments) list.resize(spec::PostsLimits::kMaxPendingAttachments); attachments_ = std::move(list); } void CommentsSession::discardAttachments() { std::vector unsubmitted = std::move(attachments_); attachments_.clear(); if (unsubmitted.empty()) return; for (const UploadResponse& attachment : unsubmitted) { const std::string id = attachment.id; AppContext::api().deleteAttachment(id, [id](Result r) { if (!r.ok()) Log::warn("Failed to delete unsubmitted comment attachment " + id + ": " + r.error().message); }); } } CommentsSession::RowState& CommentsSession::row(const std::string& commentId) { if (rows_.size() > spec::PostsLimits::kMaxRenderedComments && !rows_.count(commentId)) { for (auto it = rows_.begin(); it != rows_.end();) it = it->second.isEditing ? std::next(it) : rows_.erase(it); } return rows_[commentId]; } void CommentsSession::beginEditing(const std::string& commentId, const std::string& content) { RowState& r = row(commentId); r.editedText = content; r.isEditing = true; editingCommentId_ = commentId; } void CommentsSession::endEditing(const std::string& commentId) { row(commentId).isEditing = false; if (editingCommentId_ && *editingCommentId_ == commentId) editingCommentId_.reset(); } std::optional CommentsSession::takePendingEditQuote() { std::optional quote = std::move(pendingEditQuote_); pendingEditQuote_.reset(); return quote; } void CommentsSession::insertQuote(const std::string& quote) { if (editingCommentId_) { pendingEditQuote_ = quote; } else if (activeReply_) { SettingsStore& settings = SettingsStore::instance(); settings.setDraftReplyMessage(settings.draftReplyMessage() + quote); } } CommentEditor& CommentsSession::editor(const std::string& key) { touched_.insert(key); auto it = editors_.find(key); if (it == editors_.end()) it = editors_.emplace(key, std::make_unique(key)).first; return *it->second; } float CommentsSession::mentionListHeight() const { float h = 0; for (const auto& [key, editor] : editors_) h = std::max(h, editor->mentionListHeight()); return h; } void CommentsSession::beginFrame() { retired_.clear(); touched_.clear(); } void CommentsSession::endFrame() { for (auto it = editors_.begin(); it != editors_.end();) { if (touched_.count(it->first)) { ++it; } else { retired_.push_back(std::move(it->second)); it = editors_.erase(it); } } } void CommentsSession::reset() { activeReply_.reset(); editingCommentId_.reset(); pendingEditQuote_.reset(); attachments_.clear(); rows_.clear(); editors_.clear(); retired_.clear(); touched_.clear(); } } // namespace dp