1
1
import { Zip } from "nativescript-zip" ;
2
- import * as appSettings from "tns-core-modules/application-settings" ;
3
- import * as fs from "tns-core-modules/file-system" ;
4
- import * as fsa from "tns-core-modules/file-system/file-system-access" ;
5
- import { isIOS } from "tns-core-modules/platform" ;
6
- import * as utils from "tns-core-modules/utils/utils" ;
2
+ import { isIOS , ApplicationSettings , knownFolders , File , Folder , Utils } from "@nativescript/core" ;
3
+ import { FileSystemAccess } from "@nativescript/core/file-system/file-system-access" ;
7
4
import { AppSync } from "./app-sync" ;
8
5
import { TNSAcquisitionManager } from "./TNSAcquisitionManager" ;
9
6
@@ -30,15 +27,15 @@ export class TNSLocalPackage implements ILocalPackage {
30
27
serverUrl : string ;
31
28
32
29
install ( installSuccess : Function , errorCallback ?: ErrorCallback , installOptions ?: InstallOptions ) : void {
33
- let appFolderPath = fs . knownFolders . documents ( ) . path + "/app" ;
34
- let unzipFolderPath = fs . knownFolders . documents ( ) . path + "/AppSync-Unzipped/" + this . packageHash ;
35
- let appSyncFolder = fs . knownFolders . documents ( ) . path + "/AppSync" ;
30
+ let appFolderPath = knownFolders . documents ( ) . path + "/app" ;
31
+ let unzipFolderPath = knownFolders . documents ( ) . path + "/AppSync-Unzipped/" + this . packageHash ;
32
+ let appSyncFolder = knownFolders . documents ( ) . path + "/AppSync" ;
36
33
// make sure the AppSync folder exists
37
- fs . Folder . fromPath ( appSyncFolder ) ;
38
- let newPackageFolderPath = fs . knownFolders . documents ( ) . path + "/AppSync/" + this . packageHash ;
34
+ Folder . fromPath ( appSyncFolder ) ;
35
+ let newPackageFolderPath = knownFolders . documents ( ) . path + "/AppSync/" + this . packageHash ;
39
36
// in case of a rollback make 'newPackageFolderPath' could already exist, so check and remove
40
- if ( fs . Folder . exists ( newPackageFolderPath ) ) {
41
- fs . Folder . fromPath ( newPackageFolderPath ) . removeSync ( ) ;
37
+ if ( Folder . exists ( newPackageFolderPath ) ) {
38
+ Folder . fromPath ( newPackageFolderPath ) . removeSync ( ) ;
42
39
}
43
40
44
41
const onUnzipComplete = ( success : boolean , error ?: string ) => {
@@ -48,10 +45,10 @@ export class TNSLocalPackage implements ILocalPackage {
48
45
return ;
49
46
}
50
47
51
- const previousHash = appSettings . getString ( AppSync . CURRENT_HASH_KEY , null ) ;
52
- const isDiffPackage = fs . File . exists ( unzipFolderPath + "/hotappsync.json" ) ;
48
+ const previousHash = ApplicationSettings . getString ( AppSync . CURRENT_HASH_KEY , null ) ;
49
+ const isDiffPackage = File . exists ( unzipFolderPath + "/hotappsync.json" ) ;
53
50
if ( isDiffPackage ) {
54
- const copySourceFolder = previousHash === null ? appFolderPath : fs . knownFolders . documents ( ) . path + "/AppSync/" + previousHash ;
51
+ const copySourceFolder = previousHash === null ? appFolderPath : knownFolders . documents ( ) . path + "/AppSync/" + previousHash ;
55
52
if ( ! TNSLocalPackage . copyFolder ( copySourceFolder , newPackageFolderPath ) ) {
56
53
errorCallback && errorCallback ( new Error ( `Failed to copy ${ copySourceFolder } to ${ newPackageFolderPath } ` ) ) ;
57
54
return ;
@@ -61,39 +58,39 @@ export class TNSLocalPackage implements ILocalPackage {
61
58
return ;
62
59
}
63
60
} else {
64
- new fsa . FileSystemAccess ( ) . rename ( unzipFolderPath , newPackageFolderPath , ( error ) => {
61
+ new FileSystemAccess ( ) . rename ( unzipFolderPath , newPackageFolderPath , ( error ) => {
65
62
errorCallback && errorCallback ( new Error ( error ) ) ;
66
63
return ;
67
64
} ) ;
68
65
}
69
66
70
67
if ( ! isIOS ) {
71
- let pendingFolderPath = fs . knownFolders . documents ( ) . path + "/AppSync/pending" ;
72
- if ( fs . Folder . exists ( pendingFolderPath ) ) {
73
- fs . Folder . fromPath ( pendingFolderPath ) . removeSync ( ) ;
68
+ let pendingFolderPath = knownFolders . documents ( ) . path + "/AppSync/pending" ;
69
+ if ( Folder . exists ( pendingFolderPath ) ) {
70
+ Folder . fromPath ( pendingFolderPath ) . removeSync ( ) ;
74
71
}
75
72
if ( ! TNSLocalPackage . copyFolder ( newPackageFolderPath , pendingFolderPath ) ) {
76
73
errorCallback && errorCallback ( new Error ( `Failed to copy ${ newPackageFolderPath } to ${ pendingFolderPath } ` ) ) ;
77
74
return ;
78
75
}
79
76
}
80
77
81
- appSettings . setString ( TNSLocalPackage . APPSYNC_CURRENT_APPVERSION , this . appVersion ) ;
78
+ ApplicationSettings . setString ( TNSLocalPackage . APPSYNC_CURRENT_APPVERSION , this . appVersion ) ;
82
79
TNSLocalPackage . saveCurrentPackage ( this ) ;
83
80
84
81
let buildTime : string ;
85
82
// Note that this 'if' hardly justifies subclassing so we're not
86
83
if ( isIOS ) {
87
84
const plist = NSBundle . mainBundle . pathForResourceOfType ( null , "plist" ) ;
88
- const fileDate = new fsa . FileSystemAccess ( ) . getLastModified ( plist ) ;
85
+ const fileDate = new FileSystemAccess ( ) . getLastModified ( plist ) ;
89
86
buildTime = "" + fileDate . getTime ( ) ;
90
87
} else {
91
- const appSyncApkBuildTimeStringId = utils . ad . resources . getStringId ( TNSLocalPackage . APPSYNC_APK_BUILD_TIME ) ;
92
- buildTime = utils . ad . getApplicationContext ( ) . getResources ( ) . getString ( appSyncApkBuildTimeStringId ) ;
88
+ const appSyncApkBuildTimeStringId = Utils . android . resources . getStringId ( TNSLocalPackage . APPSYNC_APK_BUILD_TIME ) ;
89
+ buildTime = Utils . android . getApplicationContext ( ) . getResources ( ) . getString ( appSyncApkBuildTimeStringId ) ;
93
90
}
94
- appSettings . setString ( TNSLocalPackage . APPSYNC_CURRENT_APPBUILDTIME , buildTime ) ;
91
+ ApplicationSettings . setString ( TNSLocalPackage . APPSYNC_CURRENT_APPBUILDTIME , buildTime ) ;
95
92
//noinspection JSIgnoredPromiseFromCall (removal is async, don't really care if it fails)
96
- fs . File . fromPath ( this . localPath ) . remove ( ) ;
93
+ File . fromPath ( this . localPath ) . remove ( ) ;
97
94
98
95
installSuccess ( ) ;
99
96
} ;
@@ -121,7 +118,11 @@ export class TNSLocalPackage implements ILocalPackage {
121
118
}
122
119
) ;
123
120
} else {
124
- Zip . unzipWithProgress ( archive , destination , progressCallback ) . then (
121
+ Zip . unzip ( {
122
+ archive,
123
+ directory : destination ,
124
+ onProgress : progressCallback
125
+ } ) . then (
125
126
( ) => {
126
127
completionCallback ( true ) ;
127
128
} ,
@@ -138,20 +139,20 @@ export class TNSLocalPackage implements ILocalPackage {
138
139
return ;
139
140
}
140
141
141
- appSettings . remove ( TNSLocalPackage . APPSYNC_CURRENT_APPVERSION ) ;
142
- appSettings . remove ( TNSLocalPackage . APPSYNC_CURRENT_APPBUILDTIME ) ;
142
+ ApplicationSettings . remove ( TNSLocalPackage . APPSYNC_CURRENT_APPVERSION ) ;
143
+ ApplicationSettings . remove ( TNSLocalPackage . APPSYNC_CURRENT_APPBUILDTIME ) ;
143
144
144
- const appSyncFolder = fs . Folder . fromPath ( fs . knownFolders . documents ( ) . path + "/AppSync" ) ;
145
+ const appSyncFolder = Folder . fromPath ( knownFolders . documents ( ) . path + "/AppSync" ) ;
145
146
//noinspection JSIgnoredPromiseFromCall
146
147
appSyncFolder . clear ( ) ;
147
148
}
148
149
149
150
private static saveCurrentPackage ( pack : IPackage ) : void {
150
- appSettings . setString ( TNSLocalPackage . APPSYNC_CURRENT_PACKAGE , JSON . stringify ( pack ) ) ;
151
+ ApplicationSettings . setString ( TNSLocalPackage . APPSYNC_CURRENT_PACKAGE , JSON . stringify ( pack ) ) ;
151
152
}
152
153
153
154
static getCurrentPackage ( ) : IPackage {
154
- const packageStr : string = appSettings . getString ( TNSLocalPackage . APPSYNC_CURRENT_PACKAGE , null ) ;
155
+ const packageStr : string = ApplicationSettings . getString ( TNSLocalPackage . APPSYNC_CURRENT_PACKAGE , null ) ;
155
156
return packageStr === null ? null : JSON . parse ( packageStr ) ;
156
157
}
157
158
0 commit comments