Change SwipeView to StackView and clean code.

This commit is contained in:
Black Hat 2018-03-01 19:15:04 +08:00
parent c3367543bf
commit 2ac0d0cd8b
7 changed files with 54 additions and 83 deletions

View File

@ -40,7 +40,6 @@ void Controller::logout() {
void Controller::connected() { void Controller::connected() {
setUserID(m_connection->userId()); setUserID(m_connection->userId());
setToken(m_connection->accessToken()); setToken(m_connection->accessToken());
roomListModel->init(m_connection);
resync(); resync();
setIsLogin(true); setIsLogin(true);
} }

View File

@ -18,6 +18,10 @@ class Controller : public QObject
Q_PROPERTY(bool isLogin READ getIsLogin WRITE setIsLogin NOTIFY isLoginChanged) Q_PROPERTY(bool isLogin READ getIsLogin WRITE setIsLogin NOTIFY isLoginChanged)
Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged) Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged)
Q_PROPERTY(QByteArray token READ getToken WRITE setToken NOTIFY tokenChanged) Q_PROPERTY(QByteArray token READ getToken WRITE setToken NOTIFY tokenChanged)
private:
QMatrixClient::Connection* m_connection = new QMatrixClient::Connection();
public: public:
explicit Controller(QObject *parent = nullptr); explicit Controller(QObject *parent = nullptr);
~Controller(); ~Controller();
@ -29,7 +33,7 @@ public:
// All the non-Q_INVOKABLE functions. // All the non-Q_INVOKABLE functions.
// All the Q_PROPERTYs. // All the Q_PROPERTYs.
RoomListModel *roomListModel = new RoomListModel(); RoomListModel* roomListModel = new RoomListModel(m_connection);
RoomListModel* getRoomListModel() { return roomListModel; } RoomListModel* getRoomListModel() { return roomListModel; }
bool isLogin = false; bool isLogin = false;
@ -60,8 +64,6 @@ public:
} }
private: private:
QMatrixClient::Connection *m_connection = new QMatrixClient::Connection();
void connected(); void connected();
void resync(); void resync();
void reconnect(); void reconnect();

View File

@ -5,19 +5,8 @@
#include "controller.h" #include "controller.h"
RoomListModel::RoomListModel(QObject *parent) RoomListModel::RoomListModel(QMatrixClient::Connection* m_connection) : m_connection(m_connection) {
{
}
RoomListModel::~RoomListModel() {
}
void RoomListModel::init(QMatrixClient::Connection *conn) {
qDebug() << "Registering connection.";
beginResetModel(); beginResetModel();
m_connection = conn;
m_rooms.clear(); m_rooms.clear();
connect(m_connection, &QMatrixClient::Connection::newRoom, this, &RoomListModel::addRoom); connect(m_connection, &QMatrixClient::Connection::newRoom, this, &RoomListModel::addRoom);
for(QMatrixClient::Room* room: m_connection->roomMap().values()) { for(QMatrixClient::Room* room: m_connection->roomMap().values()) {
@ -27,13 +16,15 @@ void RoomListModel::init(QMatrixClient::Connection *conn) {
endResetModel(); endResetModel();
} }
QMatrixClient::Room* RoomListModel::roomAt(int row) RoomListModel::~RoomListModel() {
{
}
QMatrixClient::Room* RoomListModel::roomAt(int row) {
return m_rooms.at(row); return m_rooms.at(row);
} }
void RoomListModel::addRoom(QMatrixClient::Room* room) void RoomListModel::addRoom(QMatrixClient::Room* room) {
{
qDebug() << "Adding room."; qDebug() << "Adding room.";
beginInsertRows(QModelIndex(), m_rooms.count(), m_rooms.count()); beginInsertRows(QModelIndex(), m_rooms.count(), m_rooms.count());
connect(room, &QMatrixClient::Room::namesChanged, this, &RoomListModel::namesChanged ); connect(room, &QMatrixClient::Room::namesChanged, this, &RoomListModel::namesChanged );
@ -41,32 +32,30 @@ void RoomListModel::addRoom(QMatrixClient::Room* room)
endInsertRows(); endInsertRows();
} }
int RoomListModel::rowCount(const QModelIndex& parent) const int RoomListModel::rowCount(const QModelIndex& parent) const {
{
if( parent.isValid() ) if( parent.isValid() )
return 0; return 0;
return m_rooms.count(); return m_rooms.count();
} }
QVariant RoomListModel::data(const QModelIndex& index, int role) const QVariant RoomListModel::data(const QModelIndex& index, int role) const {
{
if(!index.isValid()) if(!index.isValid())
return QVariant(); return QVariant();
if(index.row() >= m_rooms.count()) if(index.row() >= m_rooms.count()) {
{
qDebug() << "UserListModel: something wrong here..."; qDebug() << "UserListModel: something wrong here...";
return QVariant(); return QVariant();
} }
QMatrixClient::Room* room = m_rooms.at(index.row()); QMatrixClient::Room* room = m_rooms.at(index.row());
if( role == NameRole ) if(role == NameRole) {
{
return room->displayName(); return room->displayName();
} }
if( role == ValueRole ) if(role == ValueRole) {
{
return room->topic(); return room->topic();
} }
if(role == IconRole) {
return room->avatar(48);
}
return QVariant(); return QVariant();
} }
@ -74,17 +63,16 @@ QHash<int, QByteArray> RoomListModel::roleNames() const {
QHash<int, QByteArray> roles; QHash<int, QByteArray> roles;
roles[NameRole] = "name"; roles[NameRole] = "name";
roles[ValueRole] = "value"; roles[ValueRole] = "value";
roles[IconRole] = "icon";
return roles; return roles;
} }
void RoomListModel::namesChanged(QMatrixClient::Room* room) void RoomListModel::namesChanged(QMatrixClient::Room* room) {
{
int row = m_rooms.indexOf(room); int row = m_rooms.indexOf(room);
emit dataChanged(index(row), index(row)); emit dataChanged(index(row), index(row));
} }
void RoomListModel::unreadMessagesChanged(QMatrixClient::Room* room) void RoomListModel::unreadMessagesChanged(QMatrixClient::Room* room) {
{
} }

View File

@ -39,17 +39,15 @@ class RoomListModel : public QAbstractListModel
Q_OBJECT Q_OBJECT
public: public:
explicit RoomListModel(QObject *parent = nullptr); explicit RoomListModel(QMatrixClient::Connection* m_connection = 0);
~RoomListModel(); ~RoomListModel();
enum RoomModelRoles { enum RoomModelRoles {
NameRole, ValueRole NameRole, ValueRole, IconRole
}; };
QHash<int, QByteArray> roleNames() const; QHash<int, QByteArray> roleNames() const;
void init(QMatrixClient::Connection*);
Q_INVOKABLE QMatrixClient::Room* roomAt(int row); Q_INVOKABLE QMatrixClient::Room* roomAt(int row);
QVariant data(const QModelIndex& index, int role) const override; QVariant data(const QModelIndex& index, int role) const override;
@ -65,7 +63,7 @@ private slots:
void addRoom(QMatrixClient::Room* room); void addRoom(QMatrixClient::Room* room);
private: private:
QMatrixClient::Connection *m_connection; QMatrixClient::Connection* m_connection;
QList<QMatrixClient::Room*> m_rooms; QList<QMatrixClient::Room*> m_rooms;
}; };

View File

@ -7,7 +7,6 @@ import Qt.labs.settings 1.0
import "qrc:/qml/component" import "qrc:/qml/component"
Page { Page {
property var window
property var controller property var controller
property alias homeserver: settings.server property alias homeserver: settings.server

View File

@ -4,7 +4,7 @@ import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.3 import QtQuick.Controls.Material 2.3
Item { Item {
property int index property Item page
property alias contentItem: itemDelegate.contentItem property alias contentItem: itemDelegate.contentItem
signal clicked signal clicked
@ -14,13 +14,13 @@ Item {
Layout.preferredHeight: width Layout.preferredHeight: width
Rectangle { Rectangle {
width: swipeView.currentIndex === index ? parent.width : 0 width: stackView.currentItem === page ? parent.width : 0
height: parent.height height: parent.height
anchors.bottom: itemDelegate.bottom anchors.bottom: itemDelegate.bottom
color: Qt.lighter(Material.accent) color: Qt.lighter(Material.accent)
Behavior on width { Behavior on width {
PropertyAnimation { easing.type: Easing.InOutQuad; duration: 200 } PropertyAnimation { easing.type: Easing.InOutCubic; duration: 200 }
} }
} }
@ -29,7 +29,14 @@ Item {
anchors.fill: parent anchors.fill: parent
onClicked: { onClicked: {
swipeView.currentIndex = index if(page != null) {
if(stackView.depth === 1) {
stackView.replace(page)
} else {
stackView.clear()
stackView.push(page)
}
}
buttonDelegate.clicked() buttonDelegate.clicked()
} }
} }

View File

@ -42,14 +42,17 @@ ApplicationWindow {
spacing: 0 spacing: 0
ButtonDelegate { ButtonDelegate {
index: 0
contentItem: ImageStatus { contentItem: ImageStatus {
width: parent.width width: parent.width
height: parent.width height: parent.width
source: "qrc:/asset/img/avatar.png" source: "qrc:/asset/img/avatar.png"
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
} }
page: Room {
id: roomPage
roomListModel: controller.roomListModel
}
} }
Rectangle { Rectangle {
@ -58,8 +61,6 @@ ApplicationWindow {
} }
ButtonDelegate { ButtonDelegate {
index: 1
contentItem: Text { contentItem: Text {
text: "\ue853" text: "\ue853"
font.pointSize: 16 font.pointSize: 16
@ -68,24 +69,14 @@ ApplicationWindow {
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter verticalAlignment: Text.AlignVCenter
} }
page: Login {
id: loginPage
controller: controller
}
} }
// ButtonDelegate {
// index: 2
// contentItem: Text {
// text: "\ue5d2"
// font.pointSize: 16
// font.family: materialFont.name
// color: "white"
// horizontalAlignment: Text.AlignHCenter
// verticalAlignment: Text.AlignVCenter
// }
// }
ButtonDelegate { ButtonDelegate {
index: 2
contentItem: Text { contentItem: Text {
text: "\ue8b8" text: "\ue8b8"
font.pointSize: 16 font.pointSize: 16
@ -94,11 +85,13 @@ ApplicationWindow {
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter verticalAlignment: Text.AlignVCenter
} }
page: Setting {
}
} }
ButtonDelegate { ButtonDelegate {
index: 3
contentItem: Text { contentItem: Text {
text: "\ue879" text: "\ue879"
font.pointSize: 16 font.pointSize: 16
@ -113,25 +106,10 @@ ApplicationWindow {
} }
} }
SwipeView { StackView {
id: swipeView id: stackView
anchors.fill: parent anchors.fill: parent
anchors.leftMargin: sideNav.width anchors.leftMargin: sideNav.width
interactive: false initialItem: roomPage
orientation: Qt.Vertical
Room {
roomListModel: controller.roomListModel
}
Login {
id: loginPage
window: window
controller: controller
}
Setting {
}
} }
} }