#include "screens/components/ProfileViews.hpp"
#include <algorithm>
#include <cmath>
#include "screens/notifications/UrlSyntax.hpp"
#include "spec/SocialSpec.hpp"
#include "ui/Palette.hpp"
namespace dp {
using namespace ui;
using S = spec::SocialStrings;
using M = spec::SocialMetrics;
namespace {
class BarView : public View {
public:
explicit BarView(double fraction) : fraction_(std::clamp(fraction, 0.0, 1.0)) {}
protected:
Size compute(const Proposal& p) override { return {p.w ? std::min(*p.w, 10000.0f) : 100.0f, M::kProgressBarHeight}; }
public:
void draw(Canvas& c) override {
const float r = frame_.h / 2;
c.fillRRect(frame_, Corners::all(r), Palette::fill());
const float w = static_cast<float>(frame_.w * fraction_);
if (w > 0) c.fillRRect({frame_.x, frame_.y, w, frame_.h}, Corners::all(r), Palette::accent());
}
private:
double fraction_;
};
} // namespace
std::string ProfileViews::grouped(std::int64_t value) {
const bool negative = value < 0;
std::string digits = std::to_string(negative ? -value : value), out;
int n = 0;
for (auto it = digits.rbegin(); it != digits.rend(); ++it, ++n) {
if (n && n % 3 == 0) out.insert(out.begin(), ',');
out.insert(out.begin(), *it);
}
return negative ? "-" + out : out;
}
ViewPtr ProfileViews::stat(const std::string& label, const std::string& value) {
std::string upper = label;
for (char& c : upper) if (c >= 'a' && c <= 'z') c = static_cast<char>(c - 32);
return VStack({Text(value)->font(FontSpec::system(M::kStatValue, Weight::Bold)),
Text(upper)->font(FontSpec::system(M::kStatLabel))->foreground(Palette::fg2())},
M::kStatSpacing, HAlign::Center);
}
ViewPtr ProfileViews::progressBar(double fraction) { return std::make_shared<BarView>(fraction)->maxWidth(); }
ViewPtr ProfileViews::levelProgress(std::int64_t percent) {
const double fraction = static_cast<double>(percent) / 100.0;
ViewPtr head = HStack({Text(S::kProgress), Spacer(), Text(std::to_string(percent) + "%")}, 8)
->font(FontSpec::system(M::kProgressText))->foreground(Palette::fg2());
return VStack({head, progressBar(fraction)}, M::kProgressSpacing)->paddingV(M::kProfileSpacing);
}
ViewPtr ProfileViews::sectionTitle(const std::string& title) {
return Text(title)->weight(Weight::Semibold)->foreground(Palette::fg2())->multilineAlign(TextAlign::Leading);
}
ViewPtr ProfileViews::infoRow(const std::string& title, const std::string& text) {
return HStack({sectionTitle(title), Spacer(), Text(text)->multilineAlign(TextAlign::Trailing)}, 8);
}
ViewPtr ProfileViews::linkRow(const std::string& title, const std::string& link) {
std::vector<ViewPtr> row{sectionTitle(title), Spacer()};
if (UrlSyntax::valid(link)) {
StyledSpan span;
span.text = link;
span.color = Palette::accent();
span.link = link;
row.push_back(RichText({span})->multilineAlign(TextAlign::Trailing));
}
return HStack(row, 8);
}
ViewPtr ProfileViews::webAppInfo(const std::string& username) {
StyledSpan before;
before.text = S::kWebAppPrefix;
StyledSpan link;
link.text = S::kWebAppLinkText;
link.color = Palette::accent();
link.link = std::string(S::kProfileUrlPrefix) + username;
StyledSpan after;
after.text = S::kWebAppSuffix;
return RichText({before, link, after})->multilineAlign(TextAlign::Center);
}
} // namespace dp