2015-03-06 12:00:10 -03:00
'use strict' ;
angular . module ( 'copayApp.controllers' ) . controller ( 'copayersController' ,
2017-05-24 09:44:04 -03:00
function ( $scope , $log , $timeout , $stateParams , $state , $rootScope , $ionicHistory , appConfigService , lodash , profileService , walletService , popupService , bwcError , platformInfo , gettextCatalog , ongoingProcess , pushNotificationsService ) {
2016-10-21 15:24:37 -03:00
2017-05-24 12:42:22 -03:00
var listener ;
2016-12-27 15:19:53 -03:00
var appName = appConfigService . userVisibleName ;
var appUrl = appConfigService . url ;
2015-03-06 12:00:10 -03:00
2016-10-12 15:07:09 -03:00
$scope . isCordova = platformInfo . isCordova ;
2016-09-22 16:50:06 -03:00
$scope . $on ( "$ionicView.beforeEnter" , function ( event , data ) {
2016-10-12 15:07:09 -03:00
$scope . wallet = profileService . getWallet ( data . stateParams . walletId ) ;
2016-09-22 16:04:05 -03:00
updateWallet ( ) ;
2017-05-24 09:44:04 -03:00
$scope . shareIcon = platformInfo . isIOS ? 'iOS' : 'Android' ;
2017-05-24 12:42:22 -03:00
listener = $rootScope . $on ( 'bwsEvent' , function ( e , walletId , type , n ) {
if ( $scope . wallet && walletId == $scope . wallet . id && type == ( 'NewCopayer' || 'WalletComplete' ) )
updateWalletDebounced ( ) ;
} ) ;
} ) ;
2015-03-06 12:00:10 -03:00
2017-05-24 12:42:22 -03:00
$scope . $on ( "$ionicView.leave" , function ( event , data ) {
2017-05-17 16:03:16 -03:00
listener ( ) ;
} ) ;
2016-09-22 16:04:05 -03:00
var updateWallet = function ( ) {
$log . debug ( 'Updating wallet:' + $scope . wallet . name )
walletService . getStatus ( $scope . wallet , { } , function ( err , status ) {
if ( err ) {
2017-01-16 16:00:09 -03:00
return popupService . showAlert ( bwcError . msg ( err , gettextCatalog . getString ( 'Could not update wallet' ) ) ) ;
2016-09-22 16:04:05 -03:00
}
$scope . wallet . status = status ;
$scope . copayers = $scope . wallet . status . wallet . copayers ;
$scope . secret = $scope . wallet . status . wallet . secret ;
$timeout ( function ( ) {
$scope . $apply ( ) ;
} ) ;
if ( status . wallet . status == 'complete' ) {
$scope . wallet . openWallet ( function ( err , status ) {
if ( err ) $log . error ( err ) ;
2017-05-24 09:44:04 -03:00
$scope . clearNextView ( ) ;
$state . go ( 'tabs.home' ) . then ( function ( ) {
$state . transitionTo ( 'tabs.wallet' , {
walletId : $scope . wallet . credentials . walletId
} ) ;
} ) ;
2016-09-22 16:04:05 -03:00
} ) ;
}
} ) ;
} ;
2015-03-06 12:00:10 -03:00
2017-05-24 12:42:22 -03:00
var updateWalletDebounced = lodash . debounce ( updateWallet , 5000 , true ) ;
2016-08-25 13:09:50 -03:00
$scope . showDeletePopup = function ( ) {
2016-10-12 15:07:09 -03:00
var title = gettextCatalog . getString ( 'Confirm' ) ;
var msg = gettextCatalog . getString ( 'Are you sure you want to cancel and delete this wallet?' ) ;
popupService . showConfirm ( title , msg , null , null , function ( res ) {
2016-09-06 11:22:10 -03:00
if ( res ) deleteWallet ( ) ;
2015-03-06 12:00:10 -03:00
} ) ;
} ;
2016-08-25 13:09:50 -03:00
function deleteWallet ( ) {
ongoingProcess . set ( 'deletingWallet' , true ) ;
2016-09-22 16:04:05 -03:00
profileService . deleteWalletClient ( $scope . wallet , function ( err ) {
2016-08-25 13:09:50 -03:00
ongoingProcess . set ( 'deletingWallet' , false ) ;
2016-01-28 16:49:31 -03:00
if ( err ) {
2016-09-01 10:56:13 -03:00
popupService . showAlert ( gettextCatalog . getString ( 'Error' ) , err . message || err ) ;
2016-01-28 16:49:31 -03:00
} else {
2017-05-24 09:44:04 -03:00
pushNotificationsService . unsubscribe ( $scope . wallet ) ;
$scope . clearNextView ( ) ;
$state . go ( 'tabs.home' ) ;
2016-01-28 16:49:31 -03:00
}
} ) ;
2015-03-06 12:00:10 -03:00
} ;
2016-08-15 16:07:30 -03:00
$scope . copySecret = function ( ) {
if ( $scope . isCordova ) {
2016-09-22 16:04:05 -03:00
window . cordova . plugins . clipboard . copy ( $scope . secret ) ;
2015-07-29 20:12:37 +09:00
window . plugins . toast . showShortCenter ( gettextCatalog . getString ( 'Copied to clipboard' ) ) ;
2015-03-06 12:00:10 -03:00
}
} ;
2016-08-15 16:07:30 -03:00
$scope . shareSecret = function ( ) {
if ( $scope . isCordova ) {
2016-10-21 15:24:37 -03:00
var message = gettextCatalog . getString ( 'Join my {{appName}} Wallet. Here is the invitation code: {{secret}} You can download {{appName}} for your phone or desktop at {{appUrl}}' , {
secret : $scope . secret ,
appName : appName ,
appUrl : appUrl
2016-06-09 10:11:37 -03:00
} ) ;
2016-10-21 15:24:37 -03:00
window . plugins . socialsharing . share ( message , gettextCatalog . getString ( 'Invitation to share a {{appName}} Wallet' , {
appName : appName
} ) , null , null ) ;
2015-03-06 12:00:10 -03:00
}
} ;
2016-09-22 12:02:45 -03:00
2017-05-24 09:44:04 -03:00
$scope . clearNextView = function ( ) {
2017-05-24 12:42:22 -03:00
listener ( ) ; // remove listener
2017-05-24 09:44:04 -03:00
$ionicHistory . nextViewOptions ( {
disableAnimate : true ,
historyRoot : true
} ) ;
$ionicHistory . clearHistory ( ) ;
2016-09-22 12:02:45 -03:00
} ;
2016-09-22 16:04:05 -03:00
2016-08-19 13:09:27 -03:00
} ) ;