Skip to content

Commit 32a6ef8

Browse files
Files written to the file-system are lost after update #20
1 parent e04f01d commit 32a6ef8

File tree

10 files changed

+80
-30
lines changed

10 files changed

+80
-30
lines changed

demo/appresources/Android/AndroidManifest.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="__PACKAGE__"
4-
android:versionCode="1"
5-
android:versionName="1.0.0">
4+
android:versionCode="2"
5+
android:versionName="1.1.0">
66

77
<supports-screens
88
android:smallScreens="true"

demo/appresources/iOS/Info.plist

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
<key>CFBundlePackageType</key>
1616
<string>APPL</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.0.0</string>
18+
<string>1.1.0</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>
22-
<string>1.0</string>
22+
<string>1.1</string>
2323
<key>LSRequiresIPhoneOS</key>
2424
<true/>
2525
<key>UILaunchStoryboardName</key>

demo/demoapp/main-page.xml

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded" class="page">
2-
<StackLayout class="p-20">
2+
<StackLayout class="p-20">
33

4-
<!-- <Label text="I'm AppSync iOS" class="t-20 text-center c-orange" textWrap="true"/>-->
5-
<!-- <Label text="I'm AppSync Android (mandatory)" class="t-20 text-center c-orange" textWrap="true"/>-->
6-
<Label text="I'm the APPSTORE version.." class="t-20 text-center c-orange" textWrap="true"/>
4+
<!-- <Label text="I'm AppSync iOS" class="t-20 text-center c-orange" textWrap="true"/>-->
5+
<!-- <Label text="I'm AppSync Android (mandatory)" class="t-20 text-center c-orange" textWrap="true"/>-->
6+
<Label text="I'm the APPSTORE version.." class="t-20 text-center c-orange" textWrap="true"/>
77

8-
<Label text="{{ message }}" class="t-20 m-30 text-center c-black" textWrap="true"/>
9-
</StackLayout>
8+
<Label text="{{ message }}" class="t-20 m-30 text-center c-black" textWrap="true"/>
9+
10+
<Button tap="{{ setAppSettings }}" text="set appsettings"/>
11+
<Button tap="{{ getAppSettings }}" text="get appsettings"/>
12+
13+
<Button tap="{{ setFileSystem }}" text="set filesystem"/>
14+
<Button tap="{{ getFileSystem }}" text="get filesystem"/>
15+
</StackLayout>
1016

1117
</Page>

demo/demoapp/main-view-model.ts

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import { AppSync, InstallMode, SyncStatus } from "nativescript-app-sync";
22
import * as application from "tns-core-modules/application";
3+
import * as appSettings from "tns-core-modules/application-settings";
4+
import { knownFolders } from "tns-core-modules/file-system" ;
35
import { Observable } from "tns-core-modules/data/observable";
46
import { isIOS } from "tns-core-modules/platform";
57

8+
const documents = knownFolders.documents();
9+
const txtFile = documents.getFile("/app/something.txt");
10+
611
export class HelloWorldModel extends Observable {
712
private appSync: AppSync;
813

14+
private static APPSETTINGS_KEY = "appsettings_key";
15+
916
private static APPSYNC_IOS_STAGING_KEY = "QA5daorV624ZP3p0FbFkngdZasME4ksvOXqog";
1017
private static APPSYNC_IOS_PRODUCTION_KEY = "rOw06mG4jrWfU8wkoKY7WHM2LhVa4ksvOXqog";
1118

@@ -24,6 +31,42 @@ export class HelloWorldModel extends Observable {
2431
});
2532
}
2633

34+
public setAppSettings() {
35+
const d = new Date();
36+
appSettings.setString(HelloWorldModel.APPSETTINGS_KEY, "AppSettings value set at " + d);
37+
this.set("message", "Written date to AppSettings: " + d);
38+
}
39+
40+
public getAppSettings() {
41+
this.set("message", appSettings.getString(HelloWorldModel.APPSETTINGS_KEY));
42+
}
43+
44+
public setFileSystem() {
45+
const d = new Date();
46+
txtFile.writeText("FileSystem value set at " + d)
47+
.then(() => {
48+
const value = "Written date to " + txtFile.path + ": " + d;
49+
this.set("message", value);
50+
console.log(value);
51+
})
52+
.catch(err => {
53+
this.set("message", "Error writing to " + txtFile.path + ": " + err);
54+
console.log("Error writing to " + txtFile.path + ": " + err);
55+
});
56+
}
57+
58+
public getFileSystem() {
59+
txtFile.readText()
60+
.then(content => {
61+
this.set("message", "Got from " + txtFile.path + ": " + content);
62+
console.log("Written to " + txtFile.path + ": " + content);
63+
})
64+
.catch(err => {
65+
this.set("message", "Error reading from " + txtFile.path + ": " + err);
66+
console.log("Error reading from " + txtFile.path + ": " + err);
67+
});
68+
}
69+
2770
private syncWithAppSyncServer(): void {
2871
this.set("message", "Querying AppSync..");
2972
AppSync.sync({
@@ -46,4 +89,4 @@ export class HelloWorldModel extends Observable {
4689
}
4790
});
4891
}
49-
}
92+
}

demo/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"ci.tslint": "npm i && tslint --config '../tslint.json' 'demoapp/**/*.ts' --exclude '**/node_modules/**' --exclude '**/typings/**'"
3232
},
3333
"dependencies": {
34-
"nativescript-app-sync": "file:../publish/package/nativescript-app-sync-1.0.1.tgz",
34+
"nativescript-app-sync": "file:../publish/package/nativescript-app-sync-1.0.2.tgz",
3535
"nativescript-theme-core": "~1.0.4",
3636
"nativescript-unit-test-runner": "^0.3.4",
3737
"tns-core-modules": "~5.4.2"

src/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-app-sync",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Use AppSync to hot deploy updates to your app.",
55
"main": "app-sync",
66
"typings": "index.d.ts",

src/platforms/android/tnsappsync.aar

43 Bytes
Binary file not shown.

src/platforms/android_lib/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ Using this wrapper to more easily copy some files around and (optionally) move s
44

55
### Building the framework
66
- Clone this repo
7-
- Start Android Studio and pick 'open an existing project' > {this repo}/platforms/android_lib
7+
- Start Android Studio and pick "Open an existing Android Studio project" ➡️ `{this repo}/platforms/android_lib`
88
- Update the `/src/main` folder as needed
99
- Open the Gradle toolwindow
10-
- Run tnscodepush > Tasks > build > build
11-
- The (release) .aar will be generated in tnscodepush/build/outputs/aar
10+
- Run tnsappsync > Tasks > build > build
11+
- The (release) .aar will be generated in tnsappsync/build/outputs/aar
1212
- Copy that to the platforms/android folder, replacing the old .aar
1313
- Commit and push the changes as usual

src/platforms/android_lib/tnsappsync/src/main/java/com/tns/TNSAppSync.java

+12-13
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,18 @@ static void activatePackage(final Context context) {
4444
}
4545
}
4646

47-
// move /app to /app_backup
48-
final File appFolder = new File(context.getFilesDir().getPath() + "/app");
49-
50-
if (appFolder.renameTo(appBackupFolder)) {
51-
// move pending to /app
52-
if (pendingPackage.renameTo(appFolder)) {
53-
// as long as the app wasn't restarted after an AppSync update, this key would exist to control JS behavior
54-
removePendingHash(context);
55-
} else {
56-
// next to impossible, but just to be sure:
57-
System.out.println("--- rename package to app failed");
58-
appBackupFolder.renameTo(appFolder);
59-
}
47+
// copy /app to /app_backup (not renaming the folder, because we want to retain anything the app saved to the app folder at runtime)
48+
// (example: if your app saves a file locally to /stuff/something.txt, it ends up as /data/user/0/org.nativescript.plugindemo.AppSync/files/stuff/something.txt)
49+
try {
50+
copyDirectoryContents(context.getFilesDir().getPath() + "/app", context.getFilesDir().getPath() + "/app_backup");
51+
52+
copyDirectoryContents(pendingPackagePath, context.getFilesDir().getPath() + "/app");
53+
54+
// as long as the app wasn't restarted after an AppSync update, this key would exist to control JS behavior
55+
removePendingHash(context);
56+
} catch (Exception e) {
57+
System.out.println("--- installing app update failed: " + e.getMessage());
58+
e.printStackTrace();
6059
}
6160
}
6261

src/platforms/android_lib/tnsappsync/tnsappsync.iml

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
<option name="ALLOW_USER_CONFIGURATION" value="false" />
1818
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
1919
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
20+
<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res;file://$MODULE_DIR$/build/generated/res/resValues/debug" />
21+
<option name="TEST_RES_FOLDERS_RELATIVE_PATH" value="" />
2022
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
2123
<option name="PROJECT_TYPE" value="1" />
2224
</configuration>
@@ -97,7 +99,6 @@
9799
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/library_assets" />
98100
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/library_java_res" />
99101
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/library_manifest" />
100-
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint_jar" />
101102
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_jni_libs" />
102103
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_manifests" />
103104
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_shaders" />
@@ -107,6 +108,7 @@
107108
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
108109
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shader_assets" />
109110
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
111+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/tmp" />
110112
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
111113
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
112114
<excludeFolder url="file://$MODULE_DIR$/build/reports" />

0 commit comments

Comments
 (0)