Commit d54a0fb4 authored by Armin Felder's avatar Armin Felder
Browse files

faster channelLoad

parent 344e9908
......@@ -163,6 +163,24 @@ bool ChannelModel::addChannel( const QSharedPointer<RocketChatChannel> &channel
}
bool ChannelModel::addChannelsSlot(const QVector<QSharedPointer<RocketChatChannel> > &pChannels)
{
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::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 );
duplicateCheck.insert( channel->getRoomId() );
channelList.insertSort( channel );
}
}
endResetModel();
sortChanged();
return true;
}
QHash<int, QByteArray> ChannelModel::roleNames() const
{
QHash<int, QByteArray> roles;
......@@ -291,29 +309,11 @@ void ChannelModel::setType( const QString &value )
void ChannelModel::sortChanged()
{
static int counter = 0;
counter++;
qDebug()<<counter;
beginResetModel();
std::sort( channelList.begin(), channelList.end(), []( const QSharedPointer<RocketChatChannel> &channel1, const QSharedPointer<RocketChatChannel> &channel2 ) {
auto youngest1 = channel1->getYoungestMessage();
auto youngest2 = channel2->getYoungestMessage();
if ( !youngest1.isNull() && !youngest2.isNull() ) {
auto timestamp1 = youngest1->getTimestamp();
auto timestamp2 = youngest2->getTimestamp();
return timestamp2 < timestamp1;
}
if ( youngest1.isNull() && !youngest2.isNull() ) {
return false;
}
if ( youngest2.isNull() && !youngest1.isNull() ) {
return true;
}
return false;
} );
channelList.reOrder();
endResetModel();
emit countChanged();
}
......
......@@ -63,7 +63,7 @@ class ChannelModel : public QAbstractListModel
void setType( const QString &value );
void sortChanged( void );
void clear( void );
protected:
private:
SortedVector<RocketChatChannel> channelList;
QSet<QString> duplicateCheck;
QHash<int, QByteArray> roleNames() const;
......@@ -72,6 +72,8 @@ class ChannelModel : public QAbstractListModel
void onUnreadMessageChanged( const QString &id, int number );
void onDataChanged( const QString &id, const QString &property );
void onDeleted( const QString &pId, bool deleted );
void onChannelOrderChanged(const QString &pId, qint64 pTimestamp);
QTimer timer;
signals:
void countChanged( void );
......@@ -79,6 +81,7 @@ class ChannelModel : public QAbstractListModel
public slots:
void addChannelSlot(const QSharedPointer<RocketChatChannel> &);
bool addChannelsSlot( const QVector<QSharedPointer<RocketChatChannel>> & );
};
......
......@@ -96,3 +96,9 @@ int SortedVector<T>::findPosition( const QSharedPointer<T> &pointer ) const
return row;
}
template<typename T>
void SortedVector<T>::reOrder()
{
std::sort(this->begin(),this->end());
}
......@@ -30,6 +30,7 @@ template<typename T> class SortedVector : public QVector<QSharedPointer<T>>
int insertSort( const QSharedPointer<T> & );
int findInsertPosition( const QSharedPointer<T> & ) const;
int findPosition( const QSharedPointer<T> & ) const;
void reOrder();
bool operator =( const QVector<QSharedPointer<T>> &o )
{
this->clear();
......
......@@ -59,6 +59,28 @@ bool ChannelRepository::add( const QSharedPointer<RocketChatChannel> &pChannel )
return add( id, pChannel );
}
bool ChannelRepository::add(const QString &pType, const QVector<QSharedPointer<RocketChatChannel> > &pChannels)
{
if(!pChannels.empty()){
for(const auto &elem: pChannels){
if ( !mElements.contains( elem->getRoomId() ) ) {
mNameIndex[elem->getName()] = elem;
auto result = QMetaObject::invokeMethod( mMessagesModel, "addChannelSlot", Q_ARG( QSharedPointer<RocketChatChannel>, elem ) );
AbstractBaseRepository::add( elem->getRoomId(), elem );
}
}
}
if(pType == "p"){
auto result = QMetaObject::invokeMethod( mGroupsModel, "addChannelsSlot", Q_ARG( QVector<QSharedPointer<RocketChatChannel> >, pChannels) );
}else if(pType == "c"){
auto result = QMetaObject::invokeMethod( mChannelsModel, "addChannelsSlot", Q_ARG( QVector<QSharedPointer<RocketChatChannel> >, pChannels ) );
}else if(pType == "d"){
auto result = QMetaObject::invokeMethod( mDirectModel, "addChannelsSlot", Q_ARG( QVector<QSharedPointer<RocketChatChannel> >, pChannels ) );
}
return true;
}
QSharedPointer<RocketChatChannel> ChannelRepository::getChannelByName( const QString &pName )
{
if ( mNameIndex.contains( pName ) ) {
......
......@@ -42,6 +42,7 @@ class ChannelRepository : public QObject, public AbstractBaseRepository<RocketCh
explicit ChannelRepository( QObject *parent = nullptr );
bool add( const QString &id, const QSharedPointer<RocketChatChannel> & );
bool add( const QSharedPointer<RocketChatChannel> &pChannel );
bool add(const QString &pType, const QVector<QSharedPointer<RocketChatChannel>> &pChannels);
QSharedPointer<RocketChatChannel> getChannelByName( const QString &pName );
const QSharedPointer<RocketChatChannel> getChannelByName( const QString &pName ) const;
......
......@@ -366,7 +366,10 @@ qint64 RocketChatChannel::getUpdatedAt() const
void RocketChatChannel::setUpdatedAt( const qint64 &updatedAt )
{
mUpdatedAt = updatedAt;
if(mUpdatedAt != updatedAt){
mUpdatedAt = updatedAt;
emit updatedChanged(mRoomId,mUpdatedAt);
}
}
qint64 RocketChatChannel::getCreatedAt() const
......@@ -376,7 +379,7 @@ qint64 RocketChatChannel::getCreatedAt() const
void RocketChatChannel::setCreatedAt( const qint64 &createdAt )
{
mCreatedAt = createdAt;
mCreatedAt = createdAt;
}
bool RocketChatChannel::getBlocked() const
......
......@@ -173,6 +173,7 @@ class RocketChatChannel : public QObject
void dataChanged( const QString &id, const QString &property );
void chatPartnerStatusChanged();
void messageDeleted(const QString, const QString);
void updatedChanged(const QString, qint64 updatedAt);
};
......
......@@ -36,7 +36,7 @@ RocketChatChannelService::RocketChatChannelService( QObject *parent, RocketChatS
}
QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType )
QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType, bool insertIntoRepo )
{
auto ptr = QSharedPointer<RocketChatChannel>::create( mServer, mMessageService, pRoomId, pName, pType );
......@@ -51,7 +51,7 @@ QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject(
return ptr;
}
QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType, const QString &pUsername )
QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType, const QString &pUsername, bool insertIntoRepo )
{
auto ptr = QSharedPointer<RocketChatChannel>::create( mServer, mMessageService, pRoomId, pName, pType );
......@@ -67,7 +67,8 @@ QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject(
mFileService->getFileRessource( avatarRequest );
}
if ( Q_LIKELY( mChannels ) ) {
//TODO: this should not be done here!
if (insertIntoRepo && Q_LIKELY( mChannels ) ) {
mChannels->add( ptr );
} else {
qCritical() << "Channelsrepo not available to ChannelService";
......@@ -269,11 +270,15 @@ void RocketChatChannelService::loadJoinedChannelsFromDb()
if ( Q_LIKELY( !roomsList.isEmpty() ) ) {
QVector<QSharedPointer<RocketChatChannel>> channels;
QVector<QSharedPointer<RocketChatChannel>> channelsP;
QVector<QSharedPointer<RocketChatChannel>> channelsD;
QVector<QSharedPointer<RocketChatChannel>> channelsC;
auto users = Models::getUsersModel();
for ( const auto &roomHash : roomsList ) {
QSharedPointer<RocketChatChannel> channelPointer = createChannelObject( roomHash["id"].toString(), roomHash["name"].toString(), roomHash["type"].toString(), roomHash["username"].toString() );
auto channelPointer = createChannelObject( roomHash["id"].toString(), roomHash["name"].toString(), roomHash["type"].toString(), roomHash["username"].toString(), false );
if ( !channelPointer.isNull() ) {
channelPointer->setMuted( roomHash["list"].toStringList() );
......@@ -281,6 +286,7 @@ void RocketChatChannelService::loadJoinedChannelsFromDb()
channelPointer->setReadOnly( roomHash["readOnly"].toBool() );
channelPointer->setJoined( roomHash["joined"].toBool() );
channelPointer->setChatPartnerId( roomHash["chatPartnerId"].toString() );
channelPointer->setUpdatedAt(roomHash["updatedAt"].toLongLong());
if ( channelPointer->getType() == "d" ) {
auto user = users->getUserById( channelPointer->getChatPartnerId() );
......@@ -301,9 +307,18 @@ void RocketChatChannelService::loadJoinedChannelsFromDb()
}
channels.append( channelPointer );
if(channelPointer->getType() == "p"){
channelsP.append(channelPointer);
}else if(channelPointer->getType() == "c"){
channelsC.append(channelPointer);
}else if(channelPointer->getType() == "d"){
channelsD.append(channelPointer);
}
}
}
mChannels->add("p",channelsP);
mChannels->add("c",channelsC);
mChannels->add("d",channelsD);
emit channelsLoaded( channels, true );
}
}
......
......@@ -64,8 +64,8 @@ class RocketChatChannelService : public QObject
MeteorDDP *getDdp() const;
void setDdp( MeteorDDP *ddp );
QSharedPointer<RocketChatChannel> createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType );
QSharedPointer<RocketChatChannel> createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType, const QString &username );
QSharedPointer<RocketChatChannel> createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType, bool insertIntoRepo = true );
QSharedPointer<RocketChatChannel> createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType, const QString &username, bool insertIntoRepo = true );
void openPrivateChannelWith( const QString &pUsername );
......
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