#include "screens/settings/SettingsScreen.hpp"
#include "core/Log.hpp"
#include "model/AppearanceMode.hpp"
#include "screens/AppContext.hpp"
#include "screens/Router.hpp"
#include "screens/Routes.hpp"
#include "screens/components/AlertPresenter.hpp"
#include "spec/AuthSpec.hpp"
#include "store/AppState.hpp"
#include "store/SessionStore.hpp"
#include "store/SettingsStore.hpp"
#include "ui/Controls.hpp"
#include "ui/Overlays.hpp"
#include "ui/Palette.hpp"
namespace dp {
using namespace ui;
using spec::AuthSpec;
namespace {
ViewPtr labelRow(const std::string& icon, const std::string& title) {
return HStack({Icon(icon, 17)->foreground(Palette::fg1())->frame(24, 24), Text(title)}, 10);
}
} // namespace
std::string SettingsScreen::title() const { return AuthSpec::kSettingsTitle; }
ViewPtr SettingsScreen::toolbarTrailing() {
if (AppState::instance().isLoggedIn()) return nullptr;
return ToolbarTextButton(AuthSpec::kSignInButton, [] { Router::instance().presentLogin(); }, true);
}
void SettingsScreen::askSignOut() {
AlertSpec a;
a.title = AuthSpec::kSignOutTitle;
a.buttons = {{AuthSpec::kCancel, AlertButton::Role::Cancel, nullptr},
{AuthSpec::kSignOut, AlertButton::Role::Destructive, [this, alive = alive_] { if (*alive) logOut(); }}};
Overlays::instance().alert(std::move(a));
}
void SettingsScreen::askDeleteAccount() {
AlertSpec a;
a.title = AuthSpec::kDeleteTitle;
a.buttons = {{AuthSpec::kCancel, AlertButton::Role::Cancel, nullptr},
{AuthSpec::kDeleteAccount, AlertButton::Role::Destructive, [this, alive = alive_] { if (*alive) askPassword(); }}};
Overlays::instance().alert(std::move(a));
}
void SettingsScreen::askPassword() {
AlertSpec a;
a.title = AuthSpec::kDeleteConfirmTitle;
a.message = AuthSpec::kDeleteConfirmMessage;
AlertField f;
f.placeholder = AuthSpec::kPassword;
f.secure = true;
auto text = f.text;
a.fields.push_back(f);
a.buttons = {{AuthSpec::kCancel, AlertButton::Role::Cancel, [text] { text->clear(); }},
{AuthSpec::kDeleteAccount, AlertButton::Role::Destructive, [this, alive = alive_, text] {
const std::string password = *text;
text->clear();
if (*alive) deleteAccount(password);
}}};
Overlays::instance().alert(std::move(a));
}
void SettingsScreen::logOut() {
// INV-shell 14: unregister push (backend no-op), clear stored credentials, clear state, reload the anonymous feed.
SessionStore::instance().setEmail(std::nullopt);
SessionStore::instance().setPassword(std::nullopt);
AppState::instance().clear();
AppState::instance().loadFeed(AppContext::api(), [](Result<Unit> r) {
if (!r.ok()) Log::warn("feed reload after sign out failed: " + r.error().message);
});
}
void SettingsScreen::deleteAccount(const std::string& password) {
isWorking_ = true;
auto alive = alive_;
AppContext::api().deleteAccount(password, [this, alive](Result<Unit> r) {
if (!*alive) return;
isWorking_ = false;
if (!r.ok()) {
AlertPresenter::showError(r.error());
return;
}
logOut();
AlertSpec a;
a.title = AuthSpec::kDeletedTitle;
a.message = AuthSpec::kDeletedMessage;
a.buttons = {{"OK", AlertButton::Role::Cancel, nullptr}};
Overlays::instance().alert(std::move(a));
});
}
ViewPtr SettingsScreen::body() {
AppState& state = AppState::instance();
SettingsStore& settings = SettingsStore::instance();
Navigator* nav = navigator();
std::vector<FormSection> sections;
if (state.isLoggedIn()) {
ViewPtr row = HStack({labelRow("person", AuthSpec::kProfile)->maxWidth(Alignment::leading()),
Icon("chevron.right", 13)->weight(Weight::Semibold)->foreground(Palette::fg2())}, 8);
sections.push_back({"", "", {Button(row, [nav] { if (nav) nav->push(makeProfileScreen(std::nullopt)); }, ButtonStyles::form())}});
}
std::vector<PickerOption> modes;
for (const AppearanceMode& m : AppearanceMode::all()) modes.push_back({m.title(), static_cast<int>(m.kind())});
sections.push_back({"", "", {MenuPicker(labelRow("circle.lefthalf.filled", AuthSpec::kAppearance), modes,
static_cast<int>(settings.appearance().kind()),
[](int tag) { SettingsStore::instance().setAppearance(AppearanceMode(static_cast<AppearanceMode::Value>(tag))); })}});
sections.push_back({AuthSpec::kPostsFeedHeader, "",
{Toggle(labelRow("photo", AuthSpec::kShowAttachments),
BoolBinding{[] { return SettingsStore::instance().showFeedAttachments(); }, [](bool v) { SettingsStore::instance().setShowFeedAttachments(v); }},
Palette::accent()),
Toggle(labelRow("text.bubble", AuthSpec::kShowComments),
BoolBinding{[] { return SettingsStore::instance().showFeedComments(); }, [](bool v) { SettingsStore::instance().setShowFeedComments(v); }},
Palette::accent())}});
sections.push_back({AuthSpec::kViolatingHeader, "",
{Toggle(labelRow("flag", AuthSpec::kShowReportButtons),
BoolBinding{[] { return SettingsStore::instance().showReportButtons(); }, [](bool v) { SettingsStore::instance().setShowReportButtons(v); }},
Palette::accent())}});
if (state.isLoggedIn()) {
sections.push_back({"", "",
{Button(labelRow("iphone.and.arrow.right.outward", AuthSpec::kSignOut), [this] { askSignOut(); }, ButtonStyles::form()),
Button(labelRow("trash", AuthSpec::kDeleteAccount), [this] { askDeleteAccount(); }, ButtonStyles::form())}});
}
return VStack({NavLargeTitle(AuthSpec::kSettingsTitle), Form(std::move(sections))->maxHeight(Alignment::top())}, 0)->maxSize(Alignment::topLeading());
}
} // namespace dp