From 341c7ed8e377bb7381a2ce4ab80eaa74fa083414 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Thu, 14 Dec 2023 17:15:20 +0400 Subject: [PATCH] Extracted ContentManagerModel::updateDownload() The diff is simpler if whitespace changes are ignored. --- src/contentmanagermodel.cpp | 47 +++++++++++++++++++++---------------- src/contentmanagermodel.h | 3 +++ 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/contentmanagermodel.cpp b/src/contentmanagermodel.cpp index 2e83f6f..11daf42 100644 --- a/src/contentmanagermodel.cpp +++ b/src/contentmanagermodel.cpp @@ -256,34 +256,41 @@ void ContentManagerModel::startDownload(QModelIndex index) node->setDownloadState(newDownload); QTimer *timer = newDownload->getDownloadUpdateTimer(); connect(timer, &QTimer::timeout, this, [=]() { - // We may not use node in this lambda since it may have been - // invalidated by a call to ContentManagerModel::setBooksData(). + updateDownload(bookId); + }); +} - const bool downloadStillValid = newDownload->update(bookId); +void ContentManagerModel::updateDownload(QString bookId) +{ + const auto download = m_downloads.value(bookId); - // newDownload->update() call above may result in - // ContentManagerModel::setBooksData() being called (through a chain - // of signals), which in turn will rebuild bookIdToRowMap. Hence - // bookIdToRowMap access must happen after it. + if ( ! download ) + return; - const auto it = bookIdToRowMap.constFind(bookId); + const bool downloadStillValid = download->update(bookId); - if ( ! downloadStillValid ) { - m_downloads.remove(bookId); - if ( it != bookIdToRowMap.constEnd() ) { - const size_t row = it.value(); - RowNode& rowNode = static_cast(*rootNode->child(row)); - rowNode.setDownloadState(nullptr); - } - } + // The download->update() call above may result in + // ContentManagerModel::setBooksData() being called (through a chain + // of signals), which in turn will rebuild bookIdToRowMap. Hence + // bookIdToRowMap access must happen after it. + const auto it = bookIdToRowMap.constFind(bookId); + + if ( ! downloadStillValid ) { + m_downloads.remove(bookId); if ( it != bookIdToRowMap.constEnd() ) { const size_t row = it.value(); - const QModelIndex rootNodeIndex = this->index(0, 0); - const QModelIndex newIndex = this->index(row, 5, rootNodeIndex); - emit dataChanged(newIndex, newIndex); + RowNode& rowNode = static_cast(*rootNode->child(row)); + rowNode.setDownloadState(nullptr); } - }); + } + + if ( it != bookIdToRowMap.constEnd() ) { + const size_t row = it.value(); + const QModelIndex rootNodeIndex = this->index(0, 0); + const QModelIndex newIndex = this->index(row, 5, rootNodeIndex); + emit dataChanged(newIndex, newIndex); + } } void ContentManagerModel::pauseDownload(QModelIndex index) diff --git a/src/contentmanagermodel.h b/src/contentmanagermodel.h index 06efca6..f5da2d1 100644 --- a/src/contentmanagermodel.h +++ b/src/contentmanagermodel.h @@ -53,6 +53,9 @@ protected: // functions bool canFetchMore(const QModelIndex &parent) const override; void fetchMore(const QModelIndex &parent) override; +private: // functions + void updateDownload(QString bookId); + private: // data BookInfoList m_data; std::shared_ptr rootNode;