From 64efe888806d6d7134f6e2857ed0dde29e4daa03 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Fri, 22 Jan 2016 16:02:35 +0300 Subject: [PATCH] Supported stickers real-time update --- app/js/directives.js | 5 + app/js/message_composer.js | 8 +- app/js/services.js | 248 +++++++++++++++++++------------------ 3 files changed, 142 insertions(+), 119 deletions(-) diff --git a/app/js/directives.js b/app/js/directives.js index 88731a29..f3eb85d9 100755 --- a/app/js/directives.js +++ b/app/js/directives.js @@ -1493,6 +1493,11 @@ angular.module('myApp.directives', ['myApp.filters']) } }); + $scope.$on('stickers_changed', function () { + emojiTooltip.onStickersChanged(); + }); + + var composerEmojiPanel; if (emojiPanel) { composerEmojiPanel = new EmojiPanel(emojiPanel, { diff --git a/app/js/message_composer.js b/app/js/message_composer.js index 3cbea068..1ba65ce3 100644 --- a/app/js/message_composer.js +++ b/app/js/message_composer.js @@ -364,7 +364,7 @@ EmojiTooltip.prototype.createTooltip = function () { EmojiTooltip.prototype.selectCategory = function (cat, force) { - if (this.cat === cat && !force) { + if (!this.tab && this.cat === cat && !force) { return false; } $('.active', this.categoriesEl).removeClass('active'); @@ -566,6 +566,12 @@ EmojiTooltip.prototype.onStickersScroll = function (scrollable, scrollTop) { this.activateStickerCategory(); }; +EmojiTooltip.prototype.onStickersChanged = function () { + if (this.tab) { + this.updateStickersContents(true); + } +}; + EmojiTooltip.prototype.activateStickerCategory = function () { var categoriesEl = this.categoriesEl[1]; var categoryEl = categoriesEl.childNodes[this.cat]; diff --git a/app/js/services.js b/app/js/services.js index 92777ad7..ab4d4133 100755 --- a/app/js/services.js +++ b/app/js/services.js @@ -2285,71 +2285,73 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) var currentStickerSets = []; $rootScope.$on('apiUpdate', function (e, update) { - var rewriteCached = false; - if (update._ == 'updateStickerSets') { - getStickers(true); - return true; - } - if (update._ != 'updateNewStickerSet' && + if (update._ != 'updateStickerSets' && + update._ != 'updateNewStickerSet' && update._ != 'updateDelStickerSet' && update._ != 'updateStickerSetsOrder') { return false; } return Storage.get('all_stickers').then(function (stickers) { - if (stickers && - stickers.layer == Config.Schema.API.layer) { - switch (update._) { - case 'updateNewStickerSet': - var fullSet = update.stickerset; - var set = fullSet.set; - set.pFlags.installed = true; - stickers.fullSets[set.id] = fullSet; - var pos = false; - for (var i = 0, len = stickers.sets.length; i < len; i++) { - if (stickers.sets[i].id == set.id) { - pos = i; - break; - } - } - if (pos !== false) { - stickers.sets.splice(pos, 1); + if (!stickers || + stickers.layer != Config.Schema.API.layer) { + $rootScope.$broadcast('stickers_changed'); + } + switch (update._) { + case 'updateNewStickerSet': + var fullSet = update.stickerset; + var set = fullSet.set; + var pos = false; + for (var i = 0, len = stickers.sets.length; i < len; i++) { + if (stickers.sets[i].id == set.id) { + pos = i; + break; } - stickers.sets.unshift(set); - break; + } + if (pos !== false) { + stickers.sets.splice(pos, 1); + } + set.pFlags.installed = true; + stickers.sets.unshift(set); + stickers.fullSets[set.id] = fullSet; + break; - case 'updateDelStickerSet': - for (var i = 0, len = stickers.sets.length; i < len; i++) { - if (stickers.sets[i].id == update.id) { - stickers.sets.splice(i, 1); - break; - } + case 'updateDelStickerSet': + var set; + for (var i = 0, len = stickers.sets.length; i < len; i++) { + set = stickers.sets[i]; + if (set.id == update.id) { + set.pFlags.installed = false; + stickers.sets.splice(i, 1); + break; } - delete stickers.fullSets[update.id]; - break; + } + delete stickers.fullSets[update.id]; + break; - case 'updateStickerSetsOrder': - var order = update.order; - stickers.sets.sort(function (a, b) { - return order.indexOf(a.id) - order.indexOf(b.id); - }); - break; - } - Storage.set({all_stickers: stickers}).then(function () { - getStickers(true) - }); + case 'updateStickerSetsOrder': + var order = update.order; + stickers.sets.sort(function (a, b) { + return order.indexOf(a.id) - order.indexOf(b.id); + }); + break; } + stickers.hash = getStickerSetsHash(stickers.sets); + stickers.date = 0; + Storage.set({all_stickers: stickers}).then(function () { + $rootScope.$broadcast('stickers_changed'); + }); }); }); return { start: start, + getStickers: getStickers, openStickersetLink: openStickersetLink, openStickerset: openStickerset, installStickerset: installStickerset, pushPopularSticker: pushPopularSticker, - getStickers: getStickers, getStickerset: getStickerset }; @@ -2360,46 +2362,38 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) } } - function getPopularStickers () { - return Storage.get('stickers_popular').then(function (popStickers) { - var result = []; - var i, len, docID; - if (popStickers && popStickers.length) { - for (i = 0, len = popStickers.length; i < len; i++) { - docID = popStickers[i][0]; - if (AppDocsManager.hasDoc(docID)) { - result.push({id: docID, rate: popStickers[i][1]}); - } + function getStickers (force) { + return Storage.get('all_stickers').then(function (stickers) { + var layer = Config.Schema.API.layer; + if (stickers.layer != layer) { + stickers = false; + } + if (stickers && stickers.date > tsNow(true) && !force) { + return processRawStickers(stickers); + } + return MtpApiManager.invokeApi('messages.getAllStickers', { + hash: stickers && stickers.hash || '' + }).then(function (newStickers) { + var notModified = newStickers._ == 'messages.allStickersNotModified'; + if (notModified) { + newStickers = stickers; } - }; - return result; - }); - } + newStickers.date = tsNow(true) + 3600; + newStickers.layer = layer; + delete newStickers._; - function pushPopularSticker (id) { - getPopularStickers().then(function (popularStickers) { - var exists = false; - var count = popularStickers.length; - var result = []; - for (var i = 0; i < count; i++) { - if (popularStickers[i].id == id) { - exists = true; - popularStickers[i].rate++; + if (notModified) { + Storage.set({all_stickers: newStickers}); + return processRawStickers(newStickers); } - result.push([popularStickers[i].id, popularStickers[i].rate]); - } - if (exists) { - result.sort(function (a, b) { - return b[1] - a[1]; + + return getStickerSets(newStickers, stickers && stickers.fullSets).then(function () { + Storage.set({all_stickers: newStickers}); + return processRawStickers(newStickers); }); - } else { - if (result.length > 15) { - result = result.slice(0, 15); - } - result.push([id, 1]); - } - ConfigStorage.set({stickers_popular: result}); - }); + + }); + }) } function processRawStickers(stickers) { @@ -2443,49 +2437,13 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) docIDs: docIDs }); } - - console.log('stickers', resultStickersets); return resultStickersets; }); } - function getStickers (force) { - return Storage.get('all_stickers').then(function (stickers) { - var layer = Config.Schema.API.layer; - if (stickers.layer != layer) { - stickers = false; - } - if (stickers && stickers.date > tsNow(true) && !force) { - return processRawStickers(stickers); - } - return MtpApiManager.invokeApi('messages.getAllStickers', { - hash: stickers && stickers.hash || '' - }).then(function (newStickers) { - var notModified = newStickers._ == 'messages.allStickersNotModified'; - if (notModified) { - newStickers = stickers; - } - newStickers.date = tsNow(true) + 3600; - newStickers.layer = layer; - delete newStickers._; - - if (notModified) { - Storage.set({all_stickers: newStickers}); - return processRawStickers(newStickers); - } - - return getStickerSets(newStickers).then(function () { - Storage.set({all_stickers: newStickers}); - return processRawStickers(newStickers); - }); - - }); - }) - } - - function getStickerSets (allStickers) { + function getStickerSets (allStickers, prevCachedSets) { var promises = []; - var cachedSets = allStickers.fullSets || {}; + var cachedSets = prevCachedSets || allStickers.fullSets || {}; allStickers.fullSets = {}; angular.forEach(allStickers.sets, function (shortSet) { var fullSet = cachedSets[shortSet.id]; @@ -2507,6 +2465,48 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) return $q.all(promises); } + function getPopularStickers () { + return Storage.get('stickers_popular').then(function (popStickers) { + var result = []; + var i, len, docID; + if (popStickers && popStickers.length) { + for (i = 0, len = popStickers.length; i < len; i++) { + docID = popStickers[i][0]; + if (AppDocsManager.hasDoc(docID)) { + result.push({id: docID, rate: popStickers[i][1]}); + } + } + }; + return result; + }); + } + + function pushPopularSticker (id) { + getPopularStickers().then(function (popularStickers) { + var exists = false; + var count = popularStickers.length; + var result = []; + for (var i = 0; i < count; i++) { + if (popularStickers[i].id == id) { + exists = true; + popularStickers[i].rate++; + } + result.push([popularStickers[i].id, popularStickers[i].rate]); + } + if (exists) { + result.sort(function (a, b) { + return b[1] - a[1]; + }); + } else { + if (result.length > 15) { + result = result.slice(0, 15); + } + result.push([id, 1]); + } + ConfigStorage.set({stickers_popular: result}); + }); + } + function getStickerset (inputStickerset) { return MtpApiManager.invokeApi('messages.getStickerSet', { stickerset: inputStickerset @@ -2561,6 +2561,18 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) windowClass: 'stickerset_modal_window mobile_modal' }); } + + function getStickerSetsHash (stickerSets) { + var acc = 0, set; + for (var i = 0; i < stickerSets.length; i++) { + set = stickerSets[i]; + if (set.pFlags.disabled || !set.pFlags.installed) { + continue; + } + acc = ((acc * 20261) + 0x80000000 + set.hash) % 0x80000000; + } + return acc; + } }) .service('ApiUpdatesManager', function ($rootScope, MtpNetworkerFactory, AppUsersManager, AppChatsManager, AppPeersManager, MtpApiManager) {