-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathhome-view-model.ts
127 lines (120 loc) · 4.54 KB
/
home-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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { Dialogs, Observable, Utils } from "@nativescript/core";
import { Screen } from "@nativescript/core/platform";
import { InAppBrowser } from "nativescript-inappbrowser";
import { getDeepLink, sleep } from "./utilities";
export class HelloWorldModel extends Observable {
private _url: string;
constructor() {
super();
// Initialize default values.
this._url = "https://nativescript.org";
if (InAppBrowser.warmup()) {
console.log("Warmup successful!");
InAppBrowser.mayLaunchUrl(this._url, [
"https://twitter.com/NativeScript",
"https://github.com/NativeScript/NativeScript",
"https://openjsf.org"
]);
} else {
console.log("Warmup failed :(");
}
}
get url(): string {
return this._url;
}
set url(value: string) {
if (this._url !== value) {
this._url = value;
this.notifyPropertyChange("url", value);
}
}
async openLink() {
try {
const { url } = this;
if (await InAppBrowser.isAvailable()) {
const { widthDIPs, heightDIPs } = Screen.mainScreen;
const result = await InAppBrowser.open(url, {
// iOS Properties
dismissButtonStyle: "cancel",
preferredBarTintColor: "#453AA4",
preferredControlTintColor: "white",
readerMode: false,
animated: true,
modalPresentationStyle: "formSheet",
modalTransitionStyle: "coverVertical",
modalEnabled: true,
enableBarCollapsing: true,
formSheetPreferredContentSize: {
width: widthDIPs - widthDIPs / 6,
height: heightDIPs - heightDIPs / 6,
},
// Android Properties
showTitle: true,
toolbarColor: "#6200EE",
secondaryToolbarColor: "black",
navigationBarColor: "black",
navigationBarDividerColor: "white",
enableUrlBarHiding: true,
enableDefaultShare: true,
forceCloseOnRedirection: true,
// Specify full animation resource identifier(package:anim/name)
// or only resource name(in case of animation bundled with app).
animations: {
startEnter: "slide_in_right",
startExit: "slide_out_left",
endEnter: "slide_in_left",
endExit: "slide_out_right",
},
headers: {
"my-custom-header": "my custom header value",
},
hasBackButton: true,
browserPackage: "com.android.chrome",
showInRecents: true,
includeReferrer: true,
});
await sleep(800);
Dialogs.alert({
title: "Response",
message: JSON.stringify(result),
okButtonText: "Ok",
});
} else {
Utils.openUrl(url);
}
} catch (error) {
Dialogs.alert({
title: "Error",
message: error.message,
okButtonText: "Ok",
});
}
}
async tryDeepLinking() {
const loginUrl = `https://proyecto26.github.io/react-native-inappbrowser`;
const redirectUrl = getDeepLink("home");
const url = `${loginUrl}?redirect_url=${encodeURIComponent(
redirectUrl
)}`;
try {
if (await InAppBrowser.isAvailable()) {
const result = await InAppBrowser.openAuth(url, redirectUrl, {
// iOS Properties
ephemeralWebSession: true,
// Android Properties
showTitle: false,
enableUrlBarHiding: true,
enableDefaultShare: true,
});
await sleep(800);
Dialogs.alert({
title: "Response",
message: JSON.stringify(result),
okButtonText: "Ok",
});
}
} catch {
Dialogs.alert("Something’s wrong with the app :(");
}
}
}