#include "screens/messages/MessagesScreen.hpp"
#include "screens/AppContext.hpp"
#include "screens/Routes.hpp"
#include "screens/components/Box.hpp"
#include "screens/components/ConversationRow.hpp"
#include "screens/messages/ContentUnavailable.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;
MessagesScreen::MessagesScreen() : 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)
});
}
MessagesScreen::~MessagesScreen() {
*alive_ = false;
if (observerId_) AppState::instance().unsubscribe(observerId_);
}
std::string MessagesScreen::title() const { return S::kMessagesTitle; }
void MessagesScreen::onAppear() { model_.load(); }
ViewPtr MessagesScreen::toolbarTrailing() {
if (model_.isReloading()) return ProgressSpinner(20)->frame(spec::Layout::kNavButtonSize, spec::Layout::kNavButtonSize);
return ToolbarIconButton("arrow.clockwise", [this, alive = alive_] { if (*alive) model_.reload(); });
}
ViewPtr MessagesScreen::emptyState() {
if (!AppState::instance().isLoggedIn()) {
return ContentUnavailable::view(S::kMessagesIcon, S::kSignInRequired, S::kMessagesSignInHint);
}
if (!model_.inbox()) return ProgressSpinner(20);
return ContentUnavailable::view(S::kMessagesIcon, S::kNoConversations, S::kNoConversationsHint);
}
ViewPtr MessagesScreen::list() {
const auto& rows = model_.conversations();
std::vector<ViewPtr> stack;
for (std::size_t i = 0; i < rows.size(); ++i) {
const User other = rows[i].otherUser;
stack.push_back(ConversationRow(rows[i], [this, alive = alive_, other] {
if (*alive && navigator()) navigator()->push(makeConversationScreen(other));
}));
if (i + 1 < rows.size()) stack.push_back(Divider(Palette::separator()));
}
BoxStyle style;
style.background = Palette::bg1();
style.padding = BoxStyle::Pad::None;
ViewPtr card = BoxView(VStack(stack, 0)->clipRounded(M::kCardRadius), style);
return card->padding(M::kListPadding);
}
ViewPtr MessagesScreen::content() {
ViewPtr inner = model_.conversations().empty() ? ContentUnavailable::listState(emptyState()) : list();
return VStack({NavLargeTitle(title()), inner}, 0, HAlign::Center);
}
ViewPtr MessagesScreen::body() {
ScrollOptions so;
so.onRefresh = [this, alive = alive_] { if (*alive) model_.reload(); };
so.refreshing = model_.isReloading();
return ScrollView(content(), so);
}
} // namespace dp