0

I am trying to rewrite the following controller as a service so that it can be called from my view within an ng-show directive, but I am currently struggling. It needs to be a service so that it returns a true or false value, and can be reused all over the app:

module.controller('MyCtrl', function($rootScope, $cordovaNetwork) 
  document.addEventListener("deviceready", function () {
    var type = $cordovaNetwork.getNetwork()
    var isOnline = $cordovaNetwork.isOnline() 
    var isOffline = $cordovaNetwork.isOffline()


    // listen for Online event
    $rootScope.$on('$cordovaNetwork:online', function(event, networkState){
      var onlineState = networkState;
    })

    // listen for Offline event
    $rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
      var offlineState = networkState;
    })

  }, false);
});

This is what I have done so far, which isn't working at the moment:

(function() {
    'use strict';

    angular
    .module('dingocv.services')
    .service('ConnectionService', ConnectionService)


    function ConnectionService($rootScope, $cordovaNetwork) {
    this.isOnline = function() {
      var type = $cordovaNetwork.getNetwork()
      var isOnline = $cordovaNetwork.isOnline()
      var isOffline = $cordovaNetork.isOffline()

      $rootScope.$on('$cordovaNetwork:online', function(event, networkState)) {
        return networkState;
      }   

      $rootScope.$on('$cordovaNetwork:offline', function(event, networkState)) {
        return networkState;
      }         
    }
    }

})();
methuselah
  • 12,766
  • 47
  • 165
  • 315

1 Answers1

1

If i have understand rigth, my solution is below:

//This is the service

.factory('ConnectivityMonitor', function($rootScope, $cordovaNetwork){

  return {
    isOnline: function(){
  if(ionic.Platform.isWebView()){
    return $cordovaNetwork.isOnline(); 
  } else {
    return navigator.onLine;
  }
},
isOffline: function(){
  if(ionic.Platform.isWebView()){
    return !$cordovaNetwork.isOnline();    
  } else {
    return !navigator.onLine;
  }
},
startWatching: function(){
    if(ionic.Platform.isWebView()){

      $rootScope.$on('$cordovaNetwork:online', function(event, networkState){
        console.log("went online");
      });

      $rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
        console.log("went offline");
      });

    }else {

      window.addEventListener("online", function(e) {
        console.log("went online");
      }, false);    

      window.addEventListener("offline", function(e) {
        console.log("went offline");
      }, false);  
    }       
   }
  }

  })

And to check network status all over the app i simply use (in controller):

.controller('MyCtrl',['ConnectivityMonitor', function(ConnectivityMonitor) {

 if(ConnectivityMonitor.isOnline()){

   //do something

   }else{

      //do something else 
   }   

}])

Hope this helps!

e7lT2P
  • 1,635
  • 5
  • 31
  • 57
  • Thanks for your help. How would I use startWatching in the controller, what purpose does it serve? Also I wanted to know how frequently the service checks for the network status, does it immediately identify when the user has moved from offline to online/2G/3G? – methuselah Oct 17 '16 at 06:10
  • I've tested this on my Android device and yes it does work initially onload and sets the connection status correctly but if I switch on/off my wifi connection it does not refresh immediately – methuselah Oct 17 '16 at 06:19
  • Yes you are rigth.it takes some time – e7lT2P Oct 17 '16 at 15:09
  • Is there anything that we can do to speed it up and make it real time – methuselah Oct 17 '16 at 15:47
  • I actually dont know.in everything that i had search i had found nothing :( – e7lT2P Oct 17 '16 at 16:08
  • where will I find ConnectivityMonitor? how to import? – Mitesh Shah Apr 17 '19 at 15:42