Skip to content

Commit ddd56ca

Browse files
committed
MOBILE-1499 chat: Show reconnect button if WS calls fail
1 parent 3d9db80 commit ddd56ca

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

www/addons/mod_chat/controllers/chat.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ angular.module('mm.addons.mod_chat')
2929
var chatId = $stateParams.chatid,
3030
courseId = $stateParams.courseid,
3131
title = $stateParams.title,
32-
polling,
3332
chatLastTime = 0,
3433
pollingRunning = false;
3534

@@ -115,12 +114,23 @@ angular.module('mm.addons.mod_chat')
115114
return $q.reject();
116115
}
117116

117+
// Start the polling to get chat messages periodically.
118+
function startPolling() {
119+
// We already have the polling in place.
120+
if ($scope.polling) {
121+
return;
122+
}
123+
124+
// Start polling.
125+
$scope.polling = $interval(getMessagesInterval, mmaChatPollInterval);
126+
}
127+
118128
// Convenience function to be called every certain time to get chat messages.
119129
function getMessagesInterval() {
120130
$log.debug('Polling for messages');
121131
if (!$mmApp.isOnline() || pollingRunning) {
122132
// Obviously we cannot check for new messages when the app is offline.
123-
return;
133+
return $q.reject();
124134
}
125135

126136
pollingRunning = true;
@@ -130,8 +140,11 @@ angular.module('mm.addons.mod_chat')
130140
return loginUser().then(function() {
131141
return getMessages();
132142
}).catch(function(error) {
133-
// Fail again. Stop polling.
134-
$interval.cancel(polling);
143+
// Fail again. Stop polling if needed.
144+
if ($scope.polling) {
145+
$interval.cancel($scope.polling);
146+
$scope.polling = undefined;
147+
}
135148
return showError(error, 'mma.mod_chat.errorwhileretrievingmessages');
136149
});
137150
}).finally(function() {
@@ -176,9 +189,23 @@ angular.module('mm.addons.mod_chat')
176189
});
177190
};
178191

192+
$scope.reconnect = function() {
193+
var modal = $mmUtil.showModalLoading();
194+
195+
// Call startPolling would take a while for the first execution, so we'll execute it manually to check if it works now.
196+
return getMessagesInterval().then(function() {
197+
// It works, start the polling again.
198+
startPolling();
199+
}).finally(function() {
200+
modal.dismiss();
201+
});
202+
};
203+
179204
// Login the user.
180205
loginUser().then(function() {
181-
return getMessages().catch(function(error) {
206+
return getMessages().then(function() {
207+
startPolling();
208+
}).catch(function(error) {
182209
return showError(error, 'mma.mod_chat.errorwhileretrievingmessages');
183210
});
184211
}, function(error) {
@@ -199,22 +226,11 @@ angular.module('mm.addons.mod_chat')
199226
}
200227
};
201228

202-
// Set up the polling on a view enter, this allows for the user to go back and resume the polling.
203-
$scope.$on('$ionicView.enter', function() {
204-
// Strange case, we already have the polling in place.
205-
if (polling) {
206-
return;
207-
}
208-
209-
// Start polling.
210-
polling = $interval(getMessagesInterval, mmaChatPollInterval);
211-
});
212-
213229
// Removing the polling as we leave the page.
214230
$scope.$on('$ionicView.leave', function() {
215-
if (polling) {
231+
if ($scope.polling) {
216232
$log.debug('Cancelling polling for conversation');
217-
$interval.cancel(polling);
233+
$interval.cancel($scope.polling);
218234
}
219235
});
220236

www/addons/mod_chat/scss/styles.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ $mma-chat-notice-badge: "stable" !default;
2525
margin: 0;
2626
width: 100%;
2727
}
28+
29+
.mma-chat-send-form {
30+
width: 100%;
31+
}
32+
33+
.mma-chat-reconnect-button, .button.mma-chat-reconnect-button {
34+
margin: 0;
35+
}

www/addons/mod_chat/templates/chat.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ <h2>{{ message.userfullname }}</h2>
5050

5151
<ion-footer-bar>
5252
<p class="mma-chat-footer-note" ng-if="isAppOffline()">{{ 'mma.mod_chat.mustbeonlinetosendmessages' | translate }}</p>
53-
<form ng-if="!isAppOffline()" ng-submit="sendMessage(newMessage.text);" style="width: 100%">
53+
<form ng-if="!isAppOffline() && polling && loaded" class="mma-chat-send-form" ng-submit="sendMessage(newMessage.text);">
5454
<div class="mma-chat-input-inset item-input-inset">
5555
<label class="item-input-wrapper">
5656
<input type="text" placeholder="{{ 'mma.mod_chat.entermessage' | translate }}" ng-model="newMessage.text" style="width: 100%;">
5757
</label>
5858
<button type="submit" class="button button-clear" ng-disabled="!newMessage.text">{{ 'mma.mod_chat.send' | translate}}</button>
5959
</div>
6060
</form>
61+
<button ng-if="!isAppOffline() && !polling && loaded" class="button button-block mma-chat-reconnect-button" ng-click="reconnect()">{{ 'mm.login.reconnect' | translate }}</button>
6162
</ion-footer-bar>

0 commit comments

Comments
 (0)