#pragma once // Spec: INV-social 16.3 (ProfileView.ViewModel): profile load and the follow, block, mute actions that flip their // local flag only on success. #include #include #include #include #include "api/DevPlaceApi.hpp" #include "core/AlertMessage.hpp" #include "model/Profile.hpp" namespace dp { class ProfileModel { public: using AlertSink = std::function; ProfileModel(std::optional username, DevPlaceApi& api, AlertSink alertSink = nullptr); ~ProfileModel() { *alive_ = false; } ProfileModel(const ProfileModel&) = delete; ProfileModel& operator=(const ProfileModel&) = delete; const std::optional& profile() const { return profile_; } // username ?? profile.user.username ?? "Profile" std::string navigationTitle() const; bool isMyOwnProfile() const { return profile_ && profile_->isOwner; } std::string displayedUsername() const { return profile_ ? profile_->user.username : ""; } bool isFollowing() const { return isFollowing_; } bool isBlocked() const { return isBlocked_; } bool isMuted() const { return isMuted_; } bool isLoading() const { return isLoading_; } bool isBusy() const { return pending_ > 0; } void load(std::function done = nullptr); void follow() { act(Action::Follow); } void unfollow() { act(Action::Unfollow); } void block() { act(Action::Block); } void unblock() { act(Action::Unblock); } void mute() { act(Action::Mute); } void unmute() { act(Action::Unmute); } private: enum class Action { Follow, Unfollow, Block, Unblock, Mute, Unmute }; void act(Action action); std::optional username_; DevPlaceApi& api_; AlertSink alertSink_; std::optional profile_; bool isFollowing_ = false; bool isBlocked_ = false; bool isMuted_ = false; bool isLoading_ = false; int pending_ = 0; std::shared_ptr alive_ = std::make_shared(true); }; } // namespace dp