forked from PeterStaev/NativeScript-Drop-Down
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drop-down.android.ts
547 lines (438 loc) · 18.2 KB
/
drop-down.android.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*! *****************************************************************************
Copyright (c) 2018 Tangra Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************** */
import { Color } from "color";
import { View } from "ui/core/view";
import { placeholderColorProperty } from "ui/editable-text-base/editable-text-base";
import { Label } from "ui/label";
import { StackLayout } from "ui/layouts/stack-layout";
import { ItemsSource } from "ui/list-picker";
import { Font } from "ui/styling/font";
import {
TextAlignment,
TextDecoration,
fontInternalProperty,
fontSizeProperty,
textAlignmentProperty,
textDecorationProperty
} from "ui/text-base";
import * as types from "utils/types";
import { SelectedIndexChangedEventData } from ".";
import {
DropDownBase,
backgroundColorProperty,
colorProperty,
hintProperty,
itemsPaddingProperty,
itemsProperty,
itemsTextAlignmentProperty,
selectedIndexProperty
} from "./drop-down-common";
export * from "./drop-down-common";
const LABELVIEWID = "spinner-label";
const enum RealizedViewType {
ItemView,
DropDownView
}
export class DropDown extends DropDownBase {
public nativeView: TNSSpinner;
public _realizedItems = [
new Map<android.view.View, View>(),
new Map<android.view.View, View>()
];
private _androidViewId: number;
public createNativeView() {
initializeTNSSpinner();
const spinner = new TNSSpinner(new WeakRef(this));
if (!this._androidViewId) {
this._androidViewId = android.view.View.generateViewId();
}
spinner.setId(this._androidViewId);
initializeDropDownAdapter();
const adapter = new DropDownAdapter(new WeakRef(this));
spinner.setAdapter(adapter);
spinner.adapter = adapter;
initializeDropDownItemSelectedListener();
const itemSelectedListener = new DropDownItemSelectedListener(new WeakRef(this));
spinner.setOnItemSelectedListener(itemSelectedListener);
spinner.itemSelectedListener = itemSelectedListener;
return spinner;
}
public initNativeView() {
super.initNativeView();
const nativeView = this.nativeView;
nativeView.adapter.owner = new WeakRef(this);
nativeView.itemSelectedListener.owner = new WeakRef(this);
// When used in templates the selectedIndex changed event is fired before the native widget is init.
// So here we must set the inital value (if any)
if (!types.isNullOrUndefined(this.selectedIndex)) {
this.android.setSelection(this.selectedIndex + 1); // +1 for the hint first element
}
nativeView.itemsTextAlignment = itemsTextAlignmentProperty.defaultValue;
nativeView.itemsPadding = itemsPaddingProperty.defaultValue;
}
public disposeNativeView() {
const nativeView = this.nativeView;
nativeView.adapter.owner = null;
nativeView.itemSelectedListener.owner = null;
this._clearCache(RealizedViewType.DropDownView);
this._clearCache(RealizedViewType.ItemView);
super.disposeNativeView();
}
get android(): android.widget.Spinner {
return this.nativeView;
}
public open() {
if (this.isEnabled) {
this.nativeView.performClick();
}
}
public close() {
this.nativeView.onDetachedFromWindowX();
}
public refresh() {
this._updateSelectedIndexOnItemsPropertyChanged(this.items);
if (this.android && this.android.getAdapter()) {
(this.android.getAdapter() as DropDownAdapter).notifyDataSetChanged();
}
// Coerce selected index after we have set items to native view.
selectedIndexProperty.coerce(this);
}
public [backgroundColorProperty.setNative](value: Color | number) {
this._propagateStylePropertyToRealizedViews("backgroundColor", value, true);
}
public [colorProperty.setNative](value: Color | number) {
if (!types.isNullOrUndefined(value)) {
this._propagateStylePropertyToRealizedViews("color", value, false);
}
}
public [fontInternalProperty.setNative](value: Font | android.graphics.Typeface) {
this._propagateStylePropertyToRealizedViews("fontInternal", value, true);
}
public [fontSizeProperty.setNative](value: number | { nativeSize: number }) {
if (!types.isNullOrUndefined(value)) {
this._propagateStylePropertyToRealizedViews("fontSize", value, true);
}
}
public [hintProperty.getDefault](): string {
return "";
}
public [hintProperty.setNative](value: string) {
(this.android.getAdapter() as DropDownAdapter).notifyDataSetChanged();
}
public [itemsPaddingProperty.getDefault](): string {
return "";
}
public [itemsPaddingProperty.setNative](value: string) {
this.nativeView.itemsPadding = value;
}
public [itemsProperty.getDefault](): any[] {
return null;
}
public [itemsProperty.setNative](value: any[] | ItemsSource) {
this._updateSelectedIndexOnItemsPropertyChanged(value);
(this.android.getAdapter() as DropDownAdapter).notifyDataSetChanged();
// Coerce selected index after we have set items to native view.
selectedIndexProperty.coerce(this);
}
public [itemsTextAlignmentProperty.getDefault](): TextAlignment {
return "initial";
}
public [itemsTextAlignmentProperty.setNative](value: TextAlignment) {
this.nativeView.itemsTextAlignment = value;
}
public [textDecorationProperty.getDefault](): TextDecoration {
return "none";
}
public [textDecorationProperty.setNative](value: TextDecoration) {
this._propagateStylePropertyToRealizedViews("textDecoration", value, true);
}
public [textAlignmentProperty.getDefault](): TextAlignment {
return "left";
}
public [textAlignmentProperty.setNative](value: TextAlignment) {
this._propagateStylePropertyToRealizedViews("textAlignment", value, true);
}
public [placeholderColorProperty.setNative](value: Color | number) {
this._propagateStylePropertyToRealizedViews("placeholderColor", value, true);
}
public [selectedIndexProperty.getDefault](): number {
return null;
}
public [selectedIndexProperty.setNative](value: number) {
const actualIndex = (types.isNullOrUndefined(value) ? 0 : value + 1);
this.nativeView.setSelection(actualIndex);
}
public _getRealizedView(convertView: android.view.View, realizedViewType: RealizedViewType): View {
if (!convertView) {
const view = new Label();
const layout = new StackLayout();
layout.style.horizontalAlignment = "stretch";
view.id = LABELVIEWID;
layout.addChild(view);
return layout;
}
return this._realizedItems[realizedViewType].get(convertView);
}
public _clearCache(realizedViewType: RealizedViewType) {
const realizedItems = this._realizedItems[realizedViewType];
realizedItems.forEach((view) => {
if (view.parent) {
view.parent._removeView(view);
}
});
realizedItems.clear();
}
private _propagateStylePropertyToRealizedViews(property: string, value: any, isIncludeHintIn = true) {
const realizedItems = this._realizedItems;
for (const item of realizedItems) {
item.forEach((view) => {
if (isIncludeHintIn || !(view as any).isHintViewIn) {
if (property === "textAlignment" || property === "textDecoration"
|| property === "fontInternal" || property === "fontSize"
|| property === "color"
|| property === "placeholderColor") {
const label = view.getViewById<Label>(LABELVIEWID);
label.style[property] = value;
}
else {
view.style[property] = value;
}
}
});
}
}
private _updateSelectedIndexOnItemsPropertyChanged(newItems: any[] | ItemsSource) {
let newItemsCount = 0;
if (newItems && newItems.length) {
newItemsCount = newItems.length;
}
if (newItemsCount === 0 || this.selectedIndex >= newItemsCount) {
this.selectedIndex = null;
}
}
}
/* A snapshot-friendly, lazy-loaded class for TNSSpinner BEGIN */
interface TNSSpinner extends android.widget.Spinner {
adapter;
itemSelectedListener;
itemsTextAlignment;
itemsPadding;
/*tslint:disable-next-line no-misused-new*/
new (owner: WeakRef<DropDown>): TNSSpinner;
/** onDetachedFromWindow is protected so public version renamed */
onDetachedFromWindowX();
}
let TNSSpinner: TNSSpinner;
function initializeTNSSpinner() {
if (TNSSpinner) {
return;
}
class TNSSpinnerImpl extends android.widget.Spinner {
private _isOpenedIn = false;
private _itemsTextAlignment: TextAlignment;
private _itemsPadding: string;
constructor(private owner: WeakRef<DropDown>) {
super(owner.get()._context);
return global.__native(this);
}
get itemsTextAlignment(): TextAlignment {
return this._itemsTextAlignment;
}
set itemsTextAlignment(value: TextAlignment) {
this._itemsTextAlignment = value;
}
get itemsPadding(): string {
return this._itemsPadding;
}
set itemsPadding(value: string) {
this._itemsPadding = value;
}
public performClick() {
const owner = this.owner.get();
this._isOpenedIn = true;
owner.notify({
eventName: DropDownBase.openedEvent,
object: owner
});
return super.performClick();
}
public onWindowFocusChanged(hasWindowFocus: boolean) {
super.onWindowFocusChanged(hasWindowFocus);
if (this._isOpenedIn && hasWindowFocus) {
const owner = this.owner.get();
owner.notify({
eventName: DropDownBase.closedEvent,
object: owner
});
this._isOpenedIn = false;
}
}
public onDetachedFromWindowX(): void {
super.onDetachedFromWindow();
}
}
TNSSpinner = TNSSpinnerImpl as any;
}
/* TNSSpinner END */
/* A snapshot-friendly, lazy-loaded class for DropDownAdpater BEGIN */
interface DropDownAdapter extends android.widget.BaseAdapter, android.widget.ISpinnerAdapter {
/*tslint:disable-next-line no-misused-new*/
new (owner: WeakRef<DropDown>): DropDownAdapter;
}
let DropDownAdapter: DropDownAdapter;
function initializeDropDownAdapter() {
if (DropDownAdapter) {
return;
}
class DropDownAdapterImpl extends android.widget.BaseAdapter implements android.widget.ISpinnerAdapter {
constructor(private owner: WeakRef<DropDown>) {
super();
return global.__native(this);
}
public isEnabled(i: number) {
return i !== 0;
}
public getCount() {
// In some strange situations owner can become null (see #181)
if (!this.owner) {
return 0;
}
const owner = this.owner.get();
return (owner && owner.items ? owner.items.length : 0) + 1; // +1 for the hint
}
public getItem(i: number) {
// In some strange situations owner can become null (see #181)
if (!this.owner) {
return "";
}
const owner = this.owner.get();
if (i === 0) {
return owner.hint;
}
const realIndex = i - 1;
return owner._getItemAsString(realIndex);
}
public getItemId(i: number) {
return long(i);
}
public hasStableIds(): boolean {
return true;
}
public getView(index: number, convertView: android.view.View, parent: android.view.ViewGroup): android.view.View {
return this._generateView(index, convertView, parent, RealizedViewType.ItemView);
}
public getDropDownView(index: number, convertView: android.view.View, parent: android.view.ViewGroup): android.view.View {
return this._generateView(index, convertView, parent, RealizedViewType.DropDownView);
}
private _generateView(index: number, convertView: android.view.View, parent: android.view.ViewGroup, realizedViewType: RealizedViewType): android.view.View {
// In some strange situations owner can become null (see #181)
if (!this.owner) {
return null;
}
const owner = this.owner.get();
if (!owner) {
return null;
}
const view = owner._getRealizedView(convertView, realizedViewType);
if (view) {
if (!view.parent) {
owner._addView(view);
convertView = view.android;
}
const label = view.getViewById<Label>(LABELVIEWID);
label.text = this.getItem(index);
// Copy root styles to view
if (owner.style.color) {
label.style.color = owner.style.color;
}
if (owner.style.placeholderColor) {
label.style.placeholderColor = owner.style.placeholderColor;
}
label.style.textDecoration = owner.style.textDecoration;
label.style.textAlignment = owner.nativeView.itemsTextAlignment !== itemsTextAlignmentProperty.defaultValue
&& realizedViewType === 1 ? owner.nativeView.itemsTextAlignment : owner.style.textAlignment;
label.style.fontInternal = owner.style.fontInternal;
if (owner.style.fontSize) {
label.style.fontSize = owner.style.fontSize;
}
view.style.backgroundColor = owner.style.backgroundColor;
view.style.padding = owner.nativeView.itemsPadding !== itemsPaddingProperty.defaultValue
&& realizedViewType === 1 ? owner.nativeView.itemsPadding : owner.style.padding;
view.style.height = owner.style.height;
if (realizedViewType === RealizedViewType.DropDownView) {
view.style.opacity = owner.style.opacity;
}
(view as any).isHintViewIn = false;
// Hint View styles
if (index === 0) {
if (label.style.placeholderColor) {
label.style.color = label.style.placeholderColor;
} else {
label.style.color = new Color(255, 148, 150, 148);
}
(view as any).isHintViewIn = true;
// HACK: if there is no hint defined, make the view in the drop down virtually invisible.
if (realizedViewType === RealizedViewType.DropDownView
&& (types.isNullOrUndefined(owner.hint) || owner.hint === "")) {
view.height = 1;
}
// END HACK
}
owner._realizedItems[realizedViewType].set(convertView, view);
}
return convertView;
}
}
DropDownAdapter = DropDownAdapterImpl as any;
}
/* DropDownAdpater END */
/* A snapshot-friendly, lazy-loaded class for DropDownItemSelectedListener BEGIN */
interface DropDownItemSelectedListener extends java.lang.Object, android.widget.AdapterView.OnItemSelectedListener {
/*tslint:disable-next-line no-misused-new*/
new (owner: WeakRef<DropDown>): DropDownItemSelectedListener;
}
let DropDownItemSelectedListener: DropDownItemSelectedListener;
function initializeDropDownItemSelectedListener() {
if (DropDownItemSelectedListener) {
return;
}
@Interfaces([android.widget.AdapterView.OnItemSelectedListener])
class DropDownItemSelectedListenerImpl extends java.lang.Object implements android.widget.AdapterView.OnItemSelectedListener {
constructor(private owner: WeakRef<DropDown>) {
super();
return global.__native(this);
}
public onItemSelected(parent: any, convertView: android.view.View, index: number, id: number) {
const owner = this.owner.get();
const oldIndex = owner.selectedIndex;
const newIndex = (index === 0 ? null : index - 1);
owner.selectedIndex = newIndex;
if (newIndex !== oldIndex) {
owner.notify({
eventName: DropDownBase.selectedIndexChangedEvent,
object: owner,
oldIndex,
newIndex
} as SelectedIndexChangedEventData);
// Seems if the user does not select an item the control reuses the views on the next open.
// So it should be safe to clear the cache once the user selects an item (and not when the dropdown is closed)
owner._clearCache(RealizedViewType.DropDownView);
}
}
public onNothingSelected() {
/* Currently Not Needed */
}
}
DropDownItemSelectedListener = DropDownItemSelectedListenerImpl as any;
}
/* DropDownItemSelectedListener END */