Browse Source

added basic single instance support

pull/2/head
Skycoder42 7 years ago
parent
commit
2bf6f2eaa2
No known key found for this signature in database GPG Key ID: 8E01AD9EF0578D2B
  1. 2
      examples/mvvmcore/SampleCore/tabviewmodel.h
  2. 1
      src/imports/mvvmcore/plugins.qmltypes
  3. 24
      src/mvvmcore/coreapp.cpp
  4. 1
      src/mvvmcore/coreapp_p.h
  5. 7
      src/mvvmcore/viewmodel.h

2
examples/mvvmcore/SampleCore/tabviewmodel.h

@ -7,6 +7,8 @@ class TabViewModel : public QtMvvm::ViewModel
{
Q_OBJECT
QTMVVM_SINGLETON
public:
Q_INVOKABLE explicit TabViewModel(QObject *parent = nullptr);
~TabViewModel();

1
src/imports/mvvmcore/plugins.qmltypes

@ -587,6 +587,7 @@ Module {
name: "resultReady"
Parameter { name: "result"; type: "QVariant" }
}
Signal { name: "instanceInvoked"; revision: 1 }
Method {
name: "onInit"
Parameter { name: "params"; type: "QVariantHash" }

24
src/mvvmcore/coreapp.cpp

@ -6,6 +6,7 @@
#include <QtCore/QCommandLineParser>
#include <QtCore/QRegularExpression>
#include <QtCore/QMetaClassInfo>
#include <QtGui/QGuiApplication>
@ -41,6 +42,7 @@ void CoreApp::registerApp()
qRegisterMetaType<QtMvvm::SettingsElements::Setup>();
registerInterfaceConverter<IPresenter>();
//setup
setParent(qApp);
CoreAppPrivate::instance = this;
@ -171,6 +173,24 @@ QScopedPointer<CoreAppPrivate> &CoreAppPrivate::dInstance()
void CoreAppPrivate::showViewModel(const QMetaObject *metaObject, const QVariantHash &params, QPointer<ViewModel> parent, quint32 requestCode)
{
if(presenter) {
//first: check for single instance
auto isSingleton = false;
auto sInfoIndex = metaObject->indexOfClassInfo("qtmvvm_singleton");
if(sInfoIndex != -1) {
auto sInfo = metaObject->classInfo(sInfoIndex);
Q_ASSERT(qstrcmp(sInfo.name(), "qtmvvm_singleton") == 0);
isSingleton = qstrcmp(sInfo.value(), "true") == 0;
}
// next handle the singleton
if(isSingleton) {
auto viewModel = singleInstances.value(metaObject);
if(viewModel) {
emit viewModel->instanceInvoked(ViewModel::QPrivateSignal{});
return;
}
}
QPointer<ViewModel> vm;
try {
auto obj = ServiceRegistry::instance()->constructInjected(metaObject);
@ -188,6 +208,10 @@ void CoreAppPrivate::showViewModel(const QMetaObject *metaObject, const QVariant
});
}
logDebug() << "Successfully presented" << metaObject->className();
// if singleton -> store it
if(isSingleton)
singleInstances.insert(metaObject, vm);
} catch(QException &e) {
logCritical() << "Failed to present viewmodel of type"
<< metaObject->className()

1
src/mvvmcore/coreapp_p.h

@ -28,6 +28,7 @@ private:
static QPointer<CoreApp> instance;
IPresenter *presenter = nullptr;
QHash<const QMetaObject*, QPointer<ViewModel>> singleInstances;
};
}

7
src/mvvmcore/viewmodel.h

@ -14,6 +14,7 @@
namespace QtMvvm {
class CoreApp;
class CoreAppPrivate;
class ViewModelPrivate;
//! The base class for all viewmodels
@ -36,6 +37,8 @@ Q_SIGNALS:
//! Should be emitted when the viewmodels result is ready
void resultReady(const QVariant &result);
Q_REVISION(1) void instanceInvoked(QPrivateSignal);
protected:
//! Show another viewmodel as a child of this one
template <typename TViewModel>
@ -54,12 +57,16 @@ protected:
private:
friend class QtMvvm::CoreApp;
friend class QtMvvm::CoreAppPrivate;
QScopedPointer<ViewModelPrivate> d;
static void showImp(const QMetaObject *metaObject, QVariantHash params, QPointer<ViewModel> parent, quint32 requestCode = 0);
};
//TODO doc
#define QTMVVM_SINGLETON Q_CLASSINFO("qtmvvm_singleton", "true")
// ------------- Generic Implementation -------------
template<typename TViewModel>

Loading…
Cancel
Save