-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraktpaginatedmodel.cpp
73 lines (62 loc) · 1.9 KB
/
traktpaginatedmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "traktpaginatedmodel.h"
template<class T>
TraktPaginatedModel<T>::TraktPaginatedModel(TraktRequest *request, QObject *parent, bool sendRequest) :
TraktModel<T>(parent),
m_request(request),
m_canFetchMore(false),
m_currentPage(1)
{
this->connect(request, &TraktRequest::replyReceived, this, &TraktPaginatedModel::onReplyReceived);
m_request->setLimit(15);
if (sendRequest) {
fetchMore(QModelIndex());
}
}
template<class T>
void TraktPaginatedModel<T>::fetchMore(const QModelIndex &parent)
{
Q_UNUSED(parent)
m_request->setPage(m_currentPage++);
m_request->send();
this->setLoading(true);
}
template<class T>
bool TraktPaginatedModel<T>::canFetchMore(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_canFetchMore;
}
template<class T>
void TraktPaginatedModel<T>::reset()
{
this->beginResetModel();
this->m_items.clear();
m_currentPage = 1;
m_canFetchMore = true;
this->endResetModel();
}
template<class T>
void TraktPaginatedModel<T>::onReplyReceived(TraktReply *reply)
{
reply->request()->setParent(this);
reply->deleteLater();
QList<T> newItems;
QVariantList items = reply->asList();
foreach (QVariant item, items) {
T convertedItem = this->convertItem(item.toMap());
if (convertedItem) {
newItems.append(convertedItem);
}
}
int page = reply->header("X-Pagination-Page").toInt();
int pageCount = reply->header("X-Pagination-Page-Count").toInt();
this->beginInsertRows(QModelIndex(), this->m_items.size(), this->m_items.size() + newItems.size() - 1);
this->m_items.append(newItems);
m_canFetchMore = page < pageCount;
this->endInsertRows();
this->setLoading(false);
this->setLoaded(true);
}
template class TraktPaginatedModel<TraktItem*>;
template class TraktPaginatedModel<TraktMovie*>;
template class TraktPaginatedModel<TraktShow*>;