Repos
C++
3.5 KB124 lines Opens in site
Directory
Raw
Download
#include "screens/comments/CommentsSession.hpp"
#include <algorithm>
#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<UploadResponse> CommentsSession::takeAttachmentsAndClose() {
std::vector<UploadResponse> taken = std::move(attachments_);
attachments_.clear();
activeReply_.reset();
return taken;
}
void CommentsSession::setAttachments(std::vector<UploadResponse> list) {
if (list.size() > spec::PostsLimits::kMaxPendingAttachments) list.resize(spec::PostsLimits::kMaxPendingAttachments);
attachments_ = std::move(list);
}
void CommentsSession::discardAttachments() {
std::vector<UploadResponse> 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<Unit> 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<std::string> CommentsSession::takePendingEditQuote() {
std::optional<std::string> 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<CommentEditor>(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