Browse Source

Added message drafts saved in localStorage

master
Igor Zhukov 11 years ago
parent
commit
7a62a2213c
  1. 6
      app/index.html
  2. 29
      app/js/controllers.js
  3. 9
      app/js/directives.js
  4. 36
      app/js/lib/mtproto.js

6
app/index.html

@ -37,14 +37,14 @@
<script type="text/javascript" src="js/lib/config.js"></script> <script type="text/javascript" src="js/lib/config.js"></script>
<script type="text/javascript" src="js/lib/mtproto.js?10"></script> <script type="text/javascript" src="js/lib/mtproto.js?11"></script>
<script type="text/javascript" src="js/util.js"></script> <script type="text/javascript" src="js/util.js"></script>
<script type="text/javascript" src="js/app.js?5"></script> <script type="text/javascript" src="js/app.js?5"></script>
<script type="text/javascript" src="js/services.js?6"></script> <script type="text/javascript" src="js/services.js?6"></script>
<script type="text/javascript" src="js/controllers.js?10"></script> <script type="text/javascript" src="js/controllers.js?11"></script>
<script type="text/javascript" src="js/filters.js?3"></script> <script type="text/javascript" src="js/filters.js?3"></script>
<script type="text/javascript" src="js/directives.js?6"></script> <script type="text/javascript" src="js/directives.js?7"></script>
</body> </body>
</html> </html>

29
app/js/controllers.js

@ -329,6 +329,7 @@ angular.module('myApp.controllers', [])
$scope.$broadcast('ui_history_change'); $scope.$broadcast('ui_history_change');
AppMessagesManager.readHistory($scope.curDialog.inputPeer); AppMessagesManager.readHistory($scope.curDialog.inputPeer);
}, function () { }, function () {
$scope.state = {error: true}; $scope.state = {error: true};
}); });
@ -408,7 +409,7 @@ angular.module('myApp.controllers', [])
$scope.$on('user_update', angular.noop); $scope.$on('user_update', angular.noop);
}) })
.controller('AppImSendController', function ($scope, $timeout, MtpApiManager, AppPeersManager, AppMessagesManager, ApiUpdatesManager, MtpApiFileManager) { .controller('AppImSendController', function ($scope, $timeout, MtpApiManager, AppConfigManager, AppPeersManager, AppMessagesManager, ApiUpdatesManager, MtpApiFileManager) {
$scope.$watch('curDialog.peer', resetDraft); $scope.$watch('curDialog.peer', resetDraft);
$scope.$on('user_update', angular.noop); $scope.$on('user_update', angular.noop);
@ -419,6 +420,16 @@ angular.module('myApp.controllers', [])
$scope.$watch('draftMessage.text', function (newVal) { $scope.$watch('draftMessage.text', function (newVal) {
AppMessagesManager.readHistory($scope.curDialog.inputPeer); AppMessagesManager.readHistory($scope.curDialog.inputPeer);
if (newVal.length) {
var backupDraftObj = {};
backupDraftObj['draft' + $scope.curDialog.peerID] = newVal;
AppConfigManager.set(backupDraftObj);
// dLog('draft save', backupDraftObj);
} else {
AppConfigManager.remove('draft' + $scope.curDialog.peerID);
// dLog('draft delete', 'draft' + $scope.curDialog.peerID);
}
var now = +new Date(); var now = +new Date();
if (newVal === undefined || !newVal.length || now - lastTyping < 6000) { if (newVal === undefined || !newVal.length || now - lastTyping < 6000) {
return; return;
@ -461,10 +472,18 @@ angular.module('myApp.controllers', [])
} }
function resetDraft () { function resetDraft (newPeer) {
$scope.draftMessage = { if (newPeer) {
text: '' AppConfigManager.get('draft' + $scope.curDialog.peerID).then(function (draftText) {
}; // dLog('Restore draft', 'draft' + $scope.curDialog.peerID, draftText);
$scope.draftMessage.text = draftText || '';
// dLog('send broadcast', $scope.draftMessage);
$scope.$broadcast('ui_peer_draft');
});
} else {
// dLog('Reset peer');
$scope.draftMessage.text = '';
}
} }
function onFilesSelected (newVal) { function onFilesSelected (newVal) {

9
app/js/directives.js

@ -282,20 +282,27 @@ angular.module('myApp.directives', ['myApp.filters'])
if (richTextarea) { if (richTextarea) {
scope.$watch('draftMessage.text', function (newVal) { scope.$watch('draftMessage.text', function (newVal) {
dLog('on update', newVal);
if (!newVal.length && !messageField.value.length) { if (!newVal.length && !messageField.value.length) {
$timeout(function () { $timeout(function () {
$(richTextarea).html(''); updateField();
}, 0); }, 0);
} }
}); });
} }
function updateField () {
dLog(scope.draftMessage);
dLog('update field', scope.draftMessage.text);
$(richTextarea).text(scope.draftMessage.text || '');
}
$('body').on('dragenter dragleave dragover drop', onDragDropEvent); $('body').on('dragenter dragleave dragover drop', onDragDropEvent);
scope.$on('ui_peer_change', focusField); scope.$on('ui_peer_change', focusField);
scope.$on('ui_history_change', focusField); scope.$on('ui_history_change', focusField);
scope.$on('ui_message_send', focusField); scope.$on('ui_message_send', focusField);
scope.$on('ui_peer_draft', updateField);
scope.$on('$destroy', function cleanup() { scope.$on('$destroy', function cleanup() {
$('body').off('dragenter dragleave dragover drop', onDragDropEvent); $('body').off('dragenter dragleave dragover drop', onDragDropEvent);

36
app/js/lib/mtproto.js

@ -351,7 +351,7 @@ function TLSerialization (options) {
this.createBuffer(); this.createBuffer();
this.debug = options.debug !== undefined ? options.debug : window._debugMode; // this.debug = options.debug !== undefined ? options.debug : window._debugMode;
this.mtproto = options.mtproto || false; this.mtproto = options.mtproto || false;
return this; return this;
} }
@ -631,7 +631,7 @@ function TLDeserialization (buffer, options) {
this.intView = new Uint32Array(this.buffer); this.intView = new Uint32Array(this.buffer);
this.byteView = new Uint8Array(this.buffer); this.byteView = new Uint8Array(this.buffer);
this.debug = options.debug !== undefined ? options.debug : window._debugMode; // this.debug = options.debug !== undefined ? options.debug : window._debugMode;
this.mtproto = options.mtproto || false; this.mtproto = options.mtproto || false;
return this; return this;
} }
@ -1021,7 +1021,7 @@ factory('MtpMessageIdGenerator', function (AppConfigManager) {
}); });
function generateMessageID () { function generateMessageID () {
var timeTicks = +new Date() + timeOffset, var timeTicks = +new Date() + (timeOffset * 1000),
timeSec = Math.floor(timeTicks / 1000), timeSec = Math.floor(timeTicks / 1000),
timeMSec = timeTicks % 1000, timeMSec = timeTicks % 1000,
random = nextRandomInt(0xFFFF); random = nextRandomInt(0xFFFF);
@ -1042,6 +1042,7 @@ factory('MtpMessageIdGenerator', function (AppConfigManager) {
function applyServerTime (serverTime, localTime) { function applyServerTime (serverTime, localTime) {
timeOffset = serverTime - Math.floor((localTime || +new Date()) / 1000); timeOffset = serverTime - Math.floor((localTime || +new Date()) / 1000);
dLog('Apply server time', serverTime, localTime, timeOffset);
AppConfigManager.set({server_time_offset: timeOffset}); AppConfigManager.set({server_time_offset: timeOffset});
}; };
@ -1787,7 +1788,9 @@ factory('MtpNetworkerFactory', function (MtpDcConfigurator, MtpMessageIdGenerato
this.sentMessages[message.msg_id] = containerSentMessage; this.sentMessages[message.msg_id] = containerSentMessage;
// dLog('Container', innerMessages, message.msg_id, message.seq_no); if (window._debugMode) {
dLog('Container', innerMessages, message.msg_id, message.seq_no);
}
} else { } else {
if (message.noResponse) { if (message.noResponse) {
noResponseMsgs.push(message.msg_id); noResponseMsgs.push(message.msg_id);
@ -2035,7 +2038,7 @@ factory('MtpNetworkerFactory', function (MtpDcConfigurator, MtpMessageIdGenerato
break; break;
case 'bad_server_salt': case 'bad_server_salt':
dLog('bad server salt', message); dLog('Bad server salt', message);
var sentMsg = this.sentMessages[message.bad_msg_id]; var sentMsg = this.sentMessages[message.bad_msg_id];
if (!sentMsg || sentMsg.seq_no != message.bad_msg_seqno) { if (!sentMsg || sentMsg.seq_no != message.bad_msg_seqno) {
dLog(message.bad_msg_id, message.bad_msg_seqno); dLog(message.bad_msg_id, message.bad_msg_seqno);
@ -2047,6 +2050,21 @@ factory('MtpNetworkerFactory', function (MtpDcConfigurator, MtpMessageIdGenerato
this.ackMessage(messageID); this.ackMessage(messageID);
break; break;
case 'bad_msg_notification':
dLog('Bad msg notification', message);
var sentMsg = this.sentMessages[message.bad_msg_id];
if (!sentMsg || sentMsg.seq_no != message.bad_msg_seqno) {
dLog(message.bad_msg_id, message.bad_msg_seqno);
throw new Error('Bad msg notification for invalid message');
}
if (message.error_code == 16 || message.error_code == 17) {
MtpMessageIdGenerator.applyServerTime((new BigInteger(messageID, 10)).shiftRight(32).toString(10));
this.pushResend(message.bad_msg_id);
this.ackMessage(messageID);
}
break;
case 'message': case 'message':
this.serverMessages.push(message.msg_id); this.serverMessages.push(message.msg_id);
this.processMessage(message.body, message.msg_id, sessionID); this.processMessage(message.body, message.msg_id, sessionID);
@ -2439,7 +2457,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
cacheFileWriter, cacheFileWriter,
errorHandler = function (error) { errorHandler = function (error) {
deferred.reject(error); deferred.reject(error);
if (cacheFileWriter) cacheFileWriter.truncate(); if (cacheFileWriter) cacheFileWriter.truncate(0);
errorHandler = angular.noop; errorHandler = angular.noop;
}; };
@ -2474,7 +2492,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
cacheFileWriter, cacheFileWriter,
errorHandler = function (error) { errorHandler = function (error) {
deferred.reject(error); deferred.reject(error);
if (cacheFileWriter) cacheFileWriter.truncate(); if (cacheFileWriter) cacheFileWriter.truncate(0);
errorHandler = angular.noop; errorHandler = angular.noop;
}, },
doDownload = function () { doDownload = function () {
@ -2543,7 +2561,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
console.error(error); console.error(error);
// dLog('fail'); // dLog('fail');
deferred.reject(error); deferred.reject(error);
if (cacheFileWriter) cacheFileWriter.truncate(); if (cacheFileWriter) cacheFileWriter.truncate(0);
errorHandler = angular.noop; errorHandler = angular.noop;
}, },
saveToFileEntry = function (fileEntry) { saveToFileEntry = function (fileEntry) {
@ -2685,7 +2703,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
errorHandler = function (error) { errorHandler = function (error) {
dLog('fail'); dLog('fail');
deferred.reject(error); deferred.reject(error);
if (cacheFileWriter) cacheFileWriter.truncate(); if (cacheFileWriter) cacheFileWriter.truncate(0);
errorHandler = angular.noop; errorHandler = angular.noop;
}; };

Loading…
Cancel
Save