#pragma once // Spec: shell.md section 4 (AppState, AppState+Vote, AppState+React). Singleton UI-thread state. Every mutation // bumps version() and notifies subscribers (Observable). DIFF: updateAppIconBadge (APNs/UNUserNotificationCenter) // is not ported, there is no icon badge on desktop. The feed is capped at ModelLimits::kMaxFeedPosts. #include #include #include #include "api/ApiTypes.hpp" #include "core/Observable.hpp" #include "model/AuthToken.hpp" #include "model/Feed.hpp" #include "model/TargetType.hpp" #include "model/User.hpp" #include "model/Vote.hpp" namespace dp { class DevPlaceApi; class AppState : public Observable { public: static AppState& instance(); const std::optional& token() const { return token_; } bool isLoggedIn() const { return token_.has_value(); } const std::optional& currentUser() const { return currentUser_; } const std::optional& feed() const { return feed_; } std::int64_t unreadNotificationCount() const { return unreadNotificationCount_; } std::int64_t unreadMessageCount() const { return unreadMessageCount_; } const std::optional& pendingConversationUid() const { return pendingConversationUid_; } void setToken(std::optional value); void setCurrentUser(std::optional value); void setFeed(std::optional value); void setUnreadCounts(std::int64_t notifications, std::int64_t messages); void setPendingConversationUid(std::optional value); bool isCurrentUser(const std::string& id) const { return currentUser_ && currentUser_->id == id; } // First page of the feed, then unread counts. A feed failure leaves the feed untouched and is reported. void loadFeed(DevPlaceApi& api, Done done); // Errors are logged ("Failed to load unread counts: ..."), never reported. `done` runs afterwards. void loadUnreadCounts(DevPlaceApi& api, std::function done = {}); // api.messages(withUid).otherUser, nullopt on error (logged). void conversationUser(const std::string& uid, DevPlaceApi& api, std::function)> done); // Needs a feed with nextCursor, else completes ok at once. Duplicates (by post id) are dropped and logged. void loadMoreFeed(DevPlaceApi& api, Done done); // token, feed, currentUser nil; counts 0; pendingConversationUid nil. void clear(); void updatePostVoteInFeed(const std::string& postId, Vote vote, std::int64_t count); void updateCommentVoteInFeed(const std::string& commentId, Vote vote); void updatePostReactionInFeed(const std::string& postId, const std::string& emoji); void updateCommentReactionInFeed(const std::string& commentId, const std::string& emoji); // Optimistic upvote toggle: apply(newVote) now (up -> none, otherwise up), api.vote(.up) always, on error // apply(currentVote) and a log line. `done` runs after the request finished (success or not). void performVoteToggle(TargetType targetType, const std::string& targetId, Vote currentVote, DevPlaceApi& api, std::function apply, std::function done = {}); // Optimistic reaction toggle: apply() now, api.react, on error apply() again (toggles back) and a log line. void performReactionToggle(TargetType targetType, const std::string& targetId, const std::string& emoji, DevPlaceApi& api, std::function apply, std::function done = {}); private: AppState() = default; std::optional token_; std::optional currentUser_; std::optional feed_; std::int64_t unreadNotificationCount_ = 0; std::int64_t unreadMessageCount_ = 0; std::optional pendingConversationUid_; }; } // namespace dp