From 9c2840dd633d6866edfb1a6ed18bbd3776552856 Mon Sep 17 00:00:00 2001
From: blavenie <benoit.lavenier@e-is.pro>
Date: Fri, 10 Mar 2017 11:23:42 +0100
Subject: [PATCH] - Wallet: calling restore() from app main - ES extends: now
 use an event emitted by esSettings, when enabling or disabling the plugin

---
 www/js/app.js                                 | 103 +++++++-----------
 www/js/services/settings-services.js          |   2 +-
 www/js/services/wallet-services.js            |   8 --
 .../es/js/controllers/app-controllers.js      |  52 +++------
 .../es/js/controllers/currency-controllers.js |  10 +-
 .../es/js/controllers/network-controllers.js  |  14 +--
 .../es/js/controllers/wallet-controllers.js   |  14 +--
 .../es/js/controllers/wot-controllers.js      |  15 +--
 www/plugins/es/js/services/http-services.js   |   1 +
 .../es/js/services/settings-services.js       |  29 ++++-
 10 files changed, 101 insertions(+), 147 deletions(-)

diff --git a/www/js/app.js b/www/js/app.js
index 1a96524e0..cedd5eed7 100644
--- a/www/js/app.js
+++ b/www/js/app.js
@@ -323,44 +323,8 @@ angular.module('cesium', ['ionic', 'ionic-material', 'ngMessages', 'pascalprecht
   });
   // endRemoveIf(device)
 
-  // Switch between HTTPS or HTTP intelligently
-  if (csConfig.httpsMode === 'clever') {
-    $rootScope.$on('$stateChangeStart', function (event, next, nextParams, fromState) {
-      var href, hashIndex, rootPath ;
-
-      // Redirect to HTTP if view has preferHttp=true
-      if (next.data && next.data.preferHttp &&
-        ($window.location.protocol == 'https:' || csConfig.httpsModeDebug)) {
-        href = $window.location.href;
-        hashIndex = href.indexOf('#');
-        rootPath = (hashIndex != -1) ? href.substr(0, hashIndex) : href;
-        rootPath = 'http' + rootPath.substr(5);
-        href = rootPath + $state.href(next, nextParams);
-        if (csConfig.httpsModeDebug) {
-          console.debug('[httpsMode] --- Should redirect to: ' + href);
-        }
-        else {
-          $window.location.href = href;
-        }
-      }
-      // Redirect to HTTPS
-      else if((!next.data || !next.data.preferHttp) &&
-        ($window.location.protocol != 'https:' || csConfig.httpsModeDebug)) {
-        href = $window.location.href;
-        hashIndex = href.indexOf('#');
-        rootPath = (hashIndex != -1) ? href.substr(0, hashIndex) : href;
-        href = 'https' + rootPath.substr(4) + $state.href(next, nextParams);
-        if (csConfig.httpsModeDebug) {
-          console.debug('[httpsMode] --- Should redirect to: ' + href);
-        }
-        else {
-          $window.location.href = href;
-        }
-      }
-    });
-  }
-  // Always redirect to HTTPS
-  else if (csConfig.httpsMode === true || csConfig.httpsMode === "true" || csConfig.httpsMode === 'force') {
+  // Force redirection to HTTPS
+  if (csConfig.httpsMode === true || csConfig.httpsMode === "true" || csConfig.httpsMode === 'force') {
     $rootScope.$on('$stateChangeStart', function (event, next, nextParams, fromState) {
       if($window.location.protocol != 'https:') {
         var href = $window.location.href;
@@ -377,6 +341,34 @@ angular.module('cesium', ['ionic', 'ionic-material', 'ngMessages', 'pascalprecht
     });
   }
 
+  // Update some translations, when locale changed
+  function onLocaleChange() {
+    console.debug('[app] Loading cached translations for locale [{0}]'.format($translate.use()));
+    $translate(['COMMON.DATE_PATTERN', 'COMMON.DATE_SHORT_PATTERN', 'COMMON.UD'])
+      .then(function(translations) {
+        $rootScope.translations = $rootScope.translations || {};
+        $rootScope.translations.DATE_PATTERN = translations['COMMON.DATE_PATTERN'];
+        if ($rootScope.translations.DATE_PATTERN === 'COMMON.DATE_PATTERN') {
+          $rootScope.translations.DATE_PATTERN = 'YYYY-MM-DD HH:mm';
+        }
+        $rootScope.translations.DATE_SHORT_PATTERN = translations['COMMON.DATE_SHORT_PATTERN'];
+        if ($rootScope.translations.DATE_SHORT_PATTERN === 'COMMON.DATE_SHORT_PATTERN') {
+          $rootScope.translations.DATE_SHORT_PATTERN = 'YYYY-MM-DD';
+        }
+        $rootScope.translations.UD = translations['COMMON.UD'];
+        if ($rootScope.translations.UD === 'COMMON.UD') {
+          $rootScope.translations.UD = 'UD';
+        }
+      });
+  }
+  csSettings.api.locale.on.changed($rootScope, onLocaleChange, this);
+
+  // Start plugins eager services
+  PluginService.start();
+
+  // Force at least on call
+  onLocaleChange();
+
   // We use 'ionicReady()' instead of '$ionicPlatform.ready()', because this one is callable many times
   ionicReady()
     .then(function() {
@@ -406,36 +398,19 @@ angular.module('cesium', ['ionic', 'ionic-material', 'ngMessages', 'pascalprecht
         StatusBar.styleDefault();
       }
 
+      // Force to start settings
       return csSettings.ready();
-    });
+    })
 
-  // Update some translations, when locale changed
-  function onLocaleChange() {
-    console.debug('[app] Loading cached translations for locale [{0}]'.format($translate.use()));
-    $translate(['COMMON.DATE_PATTERN', 'COMMON.DATE_SHORT_PATTERN', 'COMMON.UD'])
-      .then(function(translations) {
-        $rootScope.translations = $rootScope.translations || {};
-        $rootScope.translations.DATE_PATTERN = translations['COMMON.DATE_PATTERN'];
-        if ($rootScope.translations.DATE_PATTERN === 'COMMON.DATE_PATTERN') {
-          $rootScope.translations.DATE_PATTERN = 'YYYY-MM-DD HH:mm';
-        }
-        $rootScope.translations.DATE_SHORT_PATTERN = translations['COMMON.DATE_SHORT_PATTERN'];
-        if ($rootScope.translations.DATE_SHORT_PATTERN === 'COMMON.DATE_SHORT_PATTERN') {
-          $rootScope.translations.DATE_SHORT_PATTERN = 'YYYY-MM-DD';
-        }
-        $rootScope.translations.UD = translations['COMMON.UD'];
-        if ($rootScope.translations.UD === 'COMMON.UD') {
-          $rootScope.translations.UD = 'UD';
-        }
-      });
-  }
-  csSettings.api.locale.on.changed($rootScope, onLocaleChange, this);
+    // Trying to restore default wallet
+    .then(csWallet.restore)
+
+    // Storing wallet to root scope
+    .then(function(walletData){
+      $rootScope.walletData = walletData;
+    });
 
-  // start plugin
-  PluginService.start();
 
-  // Force at least on call
-  onLocaleChange();
 })
 ;
 
diff --git a/www/js/services/settings-services.js b/www/js/services/settings-services.js
index dd9d53d06..aad50fd71 100644
--- a/www/js/services/settings-services.js
+++ b/www/js/services/settings-services.js
@@ -253,7 +253,7 @@ angular.module('cesium.settings.services', ['ngResource', 'ngApi', 'cesium.confi
         console.debug('[settings] Started');
         started = true;
         startPromise = null;
-        // Emit event
+        // Emit event (used by plugins)
         api.data.raise.ready(data);
       });
 
diff --git a/www/js/services/wallet-services.js b/www/js/services/wallet-services.js
index b953fca13..b3a10d2e8 100644
--- a/www/js/services/wallet-services.js
+++ b/www/js/services/wallet-services.js
@@ -1773,13 +1773,5 @@ angular.module('cesium.wallet.services', ['ngResource', 'ngApi', 'cesium.bma.ser
   var service = factory('default');
   service.instance = factory;
 
-  // Default action: restore wallet, then store it to rootscope
-  csSettings.api.data.on.ready($rootScope, function() {
-    service.restore()
-      .then(function(data) {
-        $rootScope.walletData = data;
-      });
-  });
-
   return service;
 });
diff --git a/www/plugins/es/js/controllers/app-controllers.js b/www/plugins/es/js/controllers/app-controllers.js
index 2c148eb4c..0e098aeb3 100644
--- a/www/plugins/es/js/controllers/app-controllers.js
+++ b/www/plugins/es/js/controllers/app-controllers.js
@@ -63,29 +63,22 @@ angular.module('cesium.es.app.controllers', ['ngResource', 'cesium.es.services']
 /**
  * Control new account wizard extend view
  */
-function ESJoinController($scope, $state, csSettings, PluginService) {
+function ESJoinController($scope, esSettings, PluginService) {
   'ngInject';
   $scope.extensionPoint = PluginService.extensions.points.current.get();
-
-  $scope.updateView = function() {
-    $scope.enable = csSettings.data.plugins && csSettings.data.plugins.es ?
-      csSettings.data.plugins.es.enable :
-      !!csSettings.data.plugins.host;
-  };
-
-  csSettings.api.data.on.changed($scope, function() {
-    $scope.updateView();
+  $scope.enable = esSettings.isEnable();
+  esSettings.api.state.on.changed($scope, function(enable) {
+    $scope.enable = enable;
   });
-
-  $scope.updateView();
 }
 
 /**
  * Control menu extension
  */
-function ESMenuExtendController($scope, $state, PluginService, csSettings, UIUtils) {
+function ESMenuExtendController($scope, $state, PluginService, esSettings, UIUtils) {
   'ngInject';
   $scope.extensionPoint = PluginService.extensions.points.current.get();
+  $scope.enable = esSettings.isEnable();
 
   $scope.showMarketLookupView = function() {
     $state.go(UIUtils.screen.isSmall() ? 'app.market_lookup': 'app.market_lookup_lg');
@@ -119,16 +112,9 @@ function ESMenuExtendController($scope, $state, PluginService, csSettings, UIUti
     });
   };
 
-
-  $scope.updateView = function() {
-    $scope.enable = csSettings.data.plugins && csSettings.data.plugins.es ?
-                    csSettings.data.plugins.es.enable :
-                    !!csSettings.data.plugins.host;
-  };
-
-  csSettings.api.data.on.changed($scope, $scope.updateView);
-  csSettings.api.data.on.ready($scope, $scope.updateView);
-
+  esSettings.api.state.on.changed($scope, function(enable) {
+    $scope.enable = enable;
+  });
 }
 
 /**
@@ -144,24 +130,18 @@ function ESProfilePopoverExtendController($scope, $state, csSettings, csWallet)
           !!csSettings.data.plugins.host);
   };
 
-  csSettings.api.data.on.changed($scope, function() {
-    $scope.updateView();
-  });
-
-  csWallet.api.data.on.login($scope, function(walletData, resolve){
-    $scope.updateView();
-    if (resolve) resolve();
-  });
-
-  csWallet.api.data.on.logout($scope, function(){
-    $scope.updateView();
-  });
-
   $scope.showEditUserProfile = function() {
     $scope.closeProfilePopover();
     $state.go('app.user_edit_profile');
   };
 
+  csSettings.api.data.on.changed($scope, $scope.updateView);
+  csSettings.api.data.on.ready($scope, $scope.updateView);
+  csWallet.api.data.on.login($scope, function(walletData, resolve){
+    $scope.updateView();
+    if (resolve) resolve();
+  });
+  csWallet.api.data.on.logout($scope, $scope.updateView);
   $scope.updateView();
 
 }
diff --git a/www/plugins/es/js/controllers/currency-controllers.js b/www/plugins/es/js/controllers/currency-controllers.js
index 294cee034..4b26d236e 100644
--- a/www/plugins/es/js/controllers/currency-controllers.js
+++ b/www/plugins/es/js/controllers/currency-controllers.js
@@ -20,7 +20,7 @@ angular.module('cesium.es.currency.controllers', ['ngResource', 'cesium.es.servi
   .controller('ESCurrencyTabBlocksExtendCtrl', ESCurrencyTabBlocksExtendController)
 ;
 
-function ESCurrencyTabBlocksExtendController($scope, PluginService, csSettings) {
+function ESCurrencyTabBlocksExtendController($scope, PluginService, esSettings) {
   'ngInject';
 
   $scope.extensionPoint = PluginService.extensions.points.current.get();
@@ -31,10 +31,8 @@ function ESCurrencyTabBlocksExtendController($scope, PluginService, csSettings)
       !!csSettings.data.plugins.host;
   };
 
-  csSettings.api.data.on.changed($scope, function() {
-    $scope.updateView();
+  $scope.enable = esSettings.isEnable();
+  esSettings.api.state.on.changed($scope, function(enable) {
+    $scope.enable = enable;
   });
-
-  $scope.updateView();
-
 }
diff --git a/www/plugins/es/js/controllers/network-controllers.js b/www/plugins/es/js/controllers/network-controllers.js
index c9562af2e..e329488e6 100644
--- a/www/plugins/es/js/controllers/network-controllers.js
+++ b/www/plugins/es/js/controllers/network-controllers.js
@@ -22,17 +22,13 @@ angular.module('cesium.es.network.controllers', ['cesium.es.services'])
 ;
 
 
-function ESNetworkViewExtendController($scope, PluginService, csSettings) {
+function ESNetworkViewExtendController($scope, PluginService, esSettings) {
   'ngInject';
 
   $scope.extensionPoint = PluginService.extensions.points.current.get();
+  $scope.enable = esSettings.isEnable();
 
-  $scope.updateView = function() {
-    $scope.enable = csSettings.data.plugins && csSettings.data.plugins.es ?
-      csSettings.data.plugins.es.enable :
-      !!csSettings.data.plugins.host;
-  };
-
-  csSettings.api.data.on.changed($scope, $scope.updateView);
-  csSettings.api.data.on.ready($scope, $scope.updateView);
+  esSettings.api.state.on.changed($scope, function(enable) {
+    $scope.enable = enable;
+  });
 }
diff --git a/www/plugins/es/js/controllers/wallet-controllers.js b/www/plugins/es/js/controllers/wallet-controllers.js
index 5bf1ae8e6..91f8996be 100644
--- a/www/plugins/es/js/controllers/wallet-controllers.js
+++ b/www/plugins/es/js/controllers/wallet-controllers.js
@@ -22,19 +22,13 @@ angular.module('cesium.es.wallet.controllers', ['cesium.es.services'])
 
 ;
 
-function ESWalletViewController($scope, csSettings) {
+function ESWalletViewController($scope, esSettings) {
   'ngInject';
 
-  $scope.updateView = function() {
-    $scope.enable = csSettings.data.plugins && csSettings.data.plugins.es ?
-      csSettings.data.plugins.es.enable :
-      !!csSettings.data.plugins.host;
-  };
+  $scope.enable = esSettings.isEnable();
 
-  csSettings.api.data.on.changed($scope, function() {
-    $scope.updateView();
+  esSettings.api.state.on.changed($scope, function(enable) {
+    $scope.enable = enable;
   });
 
-  $scope.updateView();
-
 }
diff --git a/www/plugins/es/js/controllers/wot-controllers.js b/www/plugins/es/js/controllers/wot-controllers.js
index 2770a739f..749705f4f 100644
--- a/www/plugins/es/js/controllers/wot-controllers.js
+++ b/www/plugins/es/js/controllers/wot-controllers.js
@@ -41,20 +41,15 @@ angular.module('cesium.es.wot.controllers', ['cesium.es.services'])
 
 ;
 
-function ESWotIdentityViewController($scope, $ionicPopover, $q, UIUtils, Modals, csSettings, PluginService,
+function ESWotIdentityViewController($scope, $ionicPopover, $q, UIUtils, Modals, esSettings, PluginService,
                                      esModals, esHttp, esWallet, esInvitation) {
   'ngInject';
 
   $scope.extensionPoint = PluginService.extensions.points.current.get();
-
-  $scope.updateView = function() {
-    $scope.enable = csSettings.data.plugins && csSettings.data.plugins.es ?
-      csSettings.data.plugins.es.enable :
-      !!csSettings.data.plugins.host;
-  };
-
-  csSettings.api.data.on.changed($scope, $scope.updateView);
-  csSettings.api.data.on.ready($scope, $scope.updateView);
+  $scope.enable = esSettings.isEnable();
+  esSettings.api.state.on.changed($scope, function(enable) {
+    $scope.enable = enable;
+  });
 
   /* -- modals -- */
 
diff --git a/www/plugins/es/js/services/http-services.js b/www/plugins/es/js/services/http-services.js
index c68cd5480..1effb79c4 100644
--- a/www/plugins/es/js/services/http-services.js
+++ b/www/plugins/es/js/services/http-services.js
@@ -143,6 +143,7 @@ angular.module('cesium.es.http.services', ['ngResource', 'ngApi', 'cesium.servic
       that.alive = false;
       that.cleanCache();
       that.api.node.raise.stop();
+      return $q.when();
     };
 
     that.restart = function() {
diff --git a/www/plugins/es/js/services/settings-services.js b/www/plugins/es/js/services/settings-services.js
index aeba31e14..25f267155 100644
--- a/www/plugins/es/js/services/settings-services.js
+++ b/www/plugins/es/js/services/settings-services.js
@@ -10,7 +10,7 @@ angular.module('cesium.es.settings.services', ['cesium.services', 'cesium.es.htt
 
   })
 
-.factory('esSettings', function($rootScope, $q, $timeout, esHttp,
+.factory('esSettings', function($rootScope, $q, $timeout, Api, esHttp,
                             csConfig, csSettings, CryptoUtils, Device, UIUtils, csWallet) {
   'ngInject';
 
@@ -45,11 +45,13 @@ angular.module('cesium.es.settings.services', ['cesium.services', 'cesium.es.htt
         }
     }, {plugins: {es: csConfig.plugins && csConfig.plugins.es || {}}}),
     that = this,
+    api = new Api('esSettings'),
     previousRemoteData,
     listeners,
     ignoreSettingsChanged = false
   ;
 
+  that.api = api;
   that.get = esHttp.get('/user/settings/:id');
   that.add = esHttp.record.post('/user/settings');
   that.update = esHttp.record.post('/user/settings/:id/_update');
@@ -293,7 +295,14 @@ angular.module('cesium.es.settings.services', ['cesium.services', 'cesium.es.htt
     if (!enable && listeners && listeners.length > 0) {
       console.debug("[ES] [settings] Disable");
       removeListeners();
-      return esHttp.stop();
+
+
+      // Force ES node to stop
+      return esHttp.stop()
+        .then(function() {
+          // Emit event
+          api.state.raise.changed(enable);
+        });
     }
 
     // Enable
@@ -307,14 +316,28 @@ angular.module('cesium.es.settings.services', ['cesium.services', 'cesium.es.htt
           else {
             console.debug("[ES] [settings] Enable");
             addListeners();
+
+            // Emit event
+            api.state.raise.changed(enable);
+
             if (csWallet.isLogin()) {
-              return onWalletLogin(csWallet.data);
+              return onWalletLogin(csWallet.data)
+                .then(function() {
+                  // Emit event
+                  api.state.raise.changed(enable);
+                });
+            }
+            else {
+              // Emit event
+              api.state.raise.changed(enable);
             }
           }
         });
     }
   }
 
+  api.registerEvent('state', 'changed');
+
   csSettings.ready().then(function() {
 
     csSettings.api.data.on.changed($rootScope, onSettingsChanged, this);
-- 
GitLab