-
Notifications
You must be signed in to change notification settings - Fork 19
/
main-view-model.ts
193 lines (167 loc) · 5.8 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { Observable } from "tns-core-modules/data/observable";
import { action } from "tns-core-modules/ui/dialogs";
import { topmost } from "tns-core-modules/ui/frame";
import { TextView } from "tns-core-modules/ui/text-view";
const emailValidator = require("email-validator");
declare const IQKeyboardManager: any;
export class HelloWorldModel extends Observable {
email: string;
sliderValue = 0;
showRob = false;
showEddy = false;
showTrumpsTies = false;
// icons, see https://linearicons.com/free (click a pic for the hex code used below)
iconCamera = String.fromCharCode(0xe826);
iconTrash = String.fromCharCode(0xe811);
iconPaperclip = String.fromCharCode(0xe819);
iconPriceTag = String.fromCharCode(0xe82f);
iconMicrophone = String.fromCharCode(0xe85e);
iconDown = String.fromCharCode(0xe874);
iconValid = String.fromCharCode(0xe87f);
iconInvalid = String.fromCharCode(0xe880);
iconExpand = String.fromCharCode(0xe88c);
iconContract = String.fromCharCode(0xe88d);
constructor() {
super();
// this suppresses IQKeyboardManager's toolbar (the one with < > and OK buttons)
IQKeyboardManager.sharedManager().enableAutoToolbar = false;
// laughable code, I know, but it's just a quick demo
setTimeout(() => {
const page = topmost().currentPage;
const textView = <TextView>page.getViewById("tv3");
if (!textView) {
return;
}
textView.on("textChange", (args: any) => {
const text = textView.text;
const words = text.split(" ");
if (words.length === 0) {
return;
}
const lastWord = words[words.length - 1].toLowerCase();
this.set("showRob", false);
this.set("showEddy", false);
this.set("showTrumpsTies", false);
if (lastWord.indexOf("@") > -1) {
if ("@" === lastWord) {
this.set("showRob", true);
this.set("showEddy", true);
this.set("showTrumpsTies", true);
} else if ("@roblauer".indexOf(lastWord) > -1) {
this.set("showRob", true);
this.set("showEddy", false);
this.set("showTrumpsTies", false);
} else if ("@eddyverbruggen".indexOf(lastWord) > -1) {
this.set("showRob", false);
this.set("showEddy", true);
this.set("showTrumpsTies", false);
} else if ("@trumpsties".indexOf(lastWord) > -1) {
this.set("showRob", false);
this.set("showEddy", false);
this.set("showTrumpsTies", true);
}
}
});
}, 500);
}
setAmountInTextField(args: any): void {
const textView = <TextView>topmost().currentPage.getViewById("tf1");
textView.text = args.object.value;
this.positionCursorAtEnd(textView);
textView.dismissSoftInput();
}
appendToTextView2(args: any): void {
const textView = <TextView>topmost().currentPage.getViewById("tv2");
textView.text = " " args.object.text " ";
this.positionCursorAtEnd(textView);
}
emailOK(): boolean {
return emailValidator.validate(this.email);
}
goToTv2(args: any): void {
const textView = <TextView>topmost().currentPage.getViewById("tv2");
textView.focus();
this.positionCursorAtEnd(textView);
}
appendToTextView3(args: any): void {
const textView = <TextView>topmost().currentPage.getViewById("tv3");
let newText = textView.text;
const words = textView.text.split(" ");
if (words.length > 0) {
const lastWord = words[words.length - 1].toLowerCase();
if (args.object.text.startsWith(lastWord)) {
words.pop();
newText = words.join(" ");
}
}
textView.text = (newText " " args.object.text).trim() " ";
this.positionCursorAtEnd(textView);
}
showUserForTextView3(args: any): boolean {
const textView = <TextView>topmost().currentPage.getViewById("tv3");
console.log(args.object.text);
return textView.text.endsWith("@");
}
getSliderValue(): any {
return this.sliderValue === undefined ? undefined : Math.round(this.sliderValue);
}
onTapTv1Camera(): void {
action({
title: "Add a picture from..",
cancelable: true,
cancelButtonText: "Cancel",
actions: ["the camera", "the photo album"]
});
}
onTapTv1Trash(): void {
const page = topmost().currentPage;
const textView = <TextView>page.getViewById("tv1");
textView.text = "";
}
onTapTv1Expand(): void {
const page = topmost().currentPage;
const textView = <TextView>page.getViewById("tv1");
if ((textView.height as number) < 130) {
(textView.height as number) = 10;
} else {
console.log("Text already expanded to the maximum we allow ;)");
}
}
onTapTv1Contract(): void {
const page = topmost().currentPage;
const textView = <TextView>page.getViewById("tv1");
if ((textView.height as number) > 30) {
(textView.height as number) -= 10;
} else {
console.log("Text already contracted to the minimum we allow ;)");
}
}
onTapTv2(): void {
console.log(">> tapped tv2");
}
hideKeyboardTf1(): void {
const page = topmost().currentPage;
const textView = <TextView>page.getViewById("tf1");
textView.dismissSoftInput();
}
hideKeyboardTf3(): void {
const page = topmost().currentPage;
const textView = <TextView>page.getViewById("tf3");
textView.dismissSoftInput();
}
hideKeyboardTv1(): void {
const page = topmost().currentPage;
const textView = <TextView>page.getViewById("tv1");
textView.dismissSoftInput();
}
hideKeyboardTv2(): void {
const page = topmost().currentPage;
const textView = <TextView>page.getViewById("tv2");
textView.dismissSoftInput();
}
private positionCursorAtEnd(textView: TextView): void {
if (textView.android) {
textView.android.setSelection(textView.text.length);
}
}
}