fixed bug in updating filtered posts

This commit is contained in:
csoler 2023-03-23 21:13:38 +01:00
parent aba0ffa581
commit 141ee0b7e0
3 changed files with 41 additions and 26 deletions

View File

@ -140,7 +140,31 @@ void RsGxsChannelPostsModel::getFilesList(std::list<ChannelPostFileInfo>& files)
files.push_back(it.second);
}
bool RsGxsChannelPostsModel::postPassesFilter(const RsGxsChannelPost& post,const QStringList& strings,bool only_unread) const
{
bool passes_strings = true;
for(auto& s:strings)
passes_strings = passes_strings && QString::fromStdString(post.mMeta.mMsgName).contains(s,Qt::CaseInsensitive);
if(strings.empty())
passes_strings = true;
if(passes_strings && (!only_unread || (IS_MSG_UNREAD(post.mMeta.mMsgStatus) || IS_MSG_NEW(post.mMeta.mMsgStatus))))
return true;
return false;
}
void RsGxsChannelPostsModel::setFilter(const QStringList& strings,bool only_unread, uint32_t& count)
{
mFilteredStrings = strings;
mFilterUnread = only_unread;
updateFilter(count);
}
void RsGxsChannelPostsModel::updateFilter(uint32_t& count)
{
preMods();
@ -151,18 +175,8 @@ void RsGxsChannelPostsModel::setFilter(const QStringList& strings,bool only_unre
endResetModel();
for(size_t i=0;i<mPosts.size();++i)
{
bool passes_strings = true;
for(auto& s:strings)
passes_strings = passes_strings && QString::fromStdString(mPosts[i].mMeta.mMsgName).contains(s,Qt::CaseInsensitive);
if(strings.empty())
passes_strings = true;
if(passes_strings && (!only_unread || (IS_MSG_UNREAD(mPosts[i].mMeta.mMsgStatus) || IS_MSG_NEW(mPosts[i].mMeta.mMsgStatus))))
mFilteredPosts.push_back(i);
}
if(postPassesFilter(mPosts[i],mFilteredStrings,mFilterUnread))
mFilteredPosts.push_back(i);
count = mFilteredPosts.size();
@ -514,20 +528,13 @@ void RsGxsChannelPostsModel::updateSinglePost(const RsGxsChannelPost& post,std::
#endif
added_files.insert(post.mFiles.begin(),post.mFiles.end());
mPosts.push_back(post);
std::sort(mPosts.begin(),mPosts.end());
// We don't do that, because otherwise the filtered posts will be changed dynamically when browsing, which is bad.
//
// mFilteredPosts.clear();
// for(uint32_t i=0;i<mPosts.size();++i)
// mFilteredPosts.push_back(i);
}
std::sort(mPosts.begin(),mPosts.end());
uint32_t count;
updateFilter(count);
triggerViewUpdate();
emit layoutChanged();
emit channelPostsLoaded();
}
void RsGxsChannelPostsModel::setPosts(const RsGxsChannelGroup& group, std::vector<RsGxsChannelPost>& posts)

View File

@ -141,6 +141,8 @@ public:
void setAllMsgReadStatus(bool read_status);
void setFilter(const QStringList &strings, bool only_unread,uint32_t &count) ;
bool postPassesFilter(const RsGxsChannelPost &post, const QStringList &strings, bool only_unread) const;
void updateFilter(uint32_t& count);
#ifdef TODO
void setAuthorOpinion(const QModelIndex& indx,RsOpinion op);
@ -239,6 +241,9 @@ public:
private:
void initEmptyHierarchy();
QStringList mFilteredStrings;
bool mFilterUnread;
std::vector<int> mFilteredPosts; // stores the list of displayes indices due to filtering.
std::vector<RsGxsChannelPost> mPosts ; // store the list of posts updated from rsForums.

View File

@ -784,6 +784,7 @@ void GxsChannelPostsWidgetWithModel::handleEvent_main_thread(std::shared_ptr<con
{
if(e->mChannelGroupId == groupId())
{
RsDbg() << "Received new message in current channel, msgId=" << e->mChannelMsgId ;
RsThread::async([this,e]()
{
@ -803,6 +804,7 @@ void GxsChannelPostsWidgetWithModel::handleEvent_main_thread(std::shared_ptr<con
return;
}
#ifdef TO_REMOVE
// Need to call this in order to get the actual comment count. The previous call only retrieves the message, since we supplied the message ID.
// another way to go would be to save the comment ids of the existing message and re-insert them before calling getChannelContent.
@ -815,6 +817,7 @@ void GxsChannelPostsWidgetWithModel::handleEvent_main_thread(std::shared_ptr<con
// Normally, there's a single post in the "post" array. The function below takes a full array of posts however.
RsGxsChannelPostsModel::computeCommentCounts(posts,comments);
#endif
// 2 - update the model in the UI thread.
@ -831,8 +834,6 @@ void GxsChannelPostsWidgetWithModel::handleEvent_main_thread(std::shared_ptr<con
mChannelFilesModel->update_files(added_filesi,removed_filesi);
updateDisplay(true,false);
},this);
});
}
@ -994,6 +995,8 @@ void GxsChannelPostsWidgetWithModel::updateData(bool update_group_data, bool upd
if(update_posts)
{
blank();
ui->postsTree->setPlaceholderText(tr("Loading..."));
mChannelPostsModel->updateChannel(groupId());
@ -1068,7 +1071,7 @@ void GxsChannelPostsWidgetWithModel::postChannelPostLoad()
void GxsChannelPostsWidgetWithModel::updateDisplay(bool update_group_data,bool update_posts)
{
// First, clear all widget
blank();
#ifdef DEBUG_CHANNEL
std::cerr << "udateDisplay: groupId()=" << groupId()<< std::endl;
#endif