Make the Controller a singleton

This commit is contained in:
Tobias Fella
2020-11-04 01:43:13 +00:00
committed by Nicolas Fella
parent 1739a454da
commit 2d1a7d6500
13 changed files with 42 additions and 66 deletions

View File

@@ -10,21 +10,10 @@
AccountListModel::AccountListModel(QObject *parent)
: QAbstractListModel(parent)
{
}
void AccountListModel::setController(Controller *value)
{
if (m_controller == value) {
return;
}
beginResetModel();
m_connections.clear();
m_controller = value;
m_connections += m_controller->connections();
m_connections += Controller::instance().connections();
connect(m_controller, &Controller::connectionAdded, this, [=](Connection *conn) {
connect(&Controller::instance(), &Controller::connectionAdded, this, [=](Connection *conn) {
if (!conn) {
return;
}
@@ -32,7 +21,7 @@ void AccountListModel::setController(Controller *value)
m_connections.append(conn);
endInsertRows();
});
connect(m_controller, &Controller::connectionDropped, this, [=](Connection *conn) {
connect(&Controller::instance(), &Controller::connectionDropped, this, [=](Connection *conn) {
qDebug() << "Dropping connection" << conn->userId();
if (!conn) {
qDebug() << "Trying to remove null connection";
@@ -47,7 +36,6 @@ void AccountListModel::setController(Controller *value)
m_connections.erase(it);
endRemoveRows();
});
Q_EMIT controllerChanged();
}
QVariant AccountListModel::data(const QModelIndex &index, int role) const

View File

@@ -14,7 +14,6 @@
class AccountListModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(Controller *controller READ controller WRITE setController NOTIFY controllerChanged)
public:
enum EventRoles { UserRole = Qt::UserRole + 1, ConnectionRole };
@@ -25,18 +24,8 @@ public:
QHash<int, QByteArray> roleNames() const override;
Controller *controller() const
{
return m_controller;
}
void setController(Controller *value);
private:
Controller *m_controller = nullptr;
QVector<Connection *> m_connections;
Q_SIGNALS:
void controllerChanged();
};
#endif // ACCOUNTLISTMODEL_H

View File

@@ -31,8 +31,11 @@ class Controller : public QObject
Q_PROPERTY(bool busy READ busy WRITE setBusy NOTIFY busyChanged)
public:
explicit Controller(QObject *parent = nullptr);
~Controller();
static Controller &instance()
{
static Controller _instance;
return _instance;
}
Q_INVOKABLE void loginWithCredentials(QString, QString, QString, QString);
Q_INVOKABLE void loginWithAccessToken(QString, QString, QString, QString);
@@ -100,6 +103,9 @@ public:
}
private:
explicit Controller(QObject *parent = nullptr);
~Controller();
QVector<Connection *> m_connections;
QPointer<Connection> m_connection;
QNetworkConfigurationManager m_ncm;

View File

@@ -55,7 +55,7 @@ int main(int argc, char *argv[])
app.setApplicationName("neochat");
app.setWindowIcon(QIcon(":/assets/img/icon.png"));
qmlRegisterType<Controller>("Spectral", 0, 1, "Controller");
qmlRegisterSingletonInstance("Spectral", 0, 1, "Controller", &Controller::instance());
qmlRegisterType<AccountListModel>("Spectral", 0, 1, "AccountListModel");
qmlRegisterType<RoomListModel>("Spectral", 0, 1, "RoomListModel");
qmlRegisterType<UserListModel>("Spectral", 0, 1, "UserListModel");