#include "screens/notifications/NotificationsScreen.hpp"
#include "screens/AppContext.hpp"
#include "screens/Router.hpp"
#include "screens/components/Box.hpp"
#include "screens/components/NotificationRow.hpp"
#include "screens/messages/ContentUnavailable.hpp"
#include "screens/notifications/OnAppearView.hpp"
#include "spec/Layout.hpp"
#include "spec/SocialSpec.hpp"
#include "store/AppState.hpp"
#include "ui/Palette.hpp"
namespace dp {
using namespace ui;
using S = spec::SocialStrings;
using M = spec::SocialMetrics;
NotificationsScreen::NotificationsScreen() : model_(AppContext::api()) {
wasLoggedIn_ = AppState::instance().isLoggedIn();
observerId_ = AppState::instance().subscribe([this, alive = alive_] {
if (!*alive) return;
const bool now = AppState::instance().isLoggedIn();
if (now == wasLoggedIn_) return;
wasLoggedIn_ = now;
model_.load(); // onChange(appState.isLoggedIn)
});
focus_ = std::make_unique<WindowFocusWatcher>([this, alive = alive_](bool active) {
if (*alive && active) model_.refresh(); // onChange(scenePhase == .active)
});
}
NotificationsScreen::~NotificationsScreen() {
*alive_ = false;
if (observerId_) AppState::instance().unsubscribe(observerId_);
}
std::string NotificationsScreen::title() const { return S::kNotificationsTitle; }
void NotificationsScreen::onAppear() { model_.load(); }
void NotificationsScreen::onReselect() {
if (isAtTop_) model_.refresh();
}
ViewPtr NotificationsScreen::toolbarTrailing() {
if (!AppState::instance().isLoggedIn()) return nullptr;
return ToolbarTextButton(S::kMarkAllRead, [this, alive = alive_] { if (*alive) model_.markAllRead(); });
}
void NotificationsScreen::perform(const NotificationRoute& route) {
Router& router = Router::instance();
switch (route.kind) {
case NotificationRoute::Kind::Post: router.openPost(route.post); break;
case NotificationRoute::Kind::Conversation: router.openConversation(route.user); break;
case NotificationRoute::Kind::Web: router.openUrl(route.url); break;
case NotificationRoute::Kind::OwnProfile: router.openOwnProfile(); break;
}
}
ViewPtr NotificationsScreen::emptyState() {
if (!AppState::instance().isLoggedIn()) {
return ContentUnavailable::view(S::kNotificationsIcon, S::kSignInRequired, S::kNotificationsSignInHint);
}
if (!model_.notifications()) return ProgressSpinner(20);
return ContentUnavailable::view(S::kNotificationsIcon, S::kNoNotifications, S::kNoNotificationsHint);
}
ViewPtr NotificationsScreen::groupSection(const NotificationGroup& group) {
std::vector<ViewPtr> rows;
for (std::size_t i = 0; i < group.entries.size(); ++i) {
const Notification n = group.entries[i];
rows.push_back(NotificationRow(n, [this, alive = alive_, n] {
if (!*alive) return;
model_.open(n, [alive](std::optional<NotificationRoute> route) {
if (*alive && route) perform(*route);
});
}));
if (i + 1 < group.entries.size()) rows.push_back(Divider(Palette::separator()));
}
BoxStyle style;
style.background = Palette::bg1();
style.padding = BoxStyle::Pad::None;
return VStack({Text(group.label)->font(FontSpec::headline())->foreground(Palette::fg2())->maxWidth(Alignment::leading()),
BoxView(VStack(rows, 0)->clipRounded(M::kCardRadius), style)},
M::kGroupLabelSpacing, HAlign::Leading);
}
ViewPtr NotificationsScreen::content() {
if (model_.groups().empty()) return ContentUnavailable::listState(emptyState());
std::vector<ViewPtr> sections;
for (const auto& g : model_.groups()) sections.push_back(groupSection(g));
if (model_.hasMore()) {
sections.push_back(OnAppearView(ProgressSpinner(20)->padding(M::kFooterPadding)->maxWidth(),
[this, alive = alive_] { if (*alive) model_.loadMore(); })
->id("notifications-footer"));
}
return VStack(sections, M::kGroupSpacing, HAlign::Leading)->padding(M::kListPadding);
}
ViewPtr NotificationsScreen::body() {
ScrollOptions so;
so.onRefresh = [this, alive = alive_] { if (*alive) model_.refresh(); };
so.refreshing = model_.isRefreshing();
so.onGeometry = [this, alive = alive_](float offset, float, float) {
if (*alive) isAtTop_ = offset <= 0;
};
return ScrollView(VStack({NavLargeTitle(title()), content()}, 0, HAlign::Center), so);
}
} // namespace dp