diff --git a/app/index.html b/app/index.html index d5697005..f0e25997 100644 --- a/app/index.html +++ b/app/index.html @@ -48,14 +48,14 @@ - + - - + + - + diff --git a/app/js/controllers.js b/app/js/controllers.js index 5da74278..bb677d8e 100644 --- a/app/js/controllers.js +++ b/app/js/controllers.js @@ -133,14 +133,8 @@ angular.module('myApp.controllers', []) }); } - // $scope.userID = 0; - // MtpApiManager.getUserID().then(function (userID) { - // $scope.userID = userID; - // }); - updateCurDialog(); - function updateCurDialog() { $scope.curDialog = { peer: $routeParams.p || false @@ -150,15 +144,19 @@ angular.module('myApp.controllers', []) .controller('AppImDialogsController', function ($scope, $location, MtpApiManager, AppUsersManager, AppChatsManager, AppMessagesManager, AppPeersManager) { + // console.log('init controller'); + $scope.dialogs = []; var offset = 0, + maxID = 0, hasMore = false, limit = 20; MtpApiManager.invokeApi('account.updateStatus', {offline: false}); $scope.$on('dialogs_need_more', function () { + // console.log('on need more'); showMoreDialogs(); }); @@ -193,11 +191,13 @@ angular.module('myApp.controllers', []) function loadDialogs (startLimit) { offset = 0; + maxID = 0; hasMore = false; startLimit = startLimit || limit; - AppMessagesManager.getDialogs(offset, startLimit).then(function (dialogsResult) { + AppMessagesManager.getDialogs(maxID, startLimit).then(function (dialogsResult) { offset += startLimit; + maxID = dialogsResult.dialogs[dialogsResult.dialogs.length - 1].top_message; hasMore = offset < dialogsResult.count; $scope.dialogs = []; @@ -218,8 +218,9 @@ angular.module('myApp.controllers', []) return; } - AppMessagesManager.getDialogs(offset, limit).then(function (dialogsResult) { + AppMessagesManager.getDialogs(maxID, limit).then(function (dialogsResult) { offset += limit; + maxID = dialogsResult.dialogs[dialogsResult.dialogs.length - 1].top_message; hasMore = offset < dialogsResult.count; angular.forEach(dialogsResult.dialogs, function (dialog) { diff --git a/app/js/directives.js b/app/js/directives.js index 109c8ac5..16b8b19f 100644 --- a/app/js/directives.js +++ b/app/js/directives.js @@ -35,10 +35,13 @@ angular.module('myApp.directives', ['myApp.filters']) link: link }; + function link (scope, element, attrs) { - var dialogsWrap = $('.im_dialogs_wrap')[0], - scrollableWrap = $('.im_dialogs_scrollable_wrap')[0], - // dialogsSearch = $('im_dialogs_search')[0], + // console.log('init directive', element); + + var dialogsWrap = $('.im_dialogs_wrap', element)[0], + scrollableWrap = $('.im_dialogs_scrollable_wrap', element)[0], + // dialogsSearch = $('im_dialogs_search', element)[0], moreNotified = false; onContentLoaded(function () { @@ -69,7 +72,9 @@ angular.module('myApp.directives', ['myApp.filters']) }); $(scrollableWrap).on('scroll', function (e) { + // console.log('scroll', moreNotified); if (!moreNotified && scrollableWrap.scrollTop >= scrollableWrap.scrollHeight - scrollableWrap.clientHeight - 300) { + // console.log('emit need more'); scope.$emit('dialogs_need_more'); moreNotified = true; } @@ -96,10 +101,10 @@ angular.module('myApp.directives', ['myApp.filters']) }; function link (scope, element, attrs) { - var historyWrap = $('.im_history_wrap')[0], - historyEl = $('.im_history')[0], - scrollableWrap = $('.im_history_scrollable_wrap')[0], - scrollable = $('.im_history_scrollable')[0], + var historyWrap = $('.im_history_wrap', element)[0], + historyEl = $('.im_history', element)[0], + scrollableWrap = $('.im_history_scrollable_wrap', element)[0], + scrollable = $('.im_history_scrollable', element)[0], panelWrap = $('.im_history_panel_wrap', element)[0], sendFormWrap = $('.im_send_form_wrap', element)[0], moreNotified = false; @@ -244,7 +249,7 @@ angular.module('myApp.directives', ['myApp.filters']) editorElement = messageField, dragStarted, dragTimeout, emojiArea = $(messageField).emojiarea({button: emojiButton}), - emojiMenu = $('.emoji-menu')[0], + emojiMenu = $('.emoji-menu', element)[0], richTextarea = $('.emoji-wysiwyg-editor', element)[0]; if (richTextarea) { diff --git a/app/js/lib/mtproto.js b/app/js/lib/mtproto.js index 1e929df1..4728ce99 100644 --- a/app/js/lib/mtproto.js +++ b/app/js/lib/mtproto.js @@ -2144,7 +2144,11 @@ factory('MtpNetworkerFactory', function (MtpDcConfigurator, MtpMessageIdGenerato } } else { if (deferred) { - console.log('rpc response', message.result._); + if (window._debugMode) { + console.log('rpc response', message.result); + } else { + console.log('rpc response', message.result._); + } sentMessage.deferred.resolve(message.result); } if (sentMessage.isAPI) { diff --git a/app/js/services.js b/app/js/services.js index ef4be374..1fd6ca42 100644 --- a/app/js/services.js +++ b/app/js/services.js @@ -422,7 +422,16 @@ angular.module('myApp.services', []) NotificationsManager.start(); - function getDialogs (offset, limit) { + function getDialogs (maxID, limit) { + var offset = 0; + if (maxID > 0) { + for (offset = 0; offset < dialogsStorage.dialogs.length; offset++) { + if (maxID > dialogsStorage.dialogs[offset].top_message) { + break; + } + } + } + if (dialogsStorage.count !== null && ( dialogsStorage.dialogs.length >= offset + limit || dialogsStorage.dialogs.length == dialogsStorage.count @@ -438,16 +447,25 @@ angular.module('myApp.services', []) MtpApiManager.invokeApi('messages.getDialogs', { offset: offset, limit: limit, - max_id: 0 + max_id: maxID || 0 }).then(function (dialogsResult) { AppUsersManager.saveApiUsers(dialogsResult.users); AppChatsManager.saveApiChats(dialogsResult.chats); saveMessages(dialogsResult.messages); + if (maxID > 0) { + for (offset = 0; offset < dialogsStorage.dialogs.length; offset++) { + if (maxID > dialogsStorage.dialogs[offset].top_message) { + break; + } + } + } + dialogsStorage.count = dialogsResult._ == 'messages.dialogsSlice' ? dialogsResult.count : dialogsResult.dialogs.length; + dialogsStorage.dialogs.splice(offset, dialogsStorage.dialogs.length - offset); angular.forEach(dialogsResult.dialogs, function (dialog) { dialogsStorage.dialogs.push({ peerID: AppPeersManager.getPeerID(dialog.peer), @@ -491,7 +509,10 @@ angular.module('myApp.services', []) } // console.log('history storage', angular.copy(historyStorage.history), maxID, offset); - if (historyStorage.count !== null && historyStorage.history.length >= offset + limit) { + if (historyStorage.count !== null && ( + historyStorage.history.length >= offset + limit || + historyStorage.history.length == historyStorage.count + )) { return $q.when({ count: historyStorage.count, history: resultPending.concat(historyStorage.history.slice(offset, offset + limit))