Skip to content

Commit

Permalink
Files written to the file-system are lost after update #20
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Jul 7, 2019
1 parent e04f01d commit 32a6ef8
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 30 deletions.
4 changes: 2 additions & 2 deletions demo/appresources/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="__PACKAGE__"
android:versionCode="1"
android:versionName="1.0.0">
android:versionCode="2"
android:versionName="1.1.0">

<supports-screens
android:smallScreens="true"
Expand Down
4 changes: 2 additions & 2 deletions demo/appresources/iOS/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0.0</string>
<string>1.1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<string>1.1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
Expand Down
18 changes: 12 additions & 6 deletions demo/demoapp/main-page.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded" class="page">
<StackLayout class="p-20">
<StackLayout class="p-20">

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

<Label text="{{ message }}" class="t-20 m-30 text-center c-black" textWrap="true"/>
</StackLayout>
<Label text="{{ message }}" class="t-20 m-30 text-center c-black" textWrap="true"/>

<Button tap="{{ setAppSettings }}" text="set appsettings"/>
<Button tap="{{ getAppSettings }}" text="get appsettings"/>

<Button tap="{{ setFileSystem }}" text="set filesystem"/>
<Button tap="{{ getFileSystem }}" text="get filesystem"/>
</StackLayout>

</Page>
45 changes: 44 additions & 1 deletion demo/demoapp/main-view-model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { AppSync, InstallMode, SyncStatus } from "nativescript-app-sync";
import * as application from "tns-core-modules/application";
import * as appSettings from "tns-core-modules/application-settings";
import { knownFolders } from "tns-core-modules/file-system" ;
import { Observable } from "tns-core-modules/data/observable";
import { isIOS } from "tns-core-modules/platform";

const documents = knownFolders.documents();
const txtFile = documents.getFile("/app/something.txt");

export class HelloWorldModel extends Observable {
private appSync: AppSync;

private static APPSETTINGS_KEY = "appsettings_key";

private static APPSYNC_IOS_STAGING_KEY = "QA5daorV624ZP3p0FbFkngdZasME4ksvOXqog";
private static APPSYNC_IOS_PRODUCTION_KEY = "rOw06mG4jrWfU8wkoKY7WHM2LhVa4ksvOXqog";

Expand All @@ -24,6 +31,42 @@ export class HelloWorldModel extends Observable {
});
}

public setAppSettings() {
const d = new Date();
appSettings.setString(HelloWorldModel.APPSETTINGS_KEY, "AppSettings value set at " + d);
this.set("message", "Written date to AppSettings: " + d);
}

public getAppSettings() {
this.set("message", appSettings.getString(HelloWorldModel.APPSETTINGS_KEY));
}

public setFileSystem() {
const d = new Date();
txtFile.writeText("FileSystem value set at " + d)
.then(() => {
const value = "Written date to " + txtFile.path + ": " + d;
this.set("message", value);
console.log(value);
})
.catch(err => {
this.set("message", "Error writing to " + txtFile.path + ": " + err);
console.log("Error writing to " + txtFile.path + ": " + err);
});
}

public getFileSystem() {
txtFile.readText()
.then(content => {
this.set("message", "Got from " + txtFile.path + ": " + content);
console.log("Written to " + txtFile.path + ": " + content);
})
.catch(err => {
this.set("message", "Error reading from " + txtFile.path + ": " + err);
console.log("Error reading from " + txtFile.path + ": " + err);
});
}

private syncWithAppSyncServer(): void {
this.set("message", "Querying AppSync..");
AppSync.sync({
Expand All @@ -46,4 +89,4 @@ export class HelloWorldModel extends Observable {
}
});
}
}
}
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"ci.tslint": "npm i && tslint --config '../tslint.json' 'demoapp/**/*.ts' --exclude '**/node_modules/**' --exclude '**/typings/**'"
},
"dependencies": {
"nativescript-app-sync": "file:../publish/package/nativescript-app-sync-1.0.1.tgz",
"nativescript-app-sync": "file:../publish/package/nativescript-app-sync-1.0.2.tgz",
"nativescript-theme-core": "~1.0.4",
"nativescript-unit-test-runner": "^0.3.4",
"tns-core-modules": "~5.4.2"
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-app-sync",
"version": "1.0.1",
"version": "1.0.2",
"description": "Use AppSync to hot deploy updates to your app.",
"main": "app-sync",
"typings": "index.d.ts",
Expand Down
Binary file modified src/platforms/android/tnsappsync.aar
Binary file not shown.
6 changes: 3 additions & 3 deletions src/platforms/android_lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ Using this wrapper to more easily copy some files around and (optionally) move s

### Building the framework
- Clone this repo
- Start Android Studio and pick 'open an existing project' > {this repo}/platforms/android_lib
- Start Android Studio and pick "Open an existing Android Studio project" ➡️ `{this repo}/platforms/android_lib`
- Update the `/src/main` folder as needed
- Open the Gradle toolwindow
- Run tnscodepush > Tasks > build > build
- The (release) .aar will be generated in tnscodepush/build/outputs/aar
- Run tnsappsync > Tasks > build > build
- The (release) .aar will be generated in tnsappsync/build/outputs/aar
- Copy that to the platforms/android folder, replacing the old .aar
- Commit and push the changes as usual
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,18 @@ static void activatePackage(final Context context) {
}
}

// move /app to /app_backup
final File appFolder = new File(context.getFilesDir().getPath() + "/app");

if (appFolder.renameTo(appBackupFolder)) {
// move pending to /app
if (pendingPackage.renameTo(appFolder)) {
// as long as the app wasn't restarted after an AppSync update, this key would exist to control JS behavior
removePendingHash(context);
} else {
// next to impossible, but just to be sure:
System.out.println("--- rename package to app failed");
appBackupFolder.renameTo(appFolder);
}
// copy /app to /app_backup (not renaming the folder, because we want to retain anything the app saved to the app folder at runtime)
// (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)
try {
copyDirectoryContents(context.getFilesDir().getPath() + "/app", context.getFilesDir().getPath() + "/app_backup");

copyDirectoryContents(pendingPackagePath, context.getFilesDir().getPath() + "/app");

// as long as the app wasn't restarted after an AppSync update, this key would exist to control JS behavior
removePendingHash(context);
} catch (Exception e) {
System.out.println("--- installing app update failed: " + e.getMessage());
e.printStackTrace();
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/platforms/android_lib/tnsappsync/tnsappsync.iml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<option name="ALLOW_USER_CONFIGURATION" value="false" />
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res;file://$MODULE_DIR$/build/generated/res/resValues/debug" />
<option name="TEST_RES_FOLDERS_RELATIVE_PATH" value="" />
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
<option name="PROJECT_TYPE" value="1" />
</configuration>
Expand Down Expand Up @@ -97,7 +99,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/library_assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/library_java_res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/library_manifest" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint_jar" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_jni_libs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_shaders" />
Expand All @@ -107,6 +108,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shader_assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/tmp" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
Expand Down

0 comments on commit 32a6ef8

Please sign in to comment.