From f141abe04913d8b8abade6e556dc6bf38a23a185 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Fri, 19 Sep 2014 18:40:55 +0400 Subject: [PATCH] Fixed app reload for packaged app --- app/js/controllers.js | 8 ++++---- app/js/lib/ng_utils.js | 28 ++++++++++++++++++++++++++++ app/js/services.js | 14 ++------------ 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/app/js/controllers.js b/app/js/controllers.js index 1c9f6181..d0490d79 100644 --- a/app/js/controllers.js +++ b/app/js/controllers.js @@ -305,7 +305,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) ChangelogNotifyService.checkUpdate(); }) - .controller('AppIMController', function ($scope, $location, $routeParams, $modal, $rootScope, $modalStack, MtpApiManager, AppUsersManager, ContactsSelectService, ChangelogNotifyService, ErrorService) { + .controller('AppIMController', function ($scope, $location, $routeParams, $modal, $rootScope, $modalStack, MtpApiManager, AppUsersManager, ContactsSelectService, ChangelogNotifyService, ErrorService, AppRuntimeManager) { $scope.$on('$routeUpdate', updateCurDialog); @@ -382,7 +382,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) ErrorService.confirm({type: 'LOGOUT'}).then(function () { MtpApiManager.logOut().then(function () { location.hash = '/login'; - location.reload(); + AppRuntimeManager.reload(); }); }) }; @@ -1455,7 +1455,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) } }) - .controller('AppLangFooterController', function ($scope, _, Storage, ErrorService) { + .controller('AppLangFooterController', function ($scope, _, Storage, ErrorService, AppRuntimeManager) { $scope.supportedLocales = Config.I18n.supported; $scope.langNames = Config.I18n.languages; $scope.curLocale = Config.I18n.locale; @@ -1464,7 +1464,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) Storage.set({i18n_locale: newLocale}); if ($scope.curLocale !== newLocale) { ErrorService.confirm({type: 'APPLY_LANG_WITH_RELOAD'}).then(function () { - location.reload(); + AppRuntimeManager.reload(); }); } }; diff --git a/app/js/lib/ng_utils.js b/app/js/lib/ng_utils.js index aea2a9bc..2f2c8a2f 100644 --- a/app/js/lib/ng_utils.js +++ b/app/js/lib/ng_utils.js @@ -569,6 +569,34 @@ angular.module('izhukov.utils', []) }; }) +.service('AppRuntimeManager', function ($window) { + + return { + reload: function () { + try { + location.reload(); + } catch (e) {}; + + if ($window.chrome && chrome.runtime && chrome.runtime.reload) { + chrome.runtime.reload(); + }; + }, + focus: function () { + if (window.navigator.mozApps && document.hidden) { + // Get app instance and launch it to bring app to foreground + window.navigator.mozApps.getSelf().onsuccess = function() { + this.result.launch(); + }; + } else { + if (window.chrome && chrome.app && chrome.app.window) { + chrome.app.window.current().focus(); + } + window.focus(); + } + } + } +}) + diff --git a/app/js/services.js b/app/js/services.js index 6e20e0a1..97d6489c 100644 --- a/app/js/services.js +++ b/app/js/services.js @@ -3448,7 +3448,7 @@ angular.module('myApp.services', ['myApp.i18n']) }) -.service('NotificationsManager', function ($rootScope, $window, $timeout, $interval, $q, MtpApiManager, AppPeersManager, IdleManager, Storage) { +.service('NotificationsManager', function ($rootScope, $window, $timeout, $interval, $q, MtpApiManager, AppPeersManager, IdleManager, Storage, AppRuntimeManager) { navigator.vibrate = navigator.vibrate || navigator.mozVibrate || navigator.webkitVibrate; @@ -3647,17 +3647,7 @@ angular.module('myApp.services', ['myApp.i18n']) notification.onclick = function () { notification.close(); - if (window.navigator.mozApps && document.hidden) { - // Get app instance and launch it to bring app to foreground - window.navigator.mozApps.getSelf().onsuccess = function() { - this.result.launch(); - }; - } else { - if (window.chrome && chrome.app && chrome.app.window) { - chrome.app.window.current().focus(); - } - window.focus(); - } + AppRuntimeManager.focus(); notificationsClear(); if (data.onclick) { data.onclick();