#include "store/AppState.hpp" #include #include "api/DevPlaceApi.hpp" #include "core/Log.hpp" #include "model/CommentTree.hpp" #include "spec/ModelLimits.hpp" namespace dp { AppState& AppState::instance() { static AppState state; return state; } void AppState::setToken(std::optional value) { token_ = std::move(value); notifyChanged(); } void AppState::setCurrentUser(std::optional value) { currentUser_ = std::move(value); notifyChanged(); } void AppState::setFeed(std::optional value) { feed_ = std::move(value); notifyChanged(); } void AppState::setUnreadCounts(std::int64_t notifications, std::int64_t messages) { unreadNotificationCount_ = notifications; unreadMessageCount_ = messages; notifyChanged(); } void AppState::setPendingConversationUid(std::optional value) { pendingConversationUid_ = std::move(value); notifyChanged(); } void AppState::loadFeed(DevPlaceApi& api, Done done) { api.feed([this, &api, done = std::move(done)](Result r) { if (!r.ok()) return done(Result(r.error())); setFeed(std::move(r.value())); loadUnreadCounts(api, [done] { done(Result(Unit{})); }); }); } void AppState::loadUnreadCounts(DevPlaceApi& api, std::function done) { api.notificationCounts([this, done = std::move(done)](Result r) { if (r.ok()) setUnreadCounts(r.value().notifications, r.value().messages); else Log::debug("Failed to load unread counts: " + r.error().message); if (done) done(); }); } void AppState::conversationUser(const std::string& uid, DevPlaceApi& api, std::function)> done) { api.messages(std::optional(uid), [uid, done = std::move(done)](Result r) { if (!r.ok()) { Log::debug("Failed to resolve conversation user for uid " + uid + ": " + r.error().message); return done(std::nullopt); } done(r.value().otherUser); }); } void AppState::loadMoreFeed(DevPlaceApi& api, Done done) { if (!feed_ || !feed_->nextCursor) return done(Result(Unit{})); const Date cursor = *feed_->nextCursor; api.feed(std::optional(cursor), [this, done = std::move(done)](Result r) { if (!r.ok()) return done(Result(r.error())); if (!feed_) return done(Result(Unit{})); // cleared while the request was in flight Feed merged = std::move(r.value()); // all scalar fields come from the next page std::vector posts = feed_->posts; std::set existing; for (const auto& p : posts) existing.insert(p.id()); bool capped = false; for (auto& p : merged.posts) { if (existing.count(p.id())) { Log::debug("load-more in feed returned duplicate post from backend: " + p.id()); continue; } if (posts.size() >= spec::ModelLimits::kMaxFeedPosts) { capped = true; continue; } posts.push_back(std::move(p)); } merged.posts = std::move(posts); if (capped) { Log::warn("feed reached its cap, no further pages are loaded"); merged.nextCursor.reset(); } setFeed(std::move(merged)); done(Result(Unit{})); }); } void AppState::clear() { token_.reset(); feed_.reset(); currentUser_.reset(); unreadNotificationCount_ = 0; unreadMessageCount_ = 0; pendingConversationUid_.reset(); notifyChanged(); } void AppState::updatePostVoteInFeed(const std::string& postId, Vote vote, std::int64_t count) { if (!feed_) return; bool found = false; std::vector posts = feed_->posts; for (auto& post : posts) { if (post.data.id != postId) continue; post.data.stars = count; post.myVote = vote; found = true; } if (!found) return; setFeed(feed_->replacingPosts(posts)); } void AppState::updateCommentVoteInFeed(const std::string& commentId, Vote vote) { if (!feed_) return; std::vector posts; posts.reserve(feed_->posts.size()); for (const auto& post : feed_->posts) { posts.push_back(post.replacingRecentComments(CommentTree::updatingVote(post.recentComments, commentId, vote))); } setFeed(feed_->replacingPosts(posts)); } void AppState::updatePostReactionInFeed(const std::string& postId, const std::string& emoji) { if (!feed_) return; bool found = false; std::vector posts; posts.reserve(feed_->posts.size()); for (const auto& post : feed_->posts) { if (post.data.id == postId) { posts.push_back(post.replacingReactions(post.reactions.toggling(emoji))); found = true; } else { posts.push_back(post); } } if (!found) return; setFeed(feed_->replacingPosts(posts)); } void AppState::updateCommentReactionInFeed(const std::string& commentId, const std::string& emoji) { if (!feed_) return; std::vector posts; posts.reserve(feed_->posts.size()); for (const auto& post : feed_->posts) { posts.push_back(post.replacingRecentComments(CommentTree::updatingReaction(post.recentComments, commentId, emoji))); } setFeed(feed_->replacingPosts(posts)); } void AppState::performVoteToggle(TargetType targetType, const std::string& targetId, Vote currentVote, DevPlaceApi& api, std::function apply, std::function done) { apply(currentVote == Vote::Up ? Vote(Vote::None) : Vote(Vote::Up)); api.vote(targetType, targetId, Vote(Vote::Up), [currentVote, apply = std::move(apply), done = std::move(done)](Result r) { if (!r.ok()) { apply(currentVote); Log::debug("Double-tap vote failed: " + r.error().message); } if (done) done(); }); } void AppState::performReactionToggle(TargetType targetType, const std::string& targetId, const std::string& emoji, DevPlaceApi& api, std::function apply, std::function done) { apply(); api.react(targetType, targetId, emoji, [apply = std::move(apply), done = std::move(done)](Result r) { if (!r.ok()) { apply(); Log::debug("Reaction toggle failed: " + r.error().message); } if (done) done(); }); } } // namespace dp