Repos
C++
1.9 KB59 lines Opens in site
Directory
Raw
Download
#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 <functional>
#include <memory>
#include <optional>
#include <string>
#include "api/DevPlaceApi.hpp"
#include "core/AlertMessage.hpp"
#include "model/Profile.hpp"
namespace dp {
class ProfileModel {
public:
using AlertSink = std::function<void(const AlertMessage&)>;
ProfileModel(std::optional<std::string> username, DevPlaceApi& api, AlertSink alertSink = nullptr);
~ProfileModel() { *alive_ = false; }
ProfileModel(const ProfileModel&) = delete;
ProfileModel& operator=(const ProfileModel&) = delete;
const std::optional<Profile>& 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<void()> 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<std::string> username_;
DevPlaceApi& api_;
AlertSink alertSink_;
std::optional<Profile> profile_;
bool isFollowing_ = false;
bool isBlocked_ = false;
bool isMuted_ = false;
bool isLoading_ = false;
int pending_ = 0;
std::shared_ptr<bool> alive_ = std::make_shared<bool>(true);
};
} // namespace dp