Browse Source

made shure quick presenter keeps presenting order

pull/2/head
Skycoder42 7 years ago
parent
commit
7e56f83235
No known key found for this signature in database GPG Key ID: 8E01AD9EF0578D2B
  1. 54
      src/imports/mvvmquick/qqmlquickpresenter.cpp
  2. 9
      src/imports/mvvmquick/qqmlquickpresenter.h

54
src/imports/mvvmquick/qqmlquickpresenter.cpp

@ -16,11 +16,8 @@
using namespace QtMvvm; using namespace QtMvvm;
QQmlQuickPresenter::QQmlQuickPresenter(QQmlEngine *engine) : QQmlQuickPresenter::QQmlQuickPresenter(QQmlEngine *engine) :
QObject(engine), QObject{engine},
_engine(engine), _engine{engine}
_latestComponent(),
_componentCache(),
_loadCache()
{ {
QuickPresenterPrivate::setQmlPresenter(this); QuickPresenterPrivate::setQmlPresenter(this);
connect(QuickPresenterPrivate::currentPresenter(), &QuickPresenter::inputViewFactoryChanged, connect(QuickPresenterPrivate::currentPresenter(), &QuickPresenter::inputViewFactoryChanged,
@ -105,16 +102,19 @@ void QQmlQuickPresenter::hapticLongPress()
void QQmlQuickPresenter::present(ViewModel *viewModel, const QVariantHash &params, const QUrl &viewUrl, QPointer<ViewModel> parent) void QQmlQuickPresenter::present(ViewModel *viewModel, const QVariantHash &params, const QUrl &viewUrl, QPointer<ViewModel> parent)
{ {
auto component = _componentCache.object(viewUrl); auto component = _componentCache.object(viewUrl);
if(component) if(component) {
addObject(component, viewModel, params, parent); _loadQueue.enqueue(std::make_tuple(*component, viewModel, params, parent));
else { processShowQueue();
} else {
//create component (and replace latest) //create component (and replace latest)
if(_latestComponent) { if(_latestComponent) {
disconnect(_latestComponent, &QQmlComponent::progressChanged, disconnect(_latestComponent, &QQmlComponent::progressChanged,
this, &QQmlQuickPresenter::loadingProgressChanged); this, &QQmlQuickPresenter::loadingProgressChanged);
} }
_latestComponent = new QQmlComponent(_engine, this); _latestComponent = new QQmlComponent{_engine};
_loadCache.insert(_latestComponent, std::make_tuple(viewModel, params, parent)); component = new QSharedPointer<QQmlComponent>{_latestComponent.data()};
_componentCache.insert(viewUrl, component);
_loadQueue.enqueue(std::make_tuple(*component, viewModel, params, parent));
//setup ui status //setup ui status
emit viewLoadingChanged(true); emit viewLoadingChanged(true);
@ -157,31 +157,45 @@ void QQmlQuickPresenter::statusChanged(QQmlComponent::Status status)
case QQmlComponent::Ready: case QQmlComponent::Ready:
{ {
logDebug() << "Loaded and cached component" << component->url(); logDebug() << "Loaded and cached component" << component->url();
_componentCache.insert(component->url(), component);
auto loadInfo = _loadCache.value(component);
disconnect(component, &QQmlComponent::progressChanged,
this, &QQmlQuickPresenter::loadingProgressChanged);
addObject(component, std::get<0>(loadInfo), std::get<1>(loadInfo), std::get<2>(loadInfo));
break; break;
} }
case QQmlComponent::Error: case QQmlComponent::Error:
{ {
auto loadInfo = _loadCache.value(component); logWarning().noquote() << "Failed to load component" << component->url()
logWarning().noquote() << "Failed to load component for" << std::get<0>(loadInfo)->metaObject()->className()
<< "with error:" << component->errorString().trimmed(); << "with error:" << component->errorString().trimmed();
std::get<0>(loadInfo)->deleteLater(); _componentCache.remove(component->url());
component->deleteLater();
break; break;
} }
default: default:
return; //not break. code after must not be executed in this case return; //not break. code after must not be executed in this case
} }
_loadCache.remove(component);
if(_latestComponent == component) { if(_latestComponent == component) {
disconnect(component, &QQmlComponent::progressChanged,
this, &QQmlQuickPresenter::loadingProgressChanged);
_latestComponent = nullptr; _latestComponent = nullptr;
emit viewLoadingChanged(false); emit viewLoadingChanged(false);
} }
processShowQueue();
}
void QQmlQuickPresenter::processShowQueue()
{
while(!_loadQueue.isEmpty()) {
auto loadInfo = _loadQueue.head();
switch(std::get<0>(loadInfo)->status()){
case QQmlComponent::Ready:
addObject(std::get<0>(loadInfo).data(), std::get<1>(loadInfo), std::get<2>(loadInfo), std::get<3>(loadInfo));
break;
case QQmlComponent::Null:
case QQmlComponent::Error:
std::get<1>(loadInfo)->deleteLater();
break;
default:
return; //not break. code after must not be executed in this case
}
_loadQueue.dequeue();
}
} }
void QQmlQuickPresenter::addObject(QQmlComponent *component, ViewModel *viewModel, const QVariantHash &params, const QPointer<ViewModel> &parent) void QQmlQuickPresenter::addObject(QQmlComponent *component, ViewModel *viewModel, const QVariantHash &params, const QPointer<ViewModel> &parent)

9
src/imports/mvvmquick/qqmlquickpresenter.h

@ -8,6 +8,8 @@
#include <QtCore/QVariant> #include <QtCore/QVariant>
#include <QtCore/QPointer> #include <QtCore/QPointer>
#include <QtCore/QUrl> #include <QtCore/QUrl>
#include <QtCore/QQueue>
#include <QtCore/QSharedPointer>
#include <QtQml/QQmlComponent> #include <QtQml/QQmlComponent>
@ -158,14 +160,15 @@ private Q_SLOTS:
void statusChanged(QQmlComponent::Status status); void statusChanged(QQmlComponent::Status status);
private: private:
using PresentTuple = std::tuple<ViewModel*, QVariantHash, QPointer<ViewModel>>; using PresentTuple = std::tuple<QSharedPointer<QQmlComponent>, ViewModel*, QVariantHash, QPointer<ViewModel>>;
QQmlEngine *_engine; QQmlEngine *_engine;
QPointer<QObject> _qmlPresenter; QPointer<QObject> _qmlPresenter;
QPointer<QQmlComponent> _latestComponent; QPointer<QQmlComponent> _latestComponent;
QCache<QUrl, QQmlComponent> _componentCache; QCache<QUrl, QSharedPointer<QQmlComponent>> _componentCache;
QHash<QQmlComponent*, PresentTuple> _loadCache; QQueue<PresentTuple> _loadQueue;
void processShowQueue();
void addObject(QQmlComponent *component, ViewModel *viewModel, const QVariantHash &params, const QPointer<ViewModel> &parent); void addObject(QQmlComponent *component, ViewModel *viewModel, const QVariantHash &params, const QPointer<ViewModel> &parent);
}; };

Loading…
Cancel
Save