Commit 3f8f11dc authored by armin's avatar armin
Browse files

Merge branch 'mutexProb' into 'master'

fixed channel sorting

See merge request !118
parents d0c9c342 053bd164
......@@ -143,15 +143,12 @@ QVariant ChannelModel::data( const QModelIndex &index, int role ) const
bool ChannelModel::addChannel( const QSharedPointer<RocketChatChannel> &channel )
{
if ( !channel.isNull() && channel->getRoomId() != "" && !duplicateCheck.contains( channel->getRoomId() ) ) {
connect( channel.data(), &RocketChatChannel::messageAdded, this, &ChannelModel::onNewerMessage, Qt::UniqueConnection );
//connect( channel.data(), &RocketChatChannel::messageAdded, this, &ChannelModel::onNewerMessage, Qt::UniqueConnection );
connect( channel.data(), &RocketChatChannel::unreadMessagesChanged, this, &ChannelModel::onUnreadMessageChanged, Qt::UniqueConnection );
connect( channel.data(), &RocketChatChannel::dataChanged, this, &ChannelModel::onDataChanged, Qt::UniqueConnection );
connect( channel.data(), &RocketChatChannel::channelDeleted, this, &ChannelModel::onDeleted, Qt::UniqueConnection );
/* connect(channel.data(), &RocketChatChannel::updatedChanged, [model = this](const QString &id, qint64 pTimeStamp){
model->beginResetModel();
model->channelList.reOrder();
model->endResetModel();
});*/
connect(channel.data(), &RocketChatChannel::updatedChanged, this, &ChannelModel::onChannelOrderChanged);
duplicateCheck.insert( channel->getRoomId() );
int pos = channelList.findPosition( channel );
if(pos <0){
......@@ -173,15 +170,11 @@ bool ChannelModel::addChannelsSlot(const QVector<QSharedPointer<RocketChatChanne
beginResetModel();
for(const auto &channel: pChannels){
if ( !channel.isNull() && channel->getRoomId() != "" && !duplicateCheck.contains( channel->getRoomId() ) ) {
connect( channel.data(), &RocketChatChannel::messageAdded, this, &ChannelModel::onNewerMessage, Qt::UniqueConnection );
// connect( channel.data(), &RocketChatChannel::messageAdded, this, &ChannelModel::onNewerMessage, Qt::UniqueConnection );
connect( channel.data(), &RocketChatChannel::unreadMessagesChanged, this, &ChannelModel::onUnreadMessageChanged, Qt::UniqueConnection );
connect( channel.data(), &RocketChatChannel::dataChanged, this, &ChannelModel::onDataChanged, Qt::UniqueConnection );
connect( channel.data(), &RocketChatChannel::channelDeleted, this, &ChannelModel::onDeleted, Qt::UniqueConnection );
/* connect(channel.data(), &RocketChatChannel::updatedChanged, [model=this](const QString &id, qint64 pTimeStamp){
model->beginResetModel();
// model->channelList.reOrder();
model->endResetModel();
});*/
connect(channel.data(), &RocketChatChannel::updatedChanged, this, &ChannelModel::onChannelOrderChanged);
duplicateCheck.insert( channel->getRoomId() );
channelList.insertSort( channel );
}
......@@ -230,7 +223,7 @@ void ChannelModel::onNewerMessage( const QString &id, qint64 timestamp )
//emit dataChanged(index(pos),index(pos),{lastMessageText});
}
sortChanged();
// sortChanged();
}
void ChannelModel::onUnreadMessageChanged( const QString &id, int number )
......@@ -303,7 +296,9 @@ void ChannelModel::onDeleted( const QString &pId, bool deleted )
void ChannelModel::onChannelOrderChanged(const QString &pId, qint64 pTimestamp)
{
qDebug()<<"order changed 123";
beginResetModel();
channelList.reOrder();
endResetModel();
}
void ChannelModel::addChannelSlot( const QSharedPointer<RocketChatChannel> &channel )
......
......@@ -20,7 +20,7 @@
#include "sortedvector.h"
#include <execution>
template<typename T>
......@@ -32,33 +32,13 @@ int SortedVector<T>::insertSort( const QSharedPointer<T> &pointer )
this->append( pointer );
row = 0;
} else {
bool lastElementGreaterThanNew = *( *this )[this->count() - 1] > *pointer ;
if ( lastElementGreaterThanNew ) {
for ( int i = 0; i < this->count(); i++ ) {
if ( *( *this )[i] > *pointer ) {
this->insert( i, pointer );
row = i;
break;
}
}
} else {
for ( int i = this->count() - 1; i ; i-- ) {
if ( *( *this )[i] < *pointer ) {
this->insert( i, pointer );
row = i;
break;
}
}
}
if ( row == -1 ) {
row = this->count();
this->append( pointer );
}
}
auto elementSmallerThanNew = std::lower_bound(this->begin(),this->end(),pointer,[](const auto &first, const auto &second)->bool{
return (*first)>(*second);
});
insert(elementSmallerThanNew, pointer);
row = elementSmallerThanNew-begin();
}
return row;
}
......@@ -66,17 +46,11 @@ template<typename T>
int SortedVector<T>::findInsertPosition( const QSharedPointer<T> &pointer ) const
{
int row = -1;
auto elementSmallerThanNew = std::lower_bound(this->begin(),this->end(),pointer,[](const auto &first, const auto &second)->bool{
return (*first)>(*second);
});
for ( int i = 0; i < this->count(); i++ ) {
if ( *( *this )[i] > *pointer ) {
row = i;
break;
}
}
if ( row == -1 ) {
row = this->count();
}
row = elementSmallerThanNew-begin();
return row;
}
......@@ -86,21 +60,15 @@ int SortedVector<T>::findPosition( const QSharedPointer<T> &pointer ) const
{
int row = -1;
for ( int i = 0; i < this->count(); i++ ) {
if ( ( *this )[i] == pointer ) {
row = i;
break;
}
}
return row;
auto pos = find(begin(),end(), pointer);
return pos - begin();
}
template<typename T>
void SortedVector<T>::reOrder()
{
std::sort(this->begin(),this->end(),[](const auto &first, const auto &second){
return first.get()>second.get();
std::sort(std::execution::par,this->begin(),this->end(),[=](const auto &first, const auto &second)->bool{
return (*first) >(*second);
});
}
......@@ -24,6 +24,7 @@
#include <QVector>
#include <QSharedPointer>
#include <QMutex>
template<typename T> class SortedVector : public QVector<QSharedPointer<T>>
{
public:
......@@ -37,6 +38,8 @@ template<typename T> class SortedVector : public QVector<QSharedPointer<T>>
this->append( o );
return true;
}
private:
mutable QMutex mSortMutex;
};
#include "sortedvector.cpp"
#endif // SORTEDVECTOR_H
......@@ -168,7 +168,7 @@ QList<QSharedPointer<RocketChatMessage>> RocketChatChannel::addMessages( const Q
}
}
if(newMessages.count()){
emit messageAdded( getRoomId(), 0 );
// emit messageAdded( getRoomId(), 0 );
}
return newMessages;
}
......@@ -192,8 +192,10 @@ unsigned int RocketChatChannel::getUnreadMessages() const
void RocketChatChannel::setUnreadMessages( unsigned int value )
{
qDebug() << "set unread to: " << value;
mUnreadMessages = value;
emit unreadMessagesChanged( mRoomId, value );
if(mUnreadMessages != value){
mUnreadMessages = value;
emit unreadMessagesChanged( mRoomId, value );
}
}
const QString &RocketChatChannel::getType() const
......@@ -262,12 +264,12 @@ void RocketChatChannel::setSelfMuted( bool value )
int RocketChatChannel::operator >( const RocketChatChannel &channel ) const
{
return channel.getUpdatedAt() > channel.getUpdatedAt();
return getUpdatedAt() > channel.getUpdatedAt();
}
int RocketChatChannel::operator <( const RocketChatChannel &channel ) const
{
return channel.getUpdatedAt() < channel.getUpdatedAt();
return getUpdatedAt() < channel.getUpdatedAt();
}
bool RocketChatChannel::getDeleted() const
......@@ -327,10 +329,12 @@ const QSharedPointer<RocketChatUser> &RocketChatChannel::getChatPartner() const
void RocketChatChannel::setChatPartner( const QSharedPointer<RocketChatUser> &chatPartner )
{
mChatPartner = chatPartner;
connect( chatPartner.data(), &RocketChatUser::statusChanged, this, [ = ]() {
emit dataChanged( mRoomId, "userStatus" );
} );
if(mChatPartner != chatPartner){
mChatPartner = chatPartner;
connect( chatPartner.data(), &RocketChatUser::statusChanged, this, [ = ]() {
emit dataChanged( mRoomId, "userStatus" );
} );
}
}
const QString &RocketChatChannel::getChatPartnerId() const
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment