#include "screens/auth/LoginScreen.hpp" #include "screens/AppContext.hpp" #include "screens/Routes.hpp" #include "screens/components/AlertPresenter.hpp" #include "screens/components/Box.hpp" #include "spec/AuthSpec.hpp" #include "store/AppState.hpp" #include "store/SessionStore.hpp" #include "ui/Overlays.hpp" #include "ui/Palette.hpp" namespace dp { using namespace ui; using spec::AuthSpec; LoginScreen::LoginScreen() { SessionStore& s = SessionStore::instance(); if (s.email()) email_ = *s.email(); if (s.password()) password_ = *s.password(); } std::string LoginScreen::title() const { return AuthSpec::kSignInTitle; } ViewPtr LoginScreen::toolbarLeading() { return ToolbarIconButton("xmark", [this] { Overlays::instance().dismissSheetContaining(this); }); } void LoginScreen::submit() { if (canSubmit() && !isLoading_) logIn(); } void LoginScreen::logIn() { // INV-shell 12: credentials are stored BEFORE the request, like the Swift view model. SessionStore::instance().setEmail(email_); SessionStore::instance().setPassword(password_); isLoading_ = true; auto alive = alive_; DevPlaceApi& api = AppContext::api(); api.logIn(email_, password_, [this, alive, &api](Result r) { if (!*alive) return; isLoading_ = false; if (!r.ok()) { AlertPresenter::showError(r.error()); return; } AppState::instance().loadUnreadCounts(api); Overlays::instance().dismissSheetContaining(this); }); } ViewPtr LoginScreen::body() { TextInputOptions emailOpts; emailOpts.placeholder = AuthSpec::kEmail; emailOpts.handle = emailHandle_; emailOpts.onSubmit = [this] { passwordHandle_->focus(); }; TextInputOptions passOpts; passOpts.placeholder = AuthSpec::kPassword; passOpts.secure = true; passOpts.handle = passwordHandle_; passOpts.onSubmit = [this] { submit(); }; ViewPtr fields = VStack({DevPlaceField(TextField(Binding{[this] { return email_; }, [this](const std::string& v) { email_ = v; }}, emailOpts)), DevPlaceField(TextField(Binding{[this] { return password_; }, [this](const std::string& v) { password_ = v; }}, passOpts))}, 10) ->disabled(isLoading_); ViewPtr button = ZStack({ProgressSpinner(20)->opacity(isLoading_ ? 1.0f : 0.0f), Button(Text(AuthSpec::kSignInTitle)->bold()->maxWidth(), [this] { submit(); }, ButtonStyles::accentGradient()) ->disabled(!canSubmit())->opacity(isLoading_ ? 0.0f : 1.0f)}); ViewPtr forgot = RichText({StyledSpan{AuthSpec::kForgot, {}, Palette::accent(), false, false, {}, AuthSpec::kForgotPasswordUrl}}); Navigator* nav = navigator(); ViewPtr createLink = Button(Text(AuthSpec::kCreateLink)->bold(), [nav] { if (nav) nav->push(makeSignUpScreen()); }, ButtonStyles::automatic()); ViewPtr noAccount = Text(AuthSpec::kNoAccount)->weight(Weight::Medium)->foreground(Palette::fg2()); ViewPtr create = ViewThatFits({HStack({noAccount, createLink}, 8), VStack({Text(AuthSpec::kNoAccount)->weight(Weight::Medium)->foreground(Palette::fg2()), Button(Text(AuthSpec::kCreateLink)->bold(), [nav] { if (nav) nav->push(makeSignUpScreen()); }, ButtonStyles::automatic())}, 2)}); ViewPtr content = VStack({fields, button, forgot, create->maxWidth()}, 26)->padding(16); return ScrollView(content->maxWidth(Alignment::top())); } } // namespace dp