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

@@ -31,9 +31,7 @@ Dialog {
spacing: 16
model: AccountListModel{
controller: spectralController
}
model: AccountListModel{ }
delegate: Kirigami.Avatar {
width: 48
@@ -48,13 +46,13 @@ Dialog {
MenuItem {
text: "Mark all as read"
onClicked: spectralController.markAllMessagesAsRead(connection)
onClicked: Controller.markAllMessagesAsRead(connection)
}
MenuItem {
text: "Logout"
onClicked: spectralController.logout(connection)
onClicked: Controller.logout(connection)
}
}
@@ -63,7 +61,7 @@ Dialog {
circular: true
onPrimaryClicked: spectralController.connection = connection
onPrimaryClicked: Controller.connection = connection
onSecondaryClicked: contextMenu.popup()
}
}
@@ -113,7 +111,7 @@ Dialog {
anchors.fill: parent
onPrimaryClicked: {
joinRoomDialog.createObject(ApplicationWindow.overlay, {"controller": spectralController, "connection": spectralController.connection}).open()
joinRoomDialog.createObject(ApplicationWindow.overlay, {"connection": Controller.connection}).open()
root.close()
}
}
@@ -143,7 +141,7 @@ Dialog {
anchors.fill: parent
onPrimaryClicked: {
startChatDialog.createObject(ApplicationWindow.overlay, {"controller": spectralController, "connection": spectralController.connection}).open()
startChatDialog.createObject(ApplicationWindow.overlay, {"connection": Controller.connection}).open()
root.close()
}
}

View File

@@ -4,6 +4,8 @@ import QtQuick.Layouts 1.12
import Spectral.Component 2.0
import Spectral 0.1
Dialog {
anchors.centerIn: parent
width: 360
@@ -32,7 +34,7 @@ Dialog {
standardButtons: Dialog.Ok | Dialog.Cancel
onAccepted: spectralController.createRoom(spectralController.connection, roomNameField.text, roomTopicField.text)
onAccepted: Controller.createRoom(Controller.connection, roomNameField.text, roomTopicField.text)
onClosed: destroy()
}

View File

@@ -10,7 +10,6 @@ import Spectral.Setting 0.1
import Spectral 0.1
Dialog {
property var controller
property var room
anchors.centerIn: parent

View File

@@ -10,7 +10,6 @@ import Spectral.Setting 0.1
import Spectral 0.1
Dialog {
property var controller
property var connection
property string keyword
@@ -59,7 +58,7 @@ Dialog {
if (identifierField.isJoined) {
roomListForm.joinRoom(identifierField.room)
} else {
controller.joinRoom(connection, identifierField.text)
Controller.joinRoom(connection, identifierField.text)
}
}
}
@@ -249,7 +248,7 @@ Dialog {
circular: true
onClicked: {
controller.joinRoom(connection, roomID)
Controller.joinRoom(connection, roomID)
root.close()
}
}

View File

@@ -7,6 +7,8 @@ import Spectral.Component 2.0
import Spectral.Effect 2.0
import Spectral.Setting 0.1
import Spectral 0.1
Dialog {
property var room
@@ -168,7 +170,7 @@ Dialog {
anchors.fill: parent
onClicked: {
roomListForm.enteredRoom = spectralController.connection.room(room.predecessorId)
roomListForm.enteredRoom = Controller.connection.room(room.predecessorId)
root.close()
}
}
@@ -219,7 +221,7 @@ Dialog {
anchors.fill: parent
onClicked: {
roomListForm.enteredRoom = spectralController.connection.room(room.successorId)
roomListForm.enteredRoom = Controller.connection.room(room.successorId)
root.close()
}
}

View File

@@ -9,7 +9,6 @@ import Spectral.Setting 0.1
import Spectral 0.1
Dialog {
property var controller
property var connection
anchors.centerIn: parent
@@ -47,7 +46,7 @@ Dialog {
highlighted: true
onClicked: {
controller.createDirectChat(connection, identifierField.text)
Controller.createDirectChat(connection, identifierField.text)
}
}
}
@@ -157,7 +156,7 @@ Dialog {
circular: true
onClicked: {
controller.createDirectChat(connection, userID)
Controller.createDirectChat(connection, userID)
root.close()
}
}

View File

@@ -115,7 +115,7 @@ Kirigami.OverlayDrawer {
Layout.preferredHeight: Kirigami.Units.gridUnit
icon.name: "list-user-add"
onClicked: inviteUserDialog.createObject(ApplicationWindow.overlay, {"controller": spectralController, "room": room}).open()
onClicked: inviteUserDialog.createObject(ApplicationWindow.overlay, {"room": room}).open()
}
}

View File

@@ -8,6 +8,8 @@ import QtQuick 2.12
import QtQuick.Controls 2.12 as QQC2
import QtQuick.Layouts 1.12
import Spectral 0.1
import Spectral.Component 2.0
import org.kde.kirigami 2.12 as Kirigami
@@ -17,8 +19,6 @@ Kirigami.ScrollablePage {
title: i18n("Login")
required property var spectralController
Kirigami.FormLayout {
id: formLayout
QQC2.TextField {
@@ -55,9 +55,9 @@ Kirigami.ScrollablePage {
function doLogin() {
if (accessTokenField.text.length > 0) {
spectralController.loginWithAccessToken(serverField.text, usernameField.text, accessTokenField.text, deviceNameField.text)
Controller.loginWithAccessToken(serverField.text, usernameField.text, accessTokenField.text, deviceNameField.text)
} else {
spectralController.loginWithCredentials(serverField.text, usernameField.text, passwordField.text, deviceNameField.text)
Controller.loginWithCredentials(serverField.text, usernameField.text, passwordField.text, deviceNameField.text)
}
}
}

View File

@@ -46,42 +46,36 @@ Kirigami.ApplicationWindow {
}
}
Controller {
id: spectralController
quitOnLastWindowClosed: true
onErrorOccured: showPassiveNotification(error + ": " + detail)
Connections {
target: Controller
onInitiated: {
if (spectralController.accountCount === 0) {
pageStack.replace("qrc:/qml/LoginPage.qml", {
'spectralController': spectralController
});
if (Controller.accountCount === 0) {
pageStack.replace("qrc:/qml/LoginPage.qml", {});
} else {
pageStack.replace(roomListComponent);
}
}
onConnectionAdded: {
if (spectralController.accountCount === 1) {
if (Controller.accountCount === 1) {
console.log("roomListComponent")
pageStack.replace(roomListComponent);
}
}
onErrorOccured: showPassiveNotification(error + ": " + detail)
}
Binding {
target: imageProvider
property: "connection"
value: spectralController.connection
value: Controller.connection
}
RoomListModel {
id: spectralRoomListModel
connection: spectralController.connection
connection: Controller.connection
}
Component {

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");