From 39623095458992f00274d186c72d72d1c8914df8 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Thu, 21 Jan 2016 22:47:00 +0000 Subject: [PATCH] Improved stickers updates logic --- app/js/controllers.js | 7 +++- app/js/services.js | 98 ++++++++++++++++++++++++++++++++----------- 2 files changed, 78 insertions(+), 27 deletions(-) diff --git a/app/js/controllers.js b/app/js/controllers.js index 271f3e46..b79009c4 100644 --- a/app/js/controllers.js +++ b/app/js/controllers.js @@ -4657,11 +4657,14 @@ angular.module('myApp.controllers', ['myApp.i18n']) .controller('StickersetModalController', function ($scope, $rootScope, $modalInstance, MtpApiManager, RichTextProcessor, AppStickersManager, AppDocsManager, AppMessagesManager, LocationParamsService) { $scope.slice = {limit: 20, limitDelta: 20}; + var fullSet; + AppStickersManager.getStickerset($scope.inputStickerset).then(function (result) { $scope.$broadcast('ui_height'); $scope.stickersetLoaded = true; + fullSet = result; $scope.stickerset = result.set; - $scope.stickersetInstalled = result.set.pFlags.installed; + $scope.stickersetInstalled = result.set.pFlags.installed == true; $scope.documents = result.documents; $scope.stickerEmojis = {}; @@ -4676,7 +4679,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) }); $scope.toggleInstalled = function (installed) { - AppStickersManager.installStickerset($scope.stickerset, !installed).then(function () { + AppStickersManager.installStickerset(fullSet, !installed).then(function () { $scope.stickersetInstalled = installed; }) }; diff --git a/app/js/services.js b/app/js/services.js index 53cc3797..92777ad7 100755 --- a/app/js/services.js +++ b/app/js/services.js @@ -1222,6 +1222,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) }); break; } + return $q.reject(error); }); } @@ -2277,20 +2278,69 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) } }) -.service('AppStickersManager', function ($q, $rootScope, $modal, _, FileManager, MtpApiManager, MtpApiFileManager, AppDocsManager, Storage) { +.service('AppStickersManager', function ($q, $rootScope, $modal, _, FileManager, MtpApiManager, MtpApiFileManager, AppDocsManager, Storage, ApiUpdatesManager) { - var currentStickers = []; - var currentStickersets = []; - var stickersetItems = {}; - var applied = false; var started = false; + var applied = false; + var currentStickerSets = []; $rootScope.$on('apiUpdate', function (e, update) { + var rewriteCached = false; if (update._ == 'updateStickerSets') { - // Storage.remove('all_stickers').then(function () { - // getStickers(true); - // }); + getStickers(true); + return true; } + if (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); + } + stickers.sets.unshift(set); + 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; + } + } + 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) + }); + } + }); + }); return { @@ -2357,7 +2407,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) applied = stickers.hash; var i, j, len1, len2, doc, set, docIDs, documents; - currentStickersets = []; + currentStickerSets = []; len1 = stickers.sets.length; for (i = 0; i < len1; i++) { set = stickers.sets[i]; @@ -2373,13 +2423,14 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) docIDs.push(doc.id); } set.docIDs = docIDs; + currentStickerSets.push(set); } } return getPopularStickers().then(function (popularStickers) { - var resultStickersets = currentStickersets; + var resultStickersets = currentStickerSets; if (popularStickers.length) { - resultStickersets = currentStickersets.slice(); + resultStickersets = currentStickerSets.slice(); var docIDs = []; var i, len; for (i = 0, len = popularStickers.length; i < len; i++) { @@ -2393,6 +2444,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) }); } + console.log('stickers', resultStickersets); return resultStickersets; }); } @@ -2466,33 +2518,29 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) }); } - function installStickerset (set, uninstall) { + function installStickerset (fullSet, uninstall) { var method = uninstall ? 'messages.uninstallStickerSet' : 'messages.installStickerSet'; var inputStickerset = { _: 'inputStickerSetID', - id: set.id, - access_hash: set.access_hash + id: fullSet.set.id, + access_hash: fullSet.set.access_hash }; return MtpApiManager.invokeApi(method, { stickerset: inputStickerset, disabled: false }).then(function (result) { + var update; if (uninstall) { - + update = {_: 'updateDelStickerSet', id: fullSet.set.id}; } else { - ApiUpdatesManager.processUpdateMessage({ - _: 'updateShort', - update: { - _: 'updateNewStickerSet', - channel_id: channelID, - messages: msgIDs, - pts: affectedMessages.pts, - pts_count: affectedMessages.pts_count - } - }); + update = {_: 'updateNewStickerSet', stickerset: fullSet}; } + ApiUpdatesManager.processUpdateMessage({ + _: 'updateShort', + update: update + }); }); }