Skip to content

Commit 05b5c1a

Browse files
committed
Merge pull request #446 from dpalou/MOBILE-1499
Mobile 1499
2 parents 32e32df + ddd56ca commit 05b5c1a

File tree

4 files changed

+95
-48
lines changed

4 files changed

+95
-48
lines changed

www/addons/mod_chat/controllers/chat.js

Lines changed: 80 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ angular.module('mm.addons.mod_chat')
2222
* @name mmaModChatChatCtrl
2323
*/
2424
.controller('mmaModChatChatCtrl', function($scope, $stateParams, $mmApp, $mmaModChat, $log, $ionicModal, $mmUtil, $ionicHistory,
25-
$ionicScrollDelegate, $timeout, $mmSite, $interval, mmaChatPollInterval) {
25+
$ionicScrollDelegate, $timeout, $mmSite, $interval, mmaChatPollInterval, $q) {
2626

2727
$log = $log.getInstance('mmaModChatChatCtrl');
2828

2929
var chatId = $stateParams.chatid,
3030
courseId = $stateParams.courseid,
3131
title = $stateParams.title,
32-
polling;
32+
chatLastTime = 0,
33+
pollingRunning = false;
3334

3435
$scope.loaded = false;
3536
$scope.title = title;
@@ -42,7 +43,6 @@ angular.module('mm.addons.mod_chat')
4243
$scope.newMessage = {
4344
text: ''
4445
};
45-
chatLastTime = 0;
4646

4747
// Chat users modal.
4848
$ionicModal.fromTemplateUrl('addons/mod_chat/templates/users.html', {
@@ -87,13 +87,69 @@ angular.module('mm.addons.mod_chat')
8787
return !$mmApp.isOnline();
8888
};
8989

90+
// Convenience function to login the user.
91+
function loginUser() {
92+
return $mmaModChat.loginUser(chatId).then(function(chatsid) {
93+
$scope.chatsid = chatsid;
94+
});
95+
}
96+
97+
// Convenience function to get chat messages.
98+
function getMessages() {
99+
return $mmaModChat.getLatestMessages($scope.chatsid, chatLastTime).then(function(messagesInfo) {
100+
chatLastTime = messagesInfo.chatnewlasttime;
101+
return $mmaModChat.getMessagesUserData(messagesInfo.messages, courseId).then(function(messages) {
102+
$scope.messages = $scope.messages.concat(messages);
103+
});
104+
});
105+
}
106+
90107
// Show error modal.
91108
function showError(error, defaultMessage) {
92109
if (typeof error === 'string') {
93110
$mmUtil.showErrorModal(error);
94111
} else {
95112
$mmUtil.showErrorModal(defaultMessage, true);
96113
}
114+
return $q.reject();
115+
}
116+
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+
128+
// Convenience function to be called every certain time to get chat messages.
129+
function getMessagesInterval() {
130+
$log.debug('Polling for messages');
131+
if (!$mmApp.isOnline() || pollingRunning) {
132+
// Obviously we cannot check for new messages when the app is offline.
133+
return $q.reject();
134+
}
135+
136+
pollingRunning = true;
137+
138+
return getMessages().catch(function() {
139+
// Try to login, it might have failed because the session expired.
140+
return loginUser().then(function() {
141+
return getMessages();
142+
}).catch(function(error) {
143+
// Fail again. Stop polling if needed.
144+
if ($scope.polling) {
145+
$interval.cancel($scope.polling);
146+
$scope.polling = undefined;
147+
}
148+
return showError(error, 'mma.mod_chat.errorwhileretrievingmessages');
149+
});
150+
}).finally(function() {
151+
pollingRunning = false;
152+
});
97153
}
98154

99155
// Check if the date should be displayed between messages (when the day changes at midnight for example).
@@ -123,6 +179,7 @@ angular.module('mm.addons.mod_chat')
123179
if (beep === '') {
124180
$scope.newMessage.text = '';
125181
}
182+
getMessagesInterval(); // Update messages to show the sent message.
126183
}, function(error) {
127184
// Only close the keyboard if an error happens, we want the user to be able to send multiple
128185
// messages withoutthe keyboard being closed.
@@ -132,16 +189,24 @@ angular.module('mm.addons.mod_chat')
132189
});
133190
};
134191

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+
135204
// Login the user.
136-
$mmaModChat.loginUser(chatId).then(function(chatsid) {
137-
return $mmaModChat.getLatestMessages(chatsid, 0).then(function(messagesInfo) {
138-
$scope.chatsid = chatsid;
139-
chatLastTime = messagesInfo.chatnewlasttime;
140-
return $mmaModChat.getMessagesUserData(messagesInfo.messages, courseId).then(function(messages) {
141-
$scope.messages = $scope.messages.concat(messages);
142-
});
143-
}).catch(function(message) {
144-
showError(message, 'mma.mod_chat.errorwhileretrievingmessages');
205+
loginUser().then(function() {
206+
return getMessages().then(function() {
207+
startPolling();
208+
}).catch(function(error) {
209+
return showError(error, 'mma.mod_chat.errorwhileretrievingmessages');
145210
});
146211
}, function(error) {
147212
showError(error, 'mma.mod_chat.errorwhileconnecting');
@@ -161,39 +226,11 @@ angular.module('mm.addons.mod_chat')
161226
}
162227
};
163228

164-
// Set up the polling on a view enter, this allows for the user to go back and resume the polling.
165-
$scope.$on('$ionicView.enter', function() {
166-
// Strange case, we already have the polling in place.
167-
if (polling) {
168-
return;
169-
}
170-
171-
// Start polling.
172-
polling = $interval(function() {
173-
$log.debug('Polling for messages');
174-
if (!$mmApp.isOnline()) {
175-
// Obviously we cannot check for new messages when the app is offline.
176-
return;
177-
}
178-
179-
$mmaModChat.getLatestMessages($scope.chatsid, chatLastTime).then(function(data) {
180-
chatLastTime = data.chatnewlasttime;
181-
$mmaModChat.getMessagesUserData(data.messages, courseId).then(function(messages) {
182-
$scope.messages = $scope.messages.concat(messages);
183-
});
184-
}, function(error) {
185-
$interval.cancel(polling);
186-
showError(error, 'mma.mod_chat.errorwhileretrievingmessages');
187-
});
188-
189-
}, mmaChatPollInterval);
190-
});
191-
192229
// Removing the polling as we leave the page.
193-
$scope.$on('$ionicView.leave', function(e) {
194-
if (polling) {
230+
$scope.$on('$ionicView.leave', function() {
231+
if ($scope.polling) {
195232
$log.debug('Cancelling polling for conversation');
196-
$interval.cancel(polling);
233+
$interval.cancel($scope.polling);
197234
}
198235
});
199236

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/services/chat.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,13 @@ angular.module('mm.addons.mod_chat')
164164
var params = {
165165
chatsid: chatsid,
166166
chatlasttime: lasttime
167-
};
168-
var preSets = {
169-
getFromCache: false
167+
}, preSets = {
168+
emergencyCache: false
170169
};
171170

172-
return $mmSite.read('mod_chat_get_chat_latest_messages', params, preSets);
171+
// We use write to not use cache. It doesn't make sense to store the messages in cache
172+
// because we won't be able to retireve them if $mmaModChat#loginUser fails.
173+
return $mmSite.write('mod_chat_get_chat_latest_messages', params, preSets);
173174
};
174175

175176
/**

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)