#include "screens/auth/SignUpScreen.hpp" #include "core/TextCount.hpp" #include "model/BirthDate.hpp" #include "screens/AppContext.hpp" #include "screens/components/AlertPresenter.hpp" #include "screens/components/Box.hpp" #include "spec/AuthSpec.hpp" #include "ui/Overlays.hpp" #include "ui/Palette.hpp" namespace dp { using namespace ui; using spec::AuthSpec; SignUpScreen::SignUpScreen() = default; std::string SignUpScreen::title() const { return AuthSpec::kSignUpTitle; } bool SignUpScreen::canSubmit() const { return !username_.empty() && !email_.empty() && !password_.empty() && !confirm_.empty() && tosAccepted_; } namespace { bool usernameCharOk(const std::string& s) { for (std::size_t i = 0; i < s.size(); ++i) { const unsigned char c = static_cast(s[i]); if (c >= 0x80) continue; // non-ASCII letters and digits accepted (Swift isLetter/isNumber are Unicode aware) if (!(std::isalnum(c) || c == '-' || c == '_')) return false; } return true; } } // namespace std::optional SignUpScreen::validationError() const { const int u = TextCount::codepoints(username_), e = TextCount::codepoints(email_); const int p = TextCount::codepoints(password_); if (u < AuthSpec::kUsernameMin || u > AuthSpec::kUsernameMax) return AuthSpec::kErrUsernameLength; if (!usernameCharOk(username_)) return AuthSpec::kErrUsernameChars; if (e < AuthSpec::kEmailMin || e > AuthSpec::kEmailMax) return AuthSpec::kErrEmailLength; if (email_.find('@') == std::string::npos) return AuthSpec::kErrEmailFormat; if (p < AuthSpec::kPasswordMin || p > AuthSpec::kPasswordMax) return AuthSpec::kErrPasswordLength; if (password_ != confirm_) return AuthSpec::kErrPasswordMatch; if (!tosAccepted_) return AuthSpec::kErrTerms; return std::nullopt; } void SignUpScreen::signUp() { if (isLoading_) return; if (auto err = validationError()) { AlertPresenter::showError(*err); return; } isLoading_ = true; auto alive = alive_; BirthDate birth{birth_.year, birth_.month, birth_.day}; AppContext::api().signUp(username_, email_, password_, confirm_, birth, tosAccepted_, [this, alive](Result r) { if (!*alive) return; isLoading_ = false; if (!r.ok()) { AlertPresenter::showError(r.error()); return; } Navigator* nav = navigator(); ui::AlertSpec a; a.title = AuthSpec::kAccountCreatedTitle; a.message = AuthSpec::kAccountCreatedMessage; a.buttons = {{"OK", ui::AlertButton::Role::Cancel, [nav] { if (nav) nav->pop(); }}}; Overlays::instance().alert(std::move(a)); }); } ViewPtr SignUpScreen::field(const std::string& placeholder, std::string* value, bool secure, int minCount, int maxCount, std::shared_ptr handle, std::shared_ptr next) { TextInputOptions o; o.placeholder = placeholder; o.secure = secure; o.handle = handle; if (next) o.onSubmit = [next] { next->focus(); }; else o.onSubmit = [this] { signUp(); }; ViewPtr input = DevPlaceField(TextField(Binding{[value] { return *value; }, [value](const std::string& v) { *value = v; }}, o)); ViewPtr counter = CharacterCounter(TextCount::codepoints(*value), maxCount, minCount)->paddingH(10)->maxWidth(Alignment::leading()); return VStack({input, counter}, 4, HAlign::Leading); } ViewPtr SignUpScreen::body() { ViewPtr fields = VStack({field(AuthSpec::kUsername, &username_, false, AuthSpec::kUsernameMin, AuthSpec::kUsernameMax, hUser_, hEmail_), field(AuthSpec::kEmail, &email_, false, AuthSpec::kEmailMin, AuthSpec::kEmailMax, hEmail_, hPass_), field(AuthSpec::kPassword, &password_, true, AuthSpec::kPasswordMin, AuthSpec::kPasswordMax, hPass_, hConfirm_), field(AuthSpec::kConfirmPassword, &confirm_, true, AuthSpec::kPasswordMin, AuthSpec::kPasswordMax, hConfirm_, nullptr)}, 10) ->disabled(isLoading_); ViewPtr dob = VStack({DatePicker(Text(AuthSpec::kDateOfBirth), [this] { return birth_; }, [this](CivilDate d) { birth_ = d; }, CivilDate::today())->disabled(isLoading_), Text(AuthSpec::kDobFootnote)->font(FontSpec::footnote())->foreground(Palette::fg2())->maxWidth(Alignment::leading())}, 8, HAlign::Leading) ->maxWidth(Alignment::leading()); ViewPtr terms = VStack({RichText({StyledSpan{AuthSpec::kReadTerms, {}, Palette::accent(), false, false, {}, AuthSpec::kTermsUrl}})->maxWidth(Alignment::leading()), Toggle(Text(AuthSpec::kAcceptTerms)->foreground(Palette::fg2()), BoolBinding{[this] { return tosAccepted_; }, [this](bool v) { tosAccepted_ = v; }}, Palette::accent()) ->disabled(isLoading_)}, 12, HAlign::Leading) ->maxWidth(Alignment::leading()); ViewPtr button = ZStack({ProgressSpinner(20)->opacity(isLoading_ ? 1.0f : 0.0f), Button(Text(AuthSpec::kSignUpTitle)->bold()->maxWidth(), [this] { signUp(); }, ButtonStyles::accentGradient()) ->disabled(!canSubmit())->opacity(isLoading_ ? 0.0f : 1.0f)}); ViewPtr content = VStack({fields, dob, terms, button}, 26)->padding(16); return ScrollView(content->maxWidth(Alignment::top())); } } // namespace dp