-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain-view-model.ts
97 lines (83 loc) · 3.83 KB
/
main-view-model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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";
private static APPSYNC_ANDROID_STAGING_KEY = "ERb1vITM0ptuiLuEYKIt9v4bFhoQ4ksvOXqog";
private static APPSYNC_ANDROID_PRODUCTION_KEY = "SOPWgPl7PTe0qy6xxvW4VAjrl1Z14ksvOXqog";
public message: string;
constructor() {
super();
this.appSync = new AppSync();
// Check for updates when the app is loaded or resumed
application.on(application.resumeEvent, () => {
this.syncWithAppSyncServer();
});
this.set("message", "HMR enabled: " + (typeof (<any>global).hmrRefresh === "function"));
}
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({
enabledWhenUsingHmr: false,
deploymentKey: isIOS ? HelloWorldModel.APPSYNC_IOS_STAGING_KEY : HelloWorldModel.APPSYNC_ANDROID_STAGING_KEY,
installMode: InstallMode.ON_NEXT_RESTART, // default InstallMode.ON_NEXT_RESTART
mandatoryInstallMode: isIOS ? InstallMode.ON_NEXT_RESUME : InstallMode.IMMEDIATE, // default InstallMode.ON_NEXT_RESUME
updateDialog: { // only used for InstallMode.IMMEDIATE
optionalUpdateMessage: "Optional update msg",
updateTitle: "Please restart the app",
mandatoryUpdateMessage: "Mandatory update msg",
optionalIgnoreButtonLabel: "Later",
mandatoryContinueButtonLabel: isIOS ? "Exit now" : "Restart now",
appendReleaseDescription: true // appends the description you (optionally) provided when releasing a new version to AppSync
}
}, (syncStatus: SyncStatus, updateLabel?: string): void => {
if (syncStatus === SyncStatus.UP_TO_DATE) {
this.set("message", `AppSync: up to date: ${updateLabel}`);
} else if (syncStatus === SyncStatus.UPDATE_INSTALLED) {
this.set("message", `AppSync: update installed: ${updateLabel}`);
} else {
this.set("message", `AppSync: status changed to: ${syncStatus}`);
}
});
}
}