Extracted ContentManagerModel::updateDownload()

The diff is simpler if whitespace changes are ignored.
This commit is contained in:
Veloman Yunkan 2023-12-14 17:15:20 +04:00
parent 3e3cd676b5
commit 341c7ed8e3
2 changed files with 30 additions and 20 deletions

View File

@ -256,34 +256,41 @@ void ContentManagerModel::startDownload(QModelIndex index)
node->setDownloadState(newDownload); node->setDownloadState(newDownload);
QTimer *timer = newDownload->getDownloadUpdateTimer(); QTimer *timer = newDownload->getDownloadUpdateTimer();
connect(timer, &QTimer::timeout, this, [=]() { connect(timer, &QTimer::timeout, this, [=]() {
// We may not use node in this lambda since it may have been updateDownload(bookId);
// invalidated by a call to ContentManagerModel::setBooksData(). });
}
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 if ( ! download )
// ContentManagerModel::setBooksData() being called (through a chain return;
// of signals), which in turn will rebuild bookIdToRowMap. Hence
// bookIdToRowMap access must happen after it.
const auto it = bookIdToRowMap.constFind(bookId); const bool downloadStillValid = download->update(bookId);
if ( ! downloadStillValid ) { // The download->update() call above may result in
m_downloads.remove(bookId); // ContentManagerModel::setBooksData() being called (through a chain
if ( it != bookIdToRowMap.constEnd() ) { // of signals), which in turn will rebuild bookIdToRowMap. Hence
const size_t row = it.value(); // bookIdToRowMap access must happen after it.
RowNode& rowNode = static_cast<RowNode&>(*rootNode->child(row));
rowNode.setDownloadState(nullptr);
}
}
const auto it = bookIdToRowMap.constFind(bookId);
if ( ! downloadStillValid ) {
m_downloads.remove(bookId);
if ( it != bookIdToRowMap.constEnd() ) { if ( it != bookIdToRowMap.constEnd() ) {
const size_t row = it.value(); const size_t row = it.value();
const QModelIndex rootNodeIndex = this->index(0, 0); RowNode& rowNode = static_cast<RowNode&>(*rootNode->child(row));
const QModelIndex newIndex = this->index(row, 5, rootNodeIndex); rowNode.setDownloadState(nullptr);
emit dataChanged(newIndex, newIndex);
} }
}); }
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) void ContentManagerModel::pauseDownload(QModelIndex index)

View File

@ -53,6 +53,9 @@ protected: // functions
bool canFetchMore(const QModelIndex &parent) const override; bool canFetchMore(const QModelIndex &parent) const override;
void fetchMore(const QModelIndex &parent) override; void fetchMore(const QModelIndex &parent) override;
private: // functions
void updateDownload(QString bookId);
private: // data private: // data
BookInfoList m_data; BookInfoList m_data;
std::shared_ptr<RowNode> rootNode; std::shared_ptr<RowNode> rootNode;