General update
This commit is contained in:
@@ -2,8 +2,6 @@
|
||||
|
||||
VideoSearchModel::VideoSearchModel(QObject *parent) : QAbstractListModel{parent}
|
||||
{
|
||||
setState(Loading);
|
||||
|
||||
m_paginator = new VideoSearchPaginator(this);
|
||||
PaginationNotifier *notifier = static_cast<PaginationNotifier*>(m_paginator);
|
||||
|
||||
@@ -37,11 +35,25 @@ VideoSearchModel::VideoSearchModel(QObject *parent) : QAbstractListModel{parent}
|
||||
|
||||
QObject::connect(
|
||||
notifier,
|
||||
&PaginationNotifier::dataChanged,
|
||||
&PaginationNotifier::pageChanged,
|
||||
this,
|
||||
&VideoSearchModel::resetDataModel
|
||||
);
|
||||
|
||||
QObject::connect(
|
||||
notifier,
|
||||
&PaginationNotifier::fetchComplete,
|
||||
this,
|
||||
&VideoSearchModel::resetState
|
||||
);
|
||||
|
||||
QObject::connect(
|
||||
notifier,
|
||||
&PaginationNotifier::fetching,
|
||||
this,
|
||||
&VideoSearchModel::onPaginatorFetching
|
||||
);
|
||||
|
||||
QObject::connect(
|
||||
m_paginator,
|
||||
&VideoSearchPaginator::queryChanged,
|
||||
@@ -62,12 +74,17 @@ auto VideoSearchModel::rowCount(const QModelIndex &) const -> int
|
||||
|
||||
auto VideoSearchModel::data(const QModelIndex &index, int role) const -> QVariant
|
||||
{
|
||||
if(index.row() < 0 || m_paginator == nullptr)
|
||||
if(m_paginator == nullptr)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
VideoCache dataPoint = m_data.at(index.row());
|
||||
if(index.row() < 0 || index.row() >= m_paginator->count() )
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
VideoCache dataPoint = m_paginator->at(index.row());
|
||||
|
||||
QVariant data;
|
||||
|
||||
@@ -109,10 +126,10 @@ auto VideoSearchModel::index(int row, int column, const QModelIndex &parent) con
|
||||
{
|
||||
Q_UNUSED(parent)
|
||||
|
||||
if(row < 0 || row >= m_data.count())
|
||||
if(row < 0 || row >= m_paginator->count())
|
||||
return {};
|
||||
|
||||
return createIndex(row, column, &m_data[row]);
|
||||
return createIndex(row, column, &m_paginator[row]);
|
||||
}
|
||||
|
||||
auto VideoSearchModel::columnCount(const QModelIndex &) const -> int
|
||||
@@ -153,37 +170,30 @@ auto VideoSearchModel::resetState() -> void
|
||||
|
||||
auto VideoSearchModel::resetDataModel() -> void
|
||||
{
|
||||
setState(Loading);
|
||||
|
||||
//invalidate previous model data
|
||||
beginRemoveRows(QModelIndex(), 0, m_data.count());
|
||||
m_data.clear();
|
||||
beginRemoveRows(QModelIndex(), 0, m_paginator->count());
|
||||
endRemoveRows();
|
||||
|
||||
//signal new data
|
||||
beginInsertRows(QModelIndex(), 0, m_paginator->count() - 1);
|
||||
auto dataset = m_paginator->data();
|
||||
|
||||
for(const VideoCache& data : std::as_const(dataset))
|
||||
{
|
||||
m_data.append(data);
|
||||
}
|
||||
|
||||
endInsertRows();
|
||||
|
||||
setState(Idle);
|
||||
}
|
||||
|
||||
auto VideoSearchModel::hasNextPage() -> bool
|
||||
auto VideoSearchModel::hasNextPage() const -> bool
|
||||
{
|
||||
return m_paginator->page() < m_paginator->totalPages();
|
||||
}
|
||||
|
||||
auto VideoSearchModel::hasPreviousPage() -> bool
|
||||
auto VideoSearchModel::hasPreviousPage() const -> bool
|
||||
{
|
||||
return m_paginator->page() > 0;
|
||||
}
|
||||
|
||||
auto VideoSearchModel::onPaginatorFetching() -> void
|
||||
{
|
||||
setState(Loading);
|
||||
}
|
||||
|
||||
auto VideoSearchModel::roleNames() const -> QHash<int, QByteArray>
|
||||
{
|
||||
return m_dataRoles;
|
||||
@@ -219,7 +229,13 @@ auto VideoSearchModel::setResultsPerPage(qsizetype resultsPerPage) -> void
|
||||
{
|
||||
if(m_paginator != nullptr)
|
||||
{
|
||||
m_paginator->setResultsPerPage(resultsPerPage);
|
||||
QFuture<void> future = QtConcurrent::run
|
||||
(
|
||||
[this, resultsPerPage]
|
||||
{
|
||||
m_paginator->setResultsPerPage(resultsPerPage);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,19 +249,31 @@ auto VideoSearchModel::totalResults() const -> qsizetype
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto VideoSearchModel::nextPage() const -> void
|
||||
auto VideoSearchModel::nextPage() -> void
|
||||
{
|
||||
if(m_paginator != nullptr)
|
||||
{
|
||||
m_paginator->next();
|
||||
QFuture<void> future = QtConcurrent::run
|
||||
(
|
||||
[this]
|
||||
{
|
||||
m_paginator->next();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
auto VideoSearchModel::previousPage() const -> void
|
||||
auto VideoSearchModel::previousPage() -> void
|
||||
{
|
||||
if(m_paginator != nullptr)
|
||||
{
|
||||
m_paginator->previous();
|
||||
QFuture<void> future = QtConcurrent::run
|
||||
(
|
||||
[this]
|
||||
{
|
||||
m_paginator->previous();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -263,7 +291,13 @@ auto VideoSearchModel::setQuery(const QString &query) -> void
|
||||
{
|
||||
if(m_paginator != nullptr)
|
||||
{
|
||||
m_paginator->setQuery(query);
|
||||
QFuture<void> future = QtConcurrent::run
|
||||
(
|
||||
[this, query]
|
||||
{
|
||||
m_paginator->setQuery(query);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user