1
1
angular . module ( 'blocktrail.wallet' )
2
2
. controller ( 'SettingsCtrl' , function ( $scope , $rootScope , $q , sdkService , launchService , settingsService ,
3
- Wallet , Contacts , storageService , $cordovaDialogs , $ionicLoading ,
3
+ Wallet , Contacts , storageService , $cordovaDialogs , $ionicLoading , $cordovaFile ,
4
4
$translate , $timeout , $state , $log , $analytics , CONFIG , AppRateService , $cordovaToast ) {
5
5
$scope . appControl = {
6
6
syncing : false ,
@@ -388,12 +388,38 @@ angular.module('blocktrail.wallet')
388
388
$cordovaDialogs . alert ( err . toString ( ) , $translate . instant ( 'FAILED' ) . sentenceCase ( ) , $translate . instant ( 'OK' ) ) ;
389
389
}
390
390
} ) ;
391
- } else {
392
- $cordovaDialogs . alert (
393
- $translate . instant ( 'MSG_BACKUP_SAVED_ALREADY' ) . sentenceCase ( ) ,
394
- $translate . instant ( 'SETTINGS_BACKUP_COMPLETE' ) . sentenceCase ( ) ,
395
- $translate . instant ( 'OK' )
396
- ) ;
391
+ } else { // If backup has not been saved
392
+ settingsService . $isLoaded ( ) . then ( function ( ) {
393
+
394
+ var dialogMessage = 'MSG_BACKUP_SAVED_ALREADY' ;
395
+
396
+ var backupSettings = {
397
+ path : window . cordova ? ( ionic . Platform . isAndroid ( ) ? cordova . file . externalDataDirectory : cordova . file . documentsDirectory ) : null ,
398
+ filename : 'btc-wallet-backup-' + $scope . defaultWallet + '.pdf'
399
+ } ;
400
+
401
+ // Check if backup.pdf is still on the phone, notify accordingly
402
+ $cordovaFile . checkFile ( backupSettings . path , backupSettings . filename ) . then ( function ( success ) {
403
+ if ( ionic . Platform . isIOS ( ) ) {
404
+ dialogMessage = 'MSG_BACKUP_SAVED_PERSISTENT_IOS' ;
405
+ } else {
406
+ dialogMessage = 'MSG_BACKUP_SAVED_PERSISTENT_ANDROID' ;
407
+ }
408
+
409
+ return Promise . resolve ( ) ;
410
+ } ) . catch ( function ( ) {
411
+ $log . log ( "checking for backup PDF failed" ) ;
412
+ } ) . then ( function ( ) {
413
+ return $cordovaDialogs . alert (
414
+ $translate . instant ( dialogMessage ) . sentenceCase ( ) ,
415
+ $translate . instant ( 'SETTINGS_BACKUP_COMPLETE' ) . sentenceCase ( ) ,
416
+ $translate . instant ( 'OK' )
417
+ )
418
+ } ) . then ( function ( ) {
419
+ $btBackButtonDelegate . goBack ( ) ;
420
+ return false ;
421
+ } ) ;
422
+ } ) ;
397
423
}
398
424
} ;
399
425
@@ -868,14 +894,22 @@ angular.module('blocktrail.wallet')
868
894
$ionicActionSheet , $log , $cordovaFileOpener2 , $cordovaFile , sdkService , $cordovaEmailComposer ,
869
895
launchService , settingsService , $timeout ) {
870
896
if ( ! backupInfo ) {
871
- $cordovaDialogs . alert (
872
- $translate . instant ( 'MSG_BACKUP_SAVED_ALREADY' ) . sentenceCase ( ) ,
873
- $translate . instant ( 'SETTINGS_BACKUP_COMPLETE' ) . sentenceCase ( ) ,
874
- $translate . instant ( 'OK' )
875
- ) . then ( function ( ) {
876
- $btBackButtonDelegate . goBack ( ) ;
897
+ settingsService . $isLoaded ( ) . then ( function ( ) {
898
+
899
+ $log . log ( settingsService . backupSavedPersistent , ionic . Platform . isIOS ( ) ) ;
900
+
901
+ var dialogMessage = ( settingsService . backupSavedPersistent && ionic . Platform . isIOS ( ) )
902
+ ? 'MSG_BACKUP_SAVED_PERSISTENT' : 'MSG_BACKUP_SAVED_ALREADY' ;
903
+
904
+ $cordovaDialogs . alert (
905
+ $translate . instant ( dialogMessage ) . sentenceCase ( ) ,
906
+ $translate . instant ( 'SETTINGS_BACKUP_COMPLETE' ) . sentenceCase ( ) ,
907
+ $translate . instant ( 'OK' )
908
+ ) . then ( function ( ) {
909
+ $btBackButtonDelegate . goBack ( ) ;
910
+ } ) ;
911
+ return false ;
877
912
} ) ;
878
- return false ;
879
913
}
880
914
881
915
$scope . appControl = {
@@ -914,10 +948,11 @@ angular.module('blocktrail.wallet')
914
948
inputMode : 'M' ,
915
949
image : true
916
950
} ;
951
+
917
952
$scope . backupSettings = {
918
953
//NB: on android fileOpener2 only works with SD storage (i.e. non-private storage)
919
- path : window . cordova ? ( ionic . Platform . isAndroid ( ) ? cordova . file . externalDataDirectory : cordova . file . dataDirectory ) : null ,
920
- filename : 'btc-wallet-backup.pdf' ,
954
+ path : window . cordova ? ( ionic . Platform . isAndroid ( ) ? cordova . file . externalDataDirectory : cordova . file . documentsDirectory ) : null ,
955
+ filename : 'btc-wallet-backup-' + backupInfo . identifier + ' .pdf',
921
956
replace : true
922
957
} ;
923
958
@@ -926,13 +961,8 @@ angular.module('blocktrail.wallet')
926
961
//Temporary handling of a bug in iOS with the $cordovaFileOpener2
927
962
var optionButtons = [
928
963
{ text : $translate . instant ( 'BACKUP_EMAIL_PDF' ) } ,
929
- { text : $translate . instant ( 'BACKUP_CREATE_PDF ' ) } ,
964
+ { text : $translate . instant ( 'BACKUP_OPEN_PDF ' ) }
930
965
] ;
931
- if ( ionic . Platform . isIOS ( ) ) {
932
- optionButtons = [
933
- { text : $translate . instant ( 'BACKUP_EMAIL_PDF' ) } ,
934
- ] ;
935
- }
936
966
937
967
$scope . hideExportOptions = $ionicActionSheet . show ( {
938
968
buttons : optionButtons ,
@@ -987,6 +1017,7 @@ angular.module('blocktrail.wallet')
987
1017
return $cordovaFile . writeFile ( $scope . backupSettings . path , $scope . backupSettings . filename , buffer , $scope . backupSettings . replace ) ;
988
1018
} )
989
1019
. then ( function ( result ) {
1020
+ // Options for saving
990
1021
if ( index == 0 ) {
991
1022
//email the backup pdf
992
1023
var options = {
@@ -1001,33 +1032,52 @@ angular.module('blocktrail.wallet')
1001
1032
var deferred = $q . defer ( ) ;
1002
1033
1003
1034
//check that emails can be sent (try with normal mail, can't do attachments with gmail)
1004
- cordova . plugins . email . isAvailable ( function ( isAvailable ) {
1035
+ cordova . plugins . email . isAvailable ( function ( isAvailable ) {
1005
1036
$log . debug ( 'is email supported? ' + isAvailable ) ;
1006
1037
if ( isAvailable ) {
1007
1038
$scope . appControl . saveButtonClicked = true ;
1008
- cordova . plugins . email . open ( options , function ( result ) {
1039
+ cordova . plugins . email . open ( options , function ( result ) {
1009
1040
deferred . resolve ( result ) ;
1010
1041
} ) ;
1011
1042
} else {
1012
1043
//no mail support...sad times :(
1013
1044
$cordovaDialogs . alert (
1014
- $translate . instant ( 'MSG_EMAIL_NOT_SETUP' ) . sentenceCase ( ) ,
1015
- $translate . instant ( 'SORRY' ) . sentenceCase ( ) ,
1045
+ $translate . instant ( 'MSG_EMAIL_NOT_SETUP' ) ,
1046
+ $translate . instant ( 'SORRY' ) ,
1016
1047
$translate . instant ( 'OK' )
1017
- ) . then ( function ( ) {
1018
- deferred . reject ( 'NO_EMAIL' ) ;
1019
- } ) ;
1048
+ ) ;
1020
1049
}
1021
1050
} ) ;
1022
1051
1023
1052
return deferred . promise ;
1024
1053
1025
1054
} else if ( index == 1 ) {
1026
- //export the backup to PDF for user to handle
1027
- //call an intent or similar service to allow user decide what to do with PDF
1028
- $log . debug ( 'opening file ' + $scope . backupSettings . path + $scope . backupSettings . filename ) ;
1029
- $scope . appControl . saveButtonClicked = true ;
1030
- return $cordovaFileOpener2 . open ( $scope . backupSettings . path + $scope . backupSettings . filename , 'application/pdf' ) ;
1055
+ var msg = 'BACKUP_EXPORT_PDF_ANDROID_INFO' ;
1056
+ if ( ionic . Platform . isIOS ( ) ) {
1057
+ msg = 'BACKUP_EXPORT_PDF_IOS_INFO' ;
1058
+ }
1059
+
1060
+ return $cordovaDialogs . alert (
1061
+ $translate . instant ( msg ) ,
1062
+ $translate . instant ( 'IMPORTANT' ) ,
1063
+ $translate . instant ( 'OK' )
1064
+ ) . then ( function ( ) {
1065
+ $log . debug ( 'opening file ' + $scope . backupSettings . path + $scope . backupSettings . filename ) ;
1066
+
1067
+ if ( ionic . Platform . isIOS ( ) ) {
1068
+ cordova . plugins . disusered . open ( $scope . backupSettings . path + $scope . backupSettings . filename ,
1069
+ function ( ) {
1070
+ $scope . appControl . saveButtonClicked = true ;
1071
+ } ,
1072
+ function ( err ) {
1073
+ console . log ( err . message , err ) ;
1074
+ }
1075
+ ) ;
1076
+ } else {
1077
+ $scope . appControl . saveButtonClicked = true ;
1078
+ return $cordovaFileOpener2 . open ( $scope . backupSettings . path + $scope . backupSettings . filename , 'application/pdf' ) ;
1079
+ }
1080
+ } ) ;
1031
1081
}
1032
1082
} )
1033
1083
. then ( function ( ) {
@@ -1068,19 +1118,33 @@ angular.module('blocktrail.wallet')
1068
1118
settingsService . $isLoaded ( ) . then ( function ( ) {
1069
1119
settingsService . backupSaved = true ;
1070
1120
settingsService . backupSkipped = false ;
1071
- settingsService . $store ( ) ;
1121
+ return settingsService . $store ( ) ;
1122
+ } ) . then ( function ( ) {
1123
+ $cordovaDialogs . confirm (
1124
+ $translate . instant ( "BACKUP_OPTION_KEEP_ON_PHONE" ) ,
1125
+ $translate . instant ( "IMPORTANT" ) ,
1126
+ [
1127
+ $translate . instant ( "YES" ) ,
1128
+ $translate . instant ( "NO" )
1129
+ ] )
1130
+ . then ( function ( dialogResult ) {
1131
+
1132
+ if ( dialogResult == 1 ) {
1133
+ settingsService . backupSavedPersistent = true ;
1134
+
1135
+ console . log ( 'keeping backup' ) ;
1136
+ $scope . backupSettings . keepBackup = true ;
1137
+ return settingsService . $store ( ) ;
1138
+ } else {
1139
+ console . log ( 'not keeping backup' ) ;
1140
+ //delete the temporary backup file if created
1141
+ return $cordovaFile . removeFile ( $scope . backupSettings . path , $scope . backupSettings . filename ) ;
1142
+ }
1143
+ } )
1144
+ . then ( function ( ) {
1145
+ $btBackButtonDelegate . goBack ( ) ;
1146
+ } ) ;
1072
1147
} ) ;
1073
-
1074
- //delete the temporary backup file if created
1075
- $cordovaFile . removeFile ( $scope . backupSettings . path , $scope . backupSettings . filename )
1076
- . then ( function ( ) {
1077
- $log . debug ( 'deleted file ' + $scope . backupSettings . path + $scope . backupSettings . filename ) ;
1078
- } , function ( err ) {
1079
- $log . debug ( 'unable to delete temp wallet backup file' + err ) ;
1080
- } ) ;
1081
-
1082
- //onwards to phone number and contacts setup
1083
- $btBackButtonDelegate . goBack ( ) ;
1084
1148
} )
1085
1149
. catch ( function ( err ) {
1086
1150
console . error ( err ) ;
0 commit comments