#include "screens/feed/FeedScreen.hpp"
#include "core/Timer.hpp"
#include "screens/AppContext.hpp"
#include "screens/Routes.hpp"
#include "screens/components/AlertPresenter.hpp"
#include "screens/feed/FeedPostView.hpp"
#include "screens/feed/OnAppearView.hpp"
#include "screens/post/Accessible.hpp"
#include "screens/post/PostDetailAdapter.hpp"
#include "spec/PostsSpec.hpp"
#include "store/AppState.hpp"
#include "ui/Overlays.hpp"
#include "ui/Palette.hpp"
namespace dp {
using namespace ui;
using spec::PostsLayout;
using spec::PostsTiming;
FeedScreen::FeedScreen(bool loadOnAppear) : loadOnAppear_(loadOnAppear) { scroll_.watchAnchor(CommentEditor::scrollAnchorName()); }
FeedScreen::~FeedScreen() { *alive_ = false; }
std::string FeedScreen::title() const { return spec::PostsText::kFeedTitle; }
bool FeedScreen::busy() const { return loading_ || refreshing_ || loadingMore_ || pendingPublishes_ > 0 || scroll_.animating(); }
void FeedScreen::onAppear() {
if (!didAppear_ && loadOnAppear_ && !AppState::instance().feed()) load();
didAppear_ = true;
}
void FeedScreen::load() {
if (loading_) return;
loading_ = true;
auto alive = alive_;
AppState::instance().loadFeed(AppContext::api(), [this, alive](Result<Unit> r) {
if (!*alive) return;
loading_ = false;
if (!r.ok()) AlertPresenter::showError(r.error());
});
}
void FeedScreen::refresh() {
if (refreshing_) return;
refreshing_ = true;
auto alive = alive_;
AppContext::api().feed([this, alive](Result<Feed> r) {
if (*alive) refreshing_ = false;
if (!r.ok()) {
AlertPresenter::showError(r.error());
return;
}
if (*alive) {
stalled_ = false;
++pendingPublishes_;
}
Feed next = std::move(r.value());
Timer::after(PostsTiming::kFeedPublishDelayMs, [this, alive, next = std::move(next)]() mutable {
if (*alive && pendingPublishes_ > 0) --pendingPublishes_;
AppState::instance().setFeed(std::move(next));
});
});
}
void FeedScreen::loadMore() {
if (loadingMore_) return;
const std::size_t before = AppState::instance().feed() ? AppState::instance().feed()->posts.size() : 0;
loadingMore_ = true;
auto alive = alive_;
AppState::instance().loadMoreFeed(AppContext::api(), [this, alive, before](Result<Unit> r) {
if (!*alive) return;
loadingMore_ = false;
if (!r.ok()) {
stalled_ = true;
AlertPresenter::showError(r.error());
return;
}
const std::size_t after = AppState::instance().feed() ? AppState::instance().feed()->posts.size() : 0;
stalled_ = after <= before;
});
}
void FeedScreen::onReselect() {
if (scroll_.atTop()) refresh();
else scroll_.animateTo(0, PostsTiming::kScrollAnimationSeconds);
}
void FeedScreen::openPost(const std::string& slug, const std::optional<std::string>& scrollToCommentId) {
if (!navigator()) return;
navigator()->push(makePostScreen(PostDestination{slug, scrollToCommentId}));
}
void FeedScreen::openEditor(const Post& post) {
Overlays::instance().sheet(makeCreatePostScreen(PostDetailAdapter::fromPost(post)), SheetStyle::FullScreen, [] {
// onComplete: reload the feed after the edit screen closes (FeedPostView.handleReloadFeed).
AppState::instance().loadFeed(AppContext::api(), [](Result<Unit>) {});
});
}
void FeedScreen::presentCreate() { Overlays::instance().sheet(makeCreatePostScreen(), SheetStyle::FullScreen); }
ViewPtr FeedScreen::newPostButton() {
ViewPtr icon = Icon("plus", PostsLayout::kFabIconSize);
return Accessible(Button(icon, [this] { presentCreate(); }, ButtonStyles::accentGradient(true))->id(spec::PostsText::kNewPost),
spec::PostsText::kNewPost)->padding(PostsLayout::kFabPadding);
}
// A change of the mention list height re-scrolls the composer anchor to the bottom (FeedView.swift:117-122).
void FeedScreen::followMentionList() {
// The anchor positions come from the previous frame, so the scroll runs one frame after the height changed.
if (followPending_) {
followPending_ = false;
if (auto y = scroll_.offsetForBottom(CommentEditor::scrollAnchorName())) scroll_.jumpTo(*y);
}
const float height = session_.isComposing() ? session_.mentionListHeight() : 0.0f;
if (height != lastMentionHeight_) {
lastMentionHeight_ = height;
followPending_ = true;
UiState::instance().animating = true;
}
}
ViewPtr FeedScreen::content() {
const std::optional<Feed>& feed = AppState::instance().feed();
FeedPostContext ctx;
ctx.session = &session_;
ctx.composerAnchorKey = scroll_.qualified(CommentEditor::scrollAnchorName());
ctx.onSelect = [this](const std::string& slug, const std::optional<std::string>& scrollTo) { openPost(slug, scrollTo); };
ctx.onEdit = [this](const Post& post) { openEditor(post); };
ctx.env.openProfile = [this](const std::string& username) {
if (navigator()) navigator()->push(makeProfileScreen(username));
};
ctx.env.quote.active = session_.isComposing();
ctx.env.quote.insert = [this](const std::string& quote) { session_.insertQuote(quote); };
std::vector<ViewPtr> rows;
if (feed) {
std::size_t count = 0;
for (const Post& post : feed->posts) {
if (++count > spec::PostsLimits::kMaxFeedCardsRendered) break;
rows.push_back(FeedPostView(post, ctx));
}
if (feed->nextCursor) {
ViewPtr spinner = ProgressSpinner(20)->padding(PostsLayout::kFeedLoadMorePadding)->maxWidth();
rows.push_back(OnAppear(spinner, "feed-load-more", [this] {
if (!stalled_ && !loadingMore_) loadMore();
}));
}
}
return VStack(rows, PostsLayout::kCardSpacing, HAlign::Center)->maxWidth();
}
ViewPtr FeedScreen::body() {
session_.beginFrame();
scroll_.tick();
followMentionList();
ScrollOptions options;
options.scrollKey = scroll_.scrollKey();
options.refreshing = refreshing_;
options.onRefresh = [this] { refresh(); };
ViewPtr list = scroll_.markViewport(ScrollView(scroll_.markContent(ContentColumn(content())), options)->maxSize(Alignment::topLeading()));
ViewPtr result = list->maxSize(Alignment::topLeading());
// overlay(alignment: .bottomTrailing): a ZStack layer so the button stays in the accessibility tree
if (!session_.isComposing()) result = ZStack({result, newPostButton()->maxSize(Alignment::bottomTrailing())}, Alignment::topLeading());
session_.endFrame();
return result->foreground(Palette::fg1());
}
} // namespace dp