Skip to content

Commit

Permalink
FeedList: Automatically load next page
Browse files Browse the repository at this point in the history
  • Loading branch information
xfangfang committed May 30, 2024
1 parent 5f20aab commit 2bc9038
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 29 deletions.
2 changes: 1 addition & 1 deletion resources/xml/fragment/inbox_feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
flowMode="true"
paddingLeft="15"
paddingRight="15"
itemHeight="500"
itemHeight="100"
wireframe="false"
id="inbox/feedList" />
</brls:Box>
6 changes: 3 additions & 3 deletions wiliwili/include/fragment/inbox_feed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class InboxFeed : public AttachedView, public InboxFeedRequest {

void setMode(MsgFeedMode mode);

void onFeedReplyList(const bilibili::FeedReplyResultWrapper& result) override;
void onFeedReplyList(const bilibili::FeedReplyResultWrapper& result, bool refresh) override;

void onFeedAtList(const bilibili::FeedAtResultWrapper& result) override;
void onFeedAtList(const bilibili::FeedAtResultWrapper& result, bool refresh) override;

void onFeedLikeList(const bilibili::FeedLikeResultWrapper& result) override;
void onFeedLikeList(const bilibili::FeedLikeResultWrapper& result, bool refresh) override;

void onError(const std::string& error) override;

Expand Down
6 changes: 3 additions & 3 deletions wiliwili/include/presenter/inbox_feed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ enum class MsgFeedMode

class InboxFeedRequest : public Presenter {
public:
virtual void onFeedReplyList(const bilibili::FeedReplyResultWrapper& result);
virtual void onFeedReplyList(const bilibili::FeedReplyResultWrapper& result, bool refresh);

virtual void onFeedAtList(const bilibili::FeedAtResultWrapper& result);
virtual void onFeedAtList(const bilibili::FeedAtResultWrapper& result, bool refresh);

virtual void onFeedLikeList(const bilibili::FeedLikeResultWrapper& result);
virtual void onFeedLikeList(const bilibili::FeedLikeResultWrapper& result, bool refresh);

virtual void onError(const std::string& error);

Expand Down
61 changes: 51 additions & 10 deletions wiliwili/source/fragment/inbox_feed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ class DataSourceFeedList : public RecyclingGridDataSource {
}
}

void appendData(const std::vector<T>& result) {
for (const auto& i : result) {
this->list.push_back(i);
}
}

void clearData() override { this->list.clear(); }

private:
Expand All @@ -122,27 +128,62 @@ InboxFeed::InboxFeed() {

// 消息列表
recyclingGrid->registerCell("Cell", []() { return new FeedCard(); });

recyclingGrid->onNextPage([this](){
this->requestData(feedMode, false);
});

recyclingGrid->setRefreshAction([this](){
this->recyclingGrid->estimatedRowHeight = 100;
this->recyclingGrid->showSkeleton();
this->requestData(feedMode, true);
brls::Application::giveFocus(this->getTabBar());
});
}

InboxFeed::~InboxFeed() { brls::Logger::debug("Fragment InboxFeed: delete"); }

void InboxFeed::onCreate() { this->requestData(feedMode, true); }
void InboxFeed::onCreate() {
this->requestData(feedMode, true);
this->registerTabAction("", brls::ControllerButton::BUTTON_X ,[this](brls::View* view) {
this->recyclingGrid->refresh();
return true;
}, true);
}

void InboxFeed::setMode(MsgFeedMode mode) { this->feedMode = mode; }

void InboxFeed::onFeedReplyList(const bilibili::FeedReplyResultWrapper& result) {
auto dataSource = new DataSourceFeedList(result.items);
recyclingGrid->setDataSource(dataSource);
void InboxFeed::onFeedReplyList(const bilibili::FeedReplyResultWrapper& result, bool refresh) {
this->recyclingGrid->estimatedRowHeight = 500;
auto dataSource = dynamic_cast<DataSourceFeedList<bilibili::FeedReplyResult>*>(recyclingGrid->getDataSource());
if (dataSource && !refresh) {
dataSource->appendData(result.items);
recyclingGrid->notifyDataChanged();
} else {
recyclingGrid->setDataSource(new DataSourceFeedList(result.items));
}
}

void InboxFeed::onFeedAtList(const bilibili::FeedAtResultWrapper& result) {
auto dataSource = new DataSourceFeedList(result.items);
recyclingGrid->setDataSource(dataSource);
void InboxFeed::onFeedAtList(const bilibili::FeedAtResultWrapper& result, bool refresh) {
this->recyclingGrid->estimatedRowHeight = 500;
auto dataSource = dynamic_cast<DataSourceFeedList<bilibili::FeedAtResult>*>(recyclingGrid->getDataSource());
if (dataSource && !refresh) {
dataSource->appendData(result.items);
recyclingGrid->notifyDataChanged();
} else {
recyclingGrid->setDataSource(new DataSourceFeedList(result.items));
}
}

void InboxFeed::onFeedLikeList(const bilibili::FeedLikeResultWrapper& result) {
auto dataSource = new DataSourceFeedList(result.total.items);
recyclingGrid->setDataSource(dataSource);
void InboxFeed::onFeedLikeList(const bilibili::FeedLikeResultWrapper& result, bool refresh) {
this->recyclingGrid->estimatedRowHeight = 500;
auto dataSource = dynamic_cast<DataSourceFeedList<bilibili::FeedLikeResult>*>(recyclingGrid->getDataSource());
if (dataSource && !refresh) {
dataSource->appendData(result.total.items);
recyclingGrid->notifyDataChanged();
} else {
recyclingGrid->setDataSource(new DataSourceFeedList(result.total.items));
}
}

void InboxFeed::onError(const std::string& error) {
Expand Down
29 changes: 17 additions & 12 deletions wiliwili/source/presenter/inbox_feed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@
#include "bilibili.h"
#include "presenter/inbox_feed.hpp"

void InboxFeedRequest::onFeedReplyList(const bilibili::FeedReplyResultWrapper& result) {}
void InboxFeedRequest::onFeedReplyList(const bilibili::FeedReplyResultWrapper& result, bool refresh) {}

void InboxFeedRequest::onFeedAtList(const bilibili::FeedAtResultWrapper& result) {}
void InboxFeedRequest::onFeedAtList(const bilibili::FeedAtResultWrapper& result, bool refresh) {}

void InboxFeedRequest::onFeedLikeList(const bilibili::FeedLikeResultWrapper& result) {}
void InboxFeedRequest::onFeedLikeList(const bilibili::FeedLikeResultWrapper& result, bool refresh) {}

void InboxFeedRequest::onError(const std::string& error) {}

void InboxFeedRequest::requestData(MsgFeedMode mode, bool refresh) {
if (this->cursor.is_end && !refresh) return;
if (refresh) this->cursor = bilibili::MsgFeedCursor{};
switch (mode) {
case MsgFeedMode::REPLY: {
ASYNC_RETAIN
BILI::msg_feed_reply(
this->cursor,
[ASYNC_TOKEN](const bilibili::FeedReplyResultWrapper& result) {
brls::sync([ASYNC_TOKEN, result]() {
[ASYNC_TOKEN, refresh](const bilibili::FeedReplyResultWrapper& result) {
brls::sync([ASYNC_TOKEN, result, refresh]() {
ASYNC_RELEASE
this->onFeedReplyList(result);
this->cursor = result.cursor;
this->onFeedReplyList(result, refresh);
});
},
[ASYNC_TOKEN](BILI_ERR) {
Expand All @@ -35,10 +38,11 @@ void InboxFeedRequest::requestData(MsgFeedMode mode, bool refresh) {
ASYNC_RETAIN
BILI::msg_feed_at(
this->cursor,
[ASYNC_TOKEN](const bilibili::FeedAtResultWrapper& result) {
brls::sync([ASYNC_TOKEN, result]() {
[ASYNC_TOKEN, refresh](const bilibili::FeedAtResultWrapper& result) {
brls::sync([ASYNC_TOKEN, result, refresh]() {
ASYNC_RELEASE
this->onFeedAtList(result);
this->cursor = result.cursor;
this->onFeedAtList(result, refresh);
});
},
[ASYNC_TOKEN](BILI_ERR) {
Expand All @@ -53,10 +57,11 @@ void InboxFeedRequest::requestData(MsgFeedMode mode, bool refresh) {
ASYNC_RETAIN
BILI::msg_feed_like(
this->cursor,
[ASYNC_TOKEN](const bilibili::FeedLikeResultWrapper& result) {
brls::sync([ASYNC_TOKEN, result]() {
[ASYNC_TOKEN, refresh](const bilibili::FeedLikeResultWrapper& result) {
brls::sync([ASYNC_TOKEN, result, refresh]() {
ASYNC_RELEASE
this->onFeedLikeList(result);
this->cursor = result.total.cursor;
this->onFeedLikeList(result, refresh);
});
},
[ASYNC_TOKEN](BILI_ERR) {
Expand Down

0 comments on commit 2bc9038

Please sign in to comment.