Skip to content

Commit

Permalink
Add connection status indicators
Browse files Browse the repository at this point in the history
Little dot next to team name on the team list view and the cover page
  • Loading branch information
danvratil committed Apr 15, 2019
1 parent 383441e commit 458a7d0
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 58 deletions.
29 changes: 25 additions & 4 deletions qml/cover/CoverPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,32 @@ CoverBackground {

Column {
width: parent.width
Label {
color: Theme.highlightColor
Row {
width: parent.width
text: model.client.config.teamName
truncationMode: TruncationMode.Fade

Label {
color: Theme.highlightColor
width: parent.width - statusIndicator.width
text: model.client.config.teamName
truncationMode: TruncationMode.Fade
}
Rectangle {
id: statusIndicator
width: 15
anchors.verticalCenter: parent.verticalCenter
height: width
radius: width / 2
color: switch (model.client.connectionStatus) {
case Slack.Client.Connected: return "green"
case Slack.Client.Connecting: return "yellow"
case Slack.Client.Disconnected: return "red"
}
Behavior on color {
ColorAnimation {
duration: 200
}
}
}
}

Row {
Expand Down
42 changes: 6 additions & 36 deletions qml/pages/ConnectionPanel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ DockedPanel {

dock: Dock.Bottom

visible: slackClient.connectionStatus !== Client.Connected || !slackClient.networkAccessible

Column {
id: content
width: parent.width - Theme.paddingLarge * (Screen.sizeCategory >= Screen.Large ? 4 : 2)
Expand All @@ -30,58 +32,26 @@ DockedPanel {
}

Label {
visible: slackClient.connectionStatus === Client.Connecting
text: qsTr("Reconnecting")
}
}

Label {
id: disconnectedMessage
visible: slackClient.connectionStatus === Client.Disconnected || !slackClient.networkAccessible
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Disconnected")
text: slackClient.networkAccessible ? qsTr("Disconnected") : qsTr("No network connection")
}

Button {
id: reconnectButton
visible: slackClient.connectionStatus === Client.Disconnected && slackClient.networkAccessible
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Reconnect")
onClicked: {
slackClient.reconnect()
}
}
}

Component.onCompleted: {
slackClient.onConnected.connect(hideConnectionPanel)
slackClient.onReconnecting.connect(showReconnectingMessage)
slackClient.onDisconnected.connect(showDisconnectedMessage)
slackClient.onNetworkOff.connect(showNoNetworkMessage)
slackClient.onNetworkOn.connect(hideConnectionPanel)
}

function hideConnectionPanel() {
connectionPanel.hide()
}

function showReconnectingMessage() {
disconnectedMessage.visible = false
reconnectButton.visible = false
reconnectingMessage.visible = true
connectionPanel.show()
}

function showDisconnectedMessage() {
disconnectedMessage.text = qsTr("Disconnected")
disconnectedMessage.visible = true
reconnectButton.visible = true
reconnectingMessage.visible = false
connectionPanel.show()
}

function showNoNetworkMessage() {
disconnectedMessage.text = qsTr("No network connection")
disconnectedMessage.visible = true
reconnectButton.visible = false
reconnectingMessage.visible = false
connectionPanel.show()
}
}
20 changes: 19 additions & 1 deletion qml/pages/TeamList.qml
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,32 @@ Page {
}

Label {
width: parent.width - icon.width - Theme.paddingMedium
width: parent.width - icon.width - Theme.paddingMedium - statusIcon.width
wrapMode: Text.Wrap
anchors.verticalCenter: parent.verticalCenter
font.pixelSize: Theme.fontSizeMedium
font.bold: model.client.unreadCount > 0
text: model.client.config.teamName
color: currentColor
}

Rectangle {
id: statusIcon
width: 20
height: width
anchors.verticalCenter: parent.verticalCenter
radius: width / 2
color: switch (model.client.connectionStatus) {
case Slack.Client.Connected: return "green"
case Slack.Client.Connecting: return "yellow"
case Slack.Client.Disconnected: return "red"
}
Behavior on color {
ColorAnimation {
duration: 200
}
}
}
}
onClicked: {
console.log("Opening team " + model.client.config.teamName)
Expand Down
28 changes: 19 additions & 9 deletions src/slackclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,29 @@ void SlackClient::handleNetworkAccessibleChanged(QNetworkAccessManager::NetworkA
networkAccessible = accessible;

if (networkAccessible == QNetworkAccessManager::Accessible) {
emit networkOn();
Q_EMIT networkAccessibleChanged(true);
} else {
Q_EMIT networkAccessibleChanged(false);
}
else {
emit networkOff();
}

void SlackClient::setConnectionStatus(ConnectionStatus status) {
if (connectionStatus != status) {
connectionStatus = status;
Q_EMIT connectionStatusChanged(connectionStatus);
}
}

void SlackClient::reconnect() {
qDebug() << config->getTeamName() << ": Reconnecting";
emit reconnecting();
setConnectionStatus(Connecting);

start();
}

void SlackClient::handleStreamStart() {
qDebug() << config->getTeamName() << ": Stream started";
emit connected();
setConnectionStatus(Connected);

updatePresenceSubscription();
}
Expand Down Expand Up @@ -145,9 +152,11 @@ void SlackClient::handleStreamEnd() {

if (!config->getAccessToken().isEmpty()) {
qDebug() << config->getTeamName() << ": Stream reconnect scheduled";
emit reconnecting();
setConnectionStatus(Connecting);
reconnectTimer->setSingleShot(true);
reconnectTimer->start(1000);
} else {
setConnectionStatus(Disconnected);
}
}

Expand Down Expand Up @@ -442,8 +451,9 @@ void SlackClient::handleTestLoginReply() {
}

void SlackClient::init() {
qDebug() << config->getTeamName() << ": Start init";
loadUsers();
setConnectionStatus(Connecting);
qDebug() << config->getTeamName() << ": Start init";
loadUsers();
}

void SlackClient::loadUsers(const QString &cursor) {
Expand Down Expand Up @@ -485,7 +495,7 @@ void SlackClient::start() {

if (Request::isError(data)) {
qDebug() << config->getTeamName() << ": Connect result error";
emit disconnected();
setConnectionStatus(Disconnected);
emit initFail();
}
else {
Expand Down
27 changes: 19 additions & 8 deletions src/slackclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ class SlackClient : public QObject
Q_PROPERTY(SlackClientConfig* config READ getConfig CONSTANT)
Q_PROPERTY(QString team READ getTeam CONSTANT)
Q_PROPERTY(bool initialized READ getInitialized NOTIFY initializedChanged)

Q_PROPERTY(ConnectionStatus connectionStatus READ getConnectionStatus NOTIFY connectionStatusChanged)
Q_PROPERTY(bool networkAccessible READ getNetworkAccessible NOTIFY networkAccessibleChanged)
Q_PROPERTY(int unreadCount READ getUnreadCount NOTIFY unreadCountChanged)

public:
enum ConnectionStatus {
Disconnected,
Connecting,
Connected
};
Q_ENUM(ConnectionStatus)

explicit SlackClient(QObject *parent = 0); // only to make qmlRegisterType happy, don't use
explicit SlackClient(const QString &team, QObject *parent = 0);
~SlackClient() override;
Expand All @@ -44,6 +52,10 @@ class SlackClient : public QObject
QString getTeam() const;

int getUnreadCount() const { return unreadCount; }

ConnectionStatus getConnectionStatus() const { return connectionStatus; }
bool getNetworkAccessible() const { return networkAccessible == QNetworkAccessManager::Accessible; }

signals:
void teamChanged();

Expand Down Expand Up @@ -78,12 +90,8 @@ class SlackClient : public QObject
void postImageSuccess();
void postImageFail();

void networkOff();
void networkOn();

void reconnecting();
void disconnected();
void connected();
void connectionStatusChanged(SlackClient::ConnectionStatus status);
void networkAccessibleChanged(bool hasNetwork);

void unreadCountChanged(int count);

Expand Down Expand Up @@ -125,6 +133,8 @@ public slots:
bool appActive;
QString activeWindow;

void setConnectionStatus(ConnectionStatus status);

QNetworkReply* executePost(QString method, const QMap<QString, QString> &data);
QNetworkReply *executePostWithFile(QString method, const QMap<QString, QString>&, QFile *file);

Expand Down Expand Up @@ -178,9 +188,10 @@ public slots:
Storage storage;
MessageFormatter messageFormatter;

QNetworkAccessManager::NetworkAccessibility networkAccessible;
QNetworkAccessManager::NetworkAccessibility networkAccessible = QNetworkAccessManager::UnknownAccessibility;

bool initialized = false;
ConnectionStatus connectionStatus = Disconnected;
int unreadCount = 0;
};

Expand Down

0 comments on commit 458a7d0

Please sign in to comment.