Skip to content
Repos

DevPlaceApp

C++ 5.4 KB116 lines Opens in site

Directory Raw Download

#include "screens/profile/ProfileScreen.hpp"

#include "screens/AppContext.hpp"
#include "screens/components/ProfileViews.hpp"
#include "screens/components/UserViews.hpp"
#include "screens/components/VFlowStack.hpp"
#include "spec/SocialSpec.hpp"
#include "ui/Controls.hpp"
#include "ui/Markdown.hpp"
#include "ui/Overlays.hpp"
#include "ui/Palette.hpp"

namespace dp {

using namespace ui;
using S = spec::SocialStrings;
using M = spec::SocialMetrics;

ProfileScreen::ProfileScreen(std::optional<std::string> username) : model_(std::move(username), AppContext::api()) {}

void ProfileScreen::onAppear() {
  if (started_) return;  // .task runs once per view identity
  started_ = true;
  model_.load();
}

void ProfileScreen::tapFollow() {
  if (model_.isFollowing()) model_.unfollow();
  else model_.follow();
}

void ProfileScreen::confirm(const std::string& verb, const std::string& message, std::function<void()> action) {
  AlertSpec a;
  a.title = verb + " " + model_.displayedUsername() + S::kConfirmTitleSuffix;
  a.message = message;
  a.buttons = {{verb, AlertButton::Role::Destructive, [alive = alive_, action = std::move(action)] { if (*alive) action(); }},
               {S::kCancel, AlertButton::Role::Cancel, nullptr}};
  Overlays::instance().alert(std::move(a));
}

void ProfileScreen::tapBlock() {
  if (model_.isBlocked()) model_.unblock();
  else confirm(S::kBlock, S::kBlockMessage, [this] { model_.block(); });
}

void ProfileScreen::tapMute() {
  if (model_.isMuted()) model_.unmute();
  else confirm(S::kMute, S::kMuteMessage, [this] { model_.mute(); });
}

ViewPtr ProfileScreen::actionButton(const std::string& title, std::function<void()> action) {
  return Button(Text(title)->lineLimit(1)->maxWidth(), [alive = alive_, action = std::move(action)] { if (*alive) action(); },
                ButtonStyles::accentGradient())
      ->frameFlex(std::nullopt, std::nullopt, M::kButtonMaxWidth, std::nullopt, std::nullopt, std::nullopt);
}

ViewPtr ProfileScreen::actionsMenu() {
  ViewPtr label = ButtonStyles::accentGradient()(Text(S::kActions)->lineLimit(1)->maxWidth(), false)
                      ->frameFlex(std::nullopt, std::nullopt, M::kButtonMaxWidth * 3, std::nullopt, std::nullopt, std::nullopt);
  return OnTapRect(label, [this, alive = alive_](Rect anchor) {
    if (!*alive) return;
    std::vector<MenuItem> items{
        {model_.isFollowing() ? S::kUnfollow : S::kFollow, "", false, false, [this, alive = alive_] { if (*alive) tapFollow(); }},
        {model_.isBlocked() ? S::kUnblock : S::kBlock, "", !model_.isBlocked(), false, [this, alive = alive_] { if (*alive) tapBlock(); }},
        {model_.isMuted() ? S::kUnmute : S::kMute, "", !model_.isMuted(), false, [this, alive = alive_] { if (*alive) tapMute(); }}};
    Overlays::instance().menu(anchor, std::move(items));
  });
}

ViewPtr ProfileScreen::buttonRow() {
  ViewPtr row = HStack({actionButton(model_.isFollowing() ? S::kUnfollow : S::kFollow, [this] { tapFollow(); }),
                        actionButton(model_.isBlocked() ? S::kUnblock : S::kBlock, [this] { tapBlock(); }),
                        actionButton(model_.isMuted() ? S::kUnmute : S::kMute, [this] { tapMute(); })},
                       M::kButtonsSpacing);
  // A wide button group becomes a dropdown when the three buttons no longer fit (small phones).
  return ViewThatFits({row, actionsMenu()});
}

ViewPtr ProfileScreen::body() {
  std::vector<ViewPtr> rows;
  if (const auto& profile = model_.profile()) {
    const User& user = profile->user;
    rows.push_back(UserImage(user, AvatarSize::Large)->paddingBottom(M::kProfileSpacing));

    std::vector<ViewPtr> stats{ProfileViews::stat(S::kPosts, ProfileViews::grouped(profile->postsCount)),
                               ProfileViews::stat(S::kLevel, ProfileViews::grouped(user.level)),
                               ProfileViews::stat(S::kStars, ProfileViews::grouped(user.stars))};
    if (profile->rank) stats.push_back(ProfileViews::stat(S::kRank, "#" + ProfileViews::grouped(*profile->rank)));
    rows.push_back(HStack(stats, M::kProfileStatsSpacing));

    rows.push_back(ProfileViews::levelProgress(profile->xpProgressPercent));

    rows.push_back(VStack({ProfileViews::sectionTitle(S::kBio)->maxWidth(Alignment::leading()),
                           MarkdownView(user.bio)->multilineAlign(TextAlign::Leading)->maxWidth(Alignment::leading()),
                           Divider(Palette::separator()),
                           ProfileViews::infoRow(S::kLocation, user.location),
                           Divider(Palette::separator()),
                           ProfileViews::linkRow(S::kGitLink, user.gitLink),
                           Divider(Palette::separator()),
                           ProfileViews::linkRow(S::kWebsite, user.website)},
                          M::kProfileSpacing));

    if (!model_.isMyOwnProfile()) rows.push_back(buttonRow()->paddingTop(M::kProfileSpacing));

    std::vector<ViewPtr> badges;
    for (std::size_t i = 0; i < profile->badges.size() && i < spec::SocialLimits::kMaxBadges; ++i) {
      badges.push_back(UserBadgeView(profile->badges[i]));
    }
    rows.push_back(VFlowStack(badges, HAlign::Center, M::kBadgeSpacing)->paddingTop(M::kProfileSpacing));
    rows.push_back(ProfileViews::webAppInfo(user.username)->paddingTop(M::kProfileSpacing));
  }
  ViewPtr column = VStack(rows, M::kProfileSpacing)->maxWidth()->padding(M::kListPadding);
  return ScrollView(VStack({NavLargeTitle(title()), column}, 0, HAlign::Center));
}

}  // namespace dp
34.2ms