11import { NativeModules , NetInfo , Platform } from 'react-native' ;
2- import { download , isFileExists } from './lib/downloadHelper' ;
2+ import { deleteFile , download , isFileExists } from './lib/downloadHelper' ;
33
44const { RNEasyUpgrade } = NativeModules ;
55
@@ -35,7 +35,7 @@ class AppUpgrade {
3535 }
3636
3737 get downloadDestPath ( ) {
38- return this . options . downloadDestDirectory + '/' + this . options . downloadApkName ;
38+ return this . options . downloadDestDirectory + '/' + this . options . downloadApkName ;
3939 }
4040
4141 get downloadDestDirectory ( ) {
@@ -47,7 +47,7 @@ class AppUpgrade {
4747 }
4848
4949 _handleError ( err ) {
50- console . log ( 'downloadApkError ' , err ) ;
50+ console . log ( 'EasyUpgradeError ' , err ) ;
5151 this . options . onError ( err ) ;
5252 }
5353
@@ -63,20 +63,39 @@ class AppUpgrade {
6363
6464 checkApkHasDownloaded = async ( path = this . downloadDestPath ) => {
6565 return await isFileExists ( path ) ;
66- } ;
66+ }
67+ /**
68+ * download file
69+ * @param fileUrl
70+ * @param downloadConf
71+ */
72+ downloadFile ( fileUrl , downloadConf = { } ) {
73+ jobId = 1 ;
74+ download ( fileUrl , downloadConf )
75+ . then ( async res => {
76+ jobId = - 1 ;
77+ if ( this . options . shouldCheckApkHasDownloaded ) {
78+ await RNEasyUpgrade . moveFile ( downloadConf . tempDownloadPath , this . downloadDestPath ) ;
79+ }
80+ this . options . downloadApkEnd ( this . downloadDestPath ) ;
81+ } )
82+ . catch ( err => {
83+ jobId = - 1 ;
84+ this . _handleError ( err ) ;
85+ } ) ;
86+ }
6787
6888 /**
6989 * update app and install
7090 * @param apkUrl
7191 */
72- updateAndroidApp ( apkUrl ) {
92+ async updateAndroidApp ( apkUrl ) {
7393 if ( this . downloading ) {
7494 return ;
7595 }
7696
7797 const tempDownloadApkName = 'temp_download.apk' ;
7898 const tempDownloadPath = this . downloadDestDirectory + '/' + tempDownloadApkName ;
79-
8099 const downloadConf = {
81100 downloadTitle : this . options . downloadTitle ,
82101 downloadDescription : this . options . downloadDescription ,
@@ -85,26 +104,23 @@ class AppUpgrade {
85104 allowedInMetered : true ,
86105 showInDownloads : true ,
87106 external : true , //when false basically means use the default Download path
88- path : this . downloadDestDirectory //if "external" is true then use this path
107+ path : this . downloadDestDirectory , //if "external" is true then use this path
108+ tempDownloadPath : tempDownloadPath
89109 } ;
90- jobId = 1 ;
91- download ( apkUrl , downloadConf )
92- . then ( async res => {
93- jobId = - 1 ;
94- if ( this . options . shouldCheckApkHasDownloaded ) {
95- await RNEasyUpgrade . moveFile ( tempDownloadPath , this . downloadDestPath ) ;
110+ if ( this . options . shouldCheckApkHasDownloaded ) {
111+ try {
112+ const isTempFileExisted = await isFileExists ( tempDownloadPath ) ;
113+ if ( isTempFileExisted ) {
114+ await deleteFile ( tempDownloadPath ) ;
96115 }
97- this . options . downloadApkEnd ( this . downloadDestPath ) ;
98- } )
99- . catch ( err => {
100- console . log ( err ) ;
101- jobId = - 1 ;
116+ } catch ( err ) {
102117 this . _handleError ( err ) ;
103- } ) ;
118+ }
119+ }
120+ this . downloadFile ( apkUrl , downloadConf ) ;
104121 }
105122
106123 installApk ( apkPath = this . downloadDestPath ) {
107- console . log ( apkPath ) ;
108124 RNEasyUpgrade . installApk ( apkPath ) ;
109125 }
110126
0 commit comments