improved code consistency in feed item loading/killing

This commit is contained in:
csoler 2025-10-27 14:48:30 +01:00
parent 516ccdf4bd
commit c3fb087f92
12 changed files with 119 additions and 58 deletions

View file

@ -78,7 +78,8 @@ BaseBoardsCommentsItem::BaseBoardsCommentsItem( FeedHolder *feedHolder, uint32_t
BaseBoardsCommentsItem::~BaseBoardsCommentsItem()
{
auto timeout = std::chrono::steady_clock::now() + std::chrono::milliseconds(200);
auto timeout = std::chrono::steady_clock::now() + std::chrono::milliseconds(GROUP_ITEM_LOADING_TIMEOUT_ms);
while( (mIsLoadingGroup || mIsLoadingMessage || mIsLoadingComment)
&& std::chrono::steady_clock::now() < timeout)
{
@ -126,6 +127,7 @@ bool BaseBoardsCommentsItem::setPost(const RsPostedPost &post, bool doFill)
void BaseBoardsCommentsItem::loadGroup()
{
mIsLoadingGroup = true;
RsThread::async([this]()
{
// 1 - get group data

View file

@ -102,7 +102,7 @@ void ChannelsCommentsItem::paintEvent(QPaintEvent *e)
ChannelsCommentsItem::~ChannelsCommentsItem()
{
auto timeout = std::chrono::steady_clock::now() + std::chrono::milliseconds(300);
auto timeout = std::chrono::steady_clock::now() + std::chrono::milliseconds(GROUP_ITEM_LOADING_TIMEOUT_ms);
while( mLoading && std::chrono::steady_clock::now() < timeout )
{
@ -293,12 +293,14 @@ void ChannelsCommentsItem::load()
if(!rsGxsChannels->getChannelsInfo(groupIds,groups)) // would be better to call channel Summaries for a single group
{
RsErr() << "GxsGxsChannelGroupItem::loadGroup() ERROR getting data" << std::endl;
mLoading= false;
return;
}
if (groups.size() != 1)
{
std::cerr << "GxsGxsChannelGroupItem::loadGroup() Wrong number of Items" << std::endl;
mLoading= false;
return;
}
RsGxsChannelGroup group(groups[0]);
@ -312,7 +314,8 @@ void ChannelsCommentsItem::load()
if(! rsGxsChannels->getChannelContent( groupId(), std::set<RsGxsMessageId>( { messageId(),mThreadId } ),posts,comments,votes))
{
RsErr() << "GxsGxsChannelGroupItem::loadGroup() ERROR getting data" << std::endl;
return;
mLoading= false;
return;
}
// now that everything is in place, update the UI
@ -325,33 +328,31 @@ void ChannelsCommentsItem::load()
mGroupMeta = group.mMeta;
if(comments.size()==1)
{
RsGxsComment cmt(comments[0]);
uint32_t autorized_lines = (int)floor( (ui->avatarLabel->height() - ui->button_HL->sizeHint().height())
/ QFontMetricsF(ui->subjectLabel->font()).height());
ui->commLabel->setText(RsHtml().formatText(NULL, RsStringUtil::CopyLines(QString::fromUtf8(cmt.mComment.c_str()), autorized_lines), RSHTML_FORMATTEXT_EMBED_LINKS));
ui->nameLabel->setId(cmt.mMeta.mAuthorId);
ui->datetimeLabel->setText(DateTime::formatLongDateTime(cmt.mMeta.mPublishTs));
RsIdentityDetails idDetails ;
rsIdentity->getIdDetails(cmt.mMeta.mAuthorId,idDetails);
QPixmap pixmap ;
if(idDetails.mAvatar.mSize == 0 || !GxsIdDetails::loadPixmapFromData(idDetails.mAvatar.mData, idDetails.mAvatar.mSize, pixmap,GxsIdDetails::SMALL))
pixmap = GxsIdDetails::makeDefaultIcon(cmt.mMeta.mAuthorId,GxsIdDetails::LARGE);
ui->avatarLabel->setPixmap(pixmap);
//Change this item to be uploaded with thread element. This is really bad practice.
}
else
if(comments.size()!=1)
{
mLoading=false;
removeItem();
}
RsGxsComment cmt(comments[0]);
uint32_t autorized_lines = (int)floor( (ui->avatarLabel->height() - ui->button_HL->sizeHint().height())
/ QFontMetricsF(ui->subjectLabel->font()).height());
ui->commLabel->setText(RsHtml().formatText(NULL, RsStringUtil::CopyLines(QString::fromUtf8(cmt.mComment.c_str()), autorized_lines), RSHTML_FORMATTEXT_EMBED_LINKS));
ui->nameLabel->setId(cmt.mMeta.mAuthorId);
ui->datetimeLabel->setText(DateTime::formatLongDateTime(cmt.mMeta.mPublishTs));
RsIdentityDetails idDetails ;
rsIdentity->getIdDetails(cmt.mMeta.mAuthorId,idDetails);
QPixmap pixmap ;
if(idDetails.mAvatar.mSize == 0 || !GxsIdDetails::loadPixmapFromData(idDetails.mAvatar.mData, idDetails.mAvatar.mSize, pixmap,GxsIdDetails::SMALL))
pixmap = GxsIdDetails::makeDefaultIcon(cmt.mMeta.mAuthorId,GxsIdDetails::LARGE);
ui->avatarLabel->setPixmap(pixmap);
//Change this item to be uploaded with thread element. This is really bad practice.
if (posts.size() == 1)
setPost(posts[0]);
else

View file

@ -36,7 +36,8 @@
GxsChannelGroupItem::GxsChannelGroupItem(FeedHolder *feedHolder, uint32_t feedId, const RsGxsGroupId &groupId, bool isHome, bool autoUpdate) :
GxsGroupFeedItem(feedHolder, feedId, groupId, isHome, rsGxsChannels, autoUpdate)
{
setup();
mIsLoading = false;
setup();
requestGroup();
addEventHandler();
}
@ -44,6 +45,7 @@ GxsChannelGroupItem::GxsChannelGroupItem(FeedHolder *feedHolder, uint32_t feedId
GxsChannelGroupItem::GxsChannelGroupItem(FeedHolder *feedHolder, uint32_t feedId, const RsGxsChannelGroup &group, bool isHome, bool autoUpdate) :
GxsGroupFeedItem(feedHolder, feedId, group.mMeta.mGroupId, isHome, rsGxsChannels, autoUpdate)
{
mIsLoading = false;
setup();
setGroup(group);
addEventHandler();
@ -77,6 +79,14 @@ void GxsChannelGroupItem::addEventHandler()
GxsChannelGroupItem::~GxsChannelGroupItem()
{
auto timeout = std::chrono::steady_clock::now() + std::chrono::milliseconds(GROUP_ITEM_LOADING_TIMEOUT_ms);
while( mIsLoading && std::chrono::steady_clock::now() < timeout )
{
RsDbg() << __PRETTY_FUNCTION__ << " is Waiting for data to load " << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
rsEvents->unregisterEventsHandler(mEventHandlerId);
delete(ui);
}
@ -121,6 +131,8 @@ bool GxsChannelGroupItem::setGroup(const RsGxsChannelGroup &group)
void GxsChannelGroupItem::loadGroup()
{
mIsLoading = true;
RsThread::async([this]()
{
// 1 - get group data
@ -131,14 +143,16 @@ void GxsChannelGroupItem::loadGroup()
if(!rsGxsChannels->getChannelsInfo(groupIds,groups))
{
RsErr() << "PostedItem::loadGroup() ERROR getting data" << std::endl;
return;
mIsLoading = false;
return;
}
if (groups.size() != 1)
{
std::cerr << "GxsGxsChannelGroupItem::loadGroup() Wrong number of Items";
std::cerr << std::endl;
return;
mIsLoading = false;
return;
}
RsGxsChannelGroup group(groups[0]);
@ -149,6 +163,7 @@ void GxsChannelGroupItem::loadGroup()
* after a blocking call to RetroShare API complete */
setGroup(group);
mIsLoading = false;
}, this );
});

View file

@ -62,6 +62,8 @@ private:
void setup();
void addEventHandler();
bool mIsLoading;
private:
RsGxsChannelGroup mGroup;

View file

@ -140,7 +140,7 @@ void GxsChannelPostItem::paintEvent(QPaintEvent *e)
GxsChannelPostItem::~GxsChannelPostItem()
{
auto timeout = std::chrono::steady_clock::now() + std::chrono::milliseconds(300);
auto timeout = std::chrono::steady_clock::now() + std::chrono::milliseconds(GROUP_ITEM_LOADING_TIMEOUT_ms);
while( (mLoadingGroup || mLoadingMessage || mLoadingComment)
&& std::chrono::steady_clock::now() < timeout)
@ -306,14 +306,16 @@ void GxsChannelPostItem::loadGroup()
if(!rsGxsChannels->getChannelsInfo(groupIds,groups)) // would be better to call channel Summaries for a single group
{
RsErr() << "GxsGxsChannelGroupItem::loadGroup() ERROR getting data" << std::endl;
return;
mLoadingGroup = false;
return;
}
if (groups.size() != 1)
{
std::cerr << "GxsGxsChannelGroupItem::loadGroup() Wrong number of Items";
std::cerr << std::endl;
return;
mLoadingGroup = false;
return;
}
RsGxsChannelGroup group(groups[0]);
@ -348,7 +350,8 @@ void GxsChannelPostItem::loadMessage()
if(! rsGxsChannels->getChannelContent( groupId(), std::set<RsGxsMessageId>( { messageId() } ),posts,comments,votes))
{
RsErr() << "GxsGxsChannelGroupItem::loadGroup() ERROR getting data" << std::endl;
return;
mLoadingMessage = false;
return;
}
if (posts.size() == 1)
@ -377,11 +380,11 @@ void GxsChannelPostItem::loadMessage()
ui->commLabel->show();
ui->commLabel->setText(QString::fromUtf8(cmt.mComment.c_str()));
//Change this item to be uploaded with thread element.
// Change this item to be uploaded with thread element. Note: this is terrible coding.
setMessageId(cmt.mMeta.mThreadId);
requestMessage();
mLoadingMessage = false;
requestMessage();
}, this );
}
@ -424,7 +427,8 @@ void GxsChannelPostItem::loadComment()
if(! rsGxsChannels->getChannelComments( groupId(),msgIds,comments))
{
RsErr() << "GxsGxsChannelGroupItem::loadGroup() ERROR getting data" << std::endl;
return;
mLoadingComment = false;
return;
}
int comNb = comments.size();
@ -446,13 +450,6 @@ void GxsChannelPostItem::loadComment()
void GxsChannelPostItem::fill()
{
/* fill in */
// if (isLoading()) {
// /* Wait for all requests */
//return;
// }
#ifdef DEBUG_ITEM
std::cerr << "GxsChannelPostItem::fill()";
std::cerr << std::endl;

View file

@ -34,7 +34,8 @@
GxsForumGroupItem::GxsForumGroupItem(FeedHolder *feedHolder, uint32_t feedId, const RsGxsGroupId &groupId, bool isHome, bool autoUpdate) :
GxsGroupFeedItem(feedHolder, feedId, groupId, isHome, rsGxsForums, autoUpdate)
{
setup();
mIsLoading = false;
setup();
requestGroup();
addEventHandler();
}
@ -44,7 +45,8 @@ GxsForumGroupItem::GxsForumGroupItem(FeedHolder *feedHolder, uint32_t feedId, co
mAddedModerators(added_moderators),
mRemovedModerators(removed_moderators)
{
setup();
mIsLoading = false;
setup();
requestGroup();
addEventHandler();
}
@ -85,6 +87,14 @@ GxsForumGroupItem::GxsForumGroupItem(FeedHolder *feedHolder, uint32_t feedId, co
GxsForumGroupItem::~GxsForumGroupItem()
{
auto timeout = std::chrono::steady_clock::now() + std::chrono::milliseconds(GROUP_ITEM_LOADING_TIMEOUT_ms);
while( mIsLoading && std::chrono::steady_clock::now() < timeout )
{
RsDbg() << __PRETTY_FUNCTION__ << " is Waiting for data to load " << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
rsEvents->unregisterEventsHandler(mEventHandlerId);
delete(ui);
}
@ -129,6 +139,8 @@ bool GxsForumGroupItem::setGroup(const RsGxsForumGroup &group)
void GxsForumGroupItem::loadGroup()
{
mIsLoading = true;
RsThread::async([this]()
{
// 1 - get group data
@ -143,14 +155,16 @@ void GxsForumGroupItem::loadGroup()
if(!rsGxsForums->getForumsInfo(forumIds,groups))
{
RsErr() << "GxsForumGroupItem::loadGroup() ERROR getting data" << std::endl;
return;
mIsLoading = false;
return;
}
if (groups.size() != 1)
{
std::cerr << "GxsForumGroupItem::loadGroup() Wrong number of Items";
std::cerr << std::endl;
return;
mIsLoading = false;
return;
}
RsGxsForumGroup group(groups[0]);// no reference to teporary accross threads!
@ -161,6 +175,7 @@ void GxsForumGroupItem::loadGroup()
* after a blocking call to RetroShare API complete */
setGroup(group);
mIsLoading = false;
}, this );
});

View file

@ -69,6 +69,8 @@ private:
/** Qt Designer generated object */
Ui::GxsForumGroupItem *ui;
bool mIsLoading;
std::list<RsGxsId> mAddedModerators;
std::list<RsGxsId> mRemovedModerators;

View file

@ -89,7 +89,7 @@ GxsForumMsgItem::GxsForumMsgItem(FeedHolder *feedHolder, uint32_t feedId, const
GxsForumMsgItem::~GxsForumMsgItem()
{
auto timeout = std::chrono::steady_clock::now() + std::chrono::milliseconds(300);
auto timeout = std::chrono::steady_clock::now() + std::chrono::milliseconds(GROUP_ITEM_LOADING_TIMEOUT_ms);
while( (mLoadingGroup || mLoadingMessage || mLoadingSetAsRead || mLoadingParentMessage)
&& std::chrono::steady_clock::now() < timeout)
@ -202,14 +202,16 @@ void GxsForumMsgItem::loadGroup()
if(!rsGxsForums->getForumsInfo(forumIds,groups))
{
RsErr() << "GxsForumGroupItem::loadGroup() ERROR getting data" << std::endl;
return;
mLoadingGroup = false;
return;
}
if (groups.size() != 1)
{
std::cerr << "GxsForumGroupItem::loadGroup() Wrong number of Items";
std::cerr << std::endl;
return;
mLoadingGroup = false;
return;
}
RsGxsForumGroup group(groups[0]);
@ -249,14 +251,16 @@ void GxsForumMsgItem::loadMessage()
{
std::cerr << "GxsForumMsgItem::loadMessage() ERROR getting data";
std::cerr << std::endl;
return;
mLoadingMessage = false;
return;
}
if (msgs.size() != 1)
{
std::cerr << "GxsForumMsgItem::loadMessage() Wrong number of Items";
std::cerr << std::endl;
return;
mLoadingMessage = false;
return;
}
RsGxsForumMsg msg(msgs[0]);
@ -296,14 +300,16 @@ void GxsForumMsgItem::loadParentMessage(const RsGxsMessageId& parent_msg)
{
std::cerr << "GxsForumMsgItem::loadMessage() ERROR getting data";
std::cerr << std::endl;
return;
mLoadingParentMessage = false;
return;
}
if (msgs.size() != 1)
{
std::cerr << "GxsForumMsgItem::loadMessage() Wrong number of Items";
std::cerr << std::endl;
return;
mLoadingParentMessage = false;
return;
}
RsGxsForumMsg msg(msgs[0]);

View file

@ -35,7 +35,8 @@
PostedGroupItem::PostedGroupItem(FeedHolder *feedHolder, uint32_t feedId, const RsGxsGroupId &groupId, bool isHome, bool autoUpdate) :
GxsGroupFeedItem(feedHolder, feedId, groupId, isHome, rsPosted, autoUpdate)
{
setup();
mIsLoadingGroup = false;
setup();
requestGroup();
}
@ -43,13 +44,24 @@ PostedGroupItem::PostedGroupItem(FeedHolder *feedHolder, uint32_t feedId, const
PostedGroupItem::PostedGroupItem(FeedHolder *feedHolder, uint32_t feedId, const RsPostedGroup &group, bool isHome, bool autoUpdate) :
GxsGroupFeedItem(feedHolder, feedId, group.mMeta.mGroupId, isHome, rsPosted, autoUpdate)
{
setup();
mIsLoadingGroup = false;
setup();
setGroup(group);
}
PostedGroupItem::~PostedGroupItem()
{
auto timeout = std::chrono::steady_clock::now() + std::chrono::milliseconds(GROUP_ITEM_LOADING_TIMEOUT_ms);
while( mIsLoadingGroup && std::chrono::steady_clock::now() < timeout)
{
RsDbg() << __PRETTY_FUNCTION__ << " is Waiting "
<< (mIsLoadingGroup ? "Group " : "")
<< "loading finished." << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
delete(ui);
}
@ -95,6 +107,8 @@ bool PostedGroupItem::setGroup(const RsPostedGroup &group)
void PostedGroupItem::loadGroup()
{
mIsLoadingGroup = true;
RsThread::async([this]()
{
// 1 - get group data
@ -109,14 +123,16 @@ void PostedGroupItem::loadGroup()
if(!rsPosted->getBoardsInfo(groupIds,groups))
{
RsErr() << "GxsPostedGroupItem::loadGroup() ERROR getting data" << std::endl;
return;
mIsLoadingGroup = false;
return;
}
if (groups.size() != 1)
{
std::cerr << "GxsPostedGroupItem::loadGroup() Wrong number of Items";
std::cerr << std::endl;
return;
mIsLoadingGroup = false;
return;
}
RsPostedGroup group(groups[0]);
@ -127,6 +143,7 @@ void PostedGroupItem::loadGroup()
* after a blocking call to RetroShare API complete */
setGroup(group);
mIsLoadingGroup = false;
}, this );
});

View file

@ -63,6 +63,7 @@ private:
private:
RsPostedGroup mGroup;
bool mIsLoadingGroup;
/** Qt Designer generated object */
Ui::PostedGroupItem *ui;

View file

@ -31,6 +31,8 @@
* #define DEBUG_ITEM 1
**/
const uint GxsGroupFeedItem::GROUP_ITEM_LOADING_TIMEOUT_ms = 2000;
GxsGroupFeedItem::GxsGroupFeedItem(FeedHolder *feedHolder, uint32_t feedId, const RsGxsGroupId &groupId, bool isHome, RsGxsIfaceHelper *iface, bool /*autoUpdate*/) :
FeedItem(feedHolder,feedId,NULL)
{

View file

@ -60,6 +60,7 @@ protected slots:
protected:
bool mIsHome;
RsGxsIfaceHelper *mGxsIface;
static const uint GROUP_ITEM_LOADING_TIMEOUT_ms ;
private slots:
/* RsGxsUpdateBroadcastBase */