From 25681537c717dfd696b4363c4dc900e7723bc5b2 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Mon, 11 Jul 2016 14:33:19 +0300 Subject: [PATCH] Improved drafts handling Fixed bug with isIdle in Fx --- app/js/controllers.js | 10 +++++++--- app/js/lib/ng_utils.js | 16 ++++++++++------ app/js/services.js | 16 ++++++++-------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/app/js/controllers.js b/app/js/controllers.js index 0595a252..6f3a781a 100644 --- a/app/js/controllers.js +++ b/app/js/controllers.js @@ -2225,12 +2225,16 @@ angular.module('myApp.controllers', ['myApp.i18n']) return } if (newVal && $scope.curDialog.peerID) { - DraftsManager.syncDraft($scope.curDialog.peerID) + $scope.$broadcast('ui_message_before_send') + $timeout(function () { + DraftsManager.syncDraft($scope.curDialog.peerID) + }) } }) $scope.$on('draft_updated', function (e, draftUpdate) { - if (draftUpdate.peerID == $scope.curDialog.peerID) { + if (draftUpdate.peerID == $scope.curDialog.peerID && + !draftUpdate.local) { getDraft() } }) @@ -2373,7 +2377,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) function resetDraft (newPeer, prevPeer) { var prevPeerID = prevPeer ? AppPeersManager.getPeerID(prevPeer) : 0 - if (prevPeerID) { + if (newPeer != prevPeer && prevPeerID) { $scope.$broadcast('ui_message_before_send') $timeout(function () { DraftsManager.syncDraft(prevPeerID) diff --git a/app/js/lib/ng_utils.js b/app/js/lib/ng_utils.js index 7034d29f..4e9a4c93 100644 --- a/app/js/lib/ng_utils.js +++ b/app/js/lib/ng_utils.js @@ -1005,6 +1005,7 @@ angular.module('izhukov.utils', []) $rootScope.idle = {isIDLE: false, initial: true} var toPromise + var debouncePromise var started = false var hidden = 'hidden' @@ -1067,22 +1068,25 @@ angular.module('izhukov.utils', []) }, 10) } + var debounceTimeout = $rootScope.idle.initial ? 0 : 1000; if (e && !e.fake_initial) { delete $rootScope.idle.initial; } + $timeout.cancel(debouncePromise) + if ($rootScope.idle.isIDLE == isIDLE) { return } - // console.log('IDLE changed', isIDLE) - $rootScope.$apply(function () { + debouncePromise = $timeout(function () { + // console.log(dT(), 'IDLE changed', isIDLE) $rootScope.idle.isIDLE = isIDLE - }) + if (isIDLE && e.type == 'timeout') { + $($window).on('mousemove', onEvent) + } + }, debounceTimeout) - if (isIDLE && e.type == 'timeout') { - $($window).on('mousemove', onEvent) - } } }) diff --git a/app/js/services.js b/app/js/services.js index 9d5aca99..a031a469 100755 --- a/app/js/services.js +++ b/app/js/services.js @@ -4480,7 +4480,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) return } var peerID = AppPeersManager.getPeerID(update.peer) - saveDraft(peerID, update.draft, true) + saveDraft(peerID, update.draft, {notify: true}) }) return { @@ -4526,18 +4526,18 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) return false } - function saveDraft (peerID, apiDraft, notify) { - if (notify) { - console.warn(dT(), 'save draft', peerID, apiDraft, notify) - } + function saveDraft (peerID, apiDraft, options) { + options = options || {} var draft = processApiDraft(apiDraft) cachedServerDrafts[peerID] = draft - if (notify) { + if (options.notify) { + console.warn(dT(), 'save draft', peerID, apiDraft, options) changeDraft(peerID, draft) $rootScope.$broadcast('draft_updated', { peerID: peerID, - draft: draft + draft: draft, + local: options.sync }) } @@ -4659,7 +4659,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) } MtpApiManager.invokeApi('messages.saveDraft', params).then(function () { draftObj.date = tsNow(true) + ServerTimeManager.serverTimeOffset - saveDraft(peerID, draftObj, true) + saveDraft(peerID, draftObj, {notify: true, sync: true}) }) }) }