Skip to content

Commit 0b6b011

Browse files
DickSmithPeterStaev
authored andcommitted
Snapshot-friendly (#162)
1 parent fcc0f99 commit 0b6b011

6 files changed

Lines changed: 1796 additions & 37 deletions

File tree

drop-down.android.ts

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,31 @@ export class DropDown extends DropDownBase {
5757
private _androidViewId: number;
5858

5959
public createNativeView() {
60+
initializeTNSSpinner();
6061
const spinner = new TNSSpinner(new WeakRef(this));
6162

6263
if (!this._androidViewId) {
6364
this._androidViewId = android.view.View.generateViewId();
6465
}
6566
spinner.setId(this._androidViewId);
6667

68+
initializeDropDownAdapter();
6769
const adapter = new DropDownAdapter(new WeakRef(this));
6870
spinner.setAdapter(adapter);
69-
(spinner as any).adapter = adapter;
71+
spinner.adapter = adapter;
7072

73+
initializeDropDownItemSelectedListener();
7174
const itemSelectedListener = new DropDownItemSelectedListener(new WeakRef(this));
7275
spinner.setOnItemSelectedListener(itemSelectedListener);
73-
(spinner as any).itemSelectedListener = itemSelectedListener;
76+
spinner.itemSelectedListener = itemSelectedListener;
7477

7578
return spinner;
7679
}
7780

7881
public initNativeView() {
7982
super.initNativeView();
8083

81-
const nativeView = this.nativeView as any;
84+
const nativeView = this.nativeView;
8285
nativeView.adapter.owner = new WeakRef(this);
8386
nativeView.itemSelectedListener.owner = new WeakRef(this);
8487

@@ -90,7 +93,7 @@ export class DropDown extends DropDownBase {
9093
}
9194

9295
public disposeNativeView() {
93-
const nativeView = this.nativeView as any;
96+
const nativeView = this.nativeView;
9497
nativeView.adapter.owner = null;
9598
nativeView.itemSelectedListener.owner = null;
9699

@@ -107,11 +110,11 @@ export class DropDown extends DropDownBase {
107110
public open() {
108111
if (this.isEnabled) {
109112
this.nativeView.performClick();
110-
}
113+
}
111114
}
112115

113116
public close() {
114-
this.nativeView.onDetachedFromWindow();
117+
this.nativeView.onDetachedFromWindowX();
115118
}
116119

117120
public [selectedIndexProperty.getDefault](): number {
@@ -233,7 +236,26 @@ export class DropDown extends DropDownBase {
233236
}
234237
}
235238

236-
class TNSSpinner extends android.widget.Spinner {
239+
/* A snapshot-friendly, lazy-loaded class for TNSSpinner BEGIN */
240+
interface TNSSpinner extends android.widget.Spinner {
241+
adapter;
242+
itemSelectedListener;
243+
244+
/*tslint:disable-next-line no-misused-new*/
245+
new(owner: WeakRef<DropDown>): TNSSpinner;
246+
247+
/** onDetachedFromWindow is protected so public version renamed */
248+
onDetachedFromWindowX();
249+
}
250+
251+
let TNSSpinner: TNSSpinner;
252+
253+
function initializeTNSSpinner() {
254+
if (TNSSpinner) {
255+
return;
256+
}
257+
258+
class TNSSpinnerImpl extends android.widget.Spinner {
237259
private _isOpenedIn = false;
238260

239261
constructor(private owner: WeakRef<DropDown>) {
@@ -266,12 +288,29 @@ class TNSSpinner extends android.widget.Spinner {
266288
}
267289
}
268290

269-
public onDetachedFromWindow() {
291+
public onDetachedFromWindowX(): void {
270292
super.onDetachedFromWindow();
271293
}
272294
}
273295

274-
class DropDownAdapter extends android.widget.BaseAdapter implements android.widget.ISpinnerAdapter {
296+
TNSSpinner = TNSSpinnerImpl as any;
297+
}
298+
/* TNSSpinner END */
299+
300+
/* A snapshot-friendly, lazy-loaded class for DropDownAdpater BEGIN */
301+
interface DropDownAdapter extends android.widget.BaseAdapter, android.widget.ISpinnerAdapter {
302+
/*tslint:disable-next-line no-misused-new*/
303+
new(owner: WeakRef<DropDown>): DropDownAdapter;
304+
}
305+
306+
let DropDownAdapter: DropDownAdapter;
307+
308+
function initializeDropDownAdapter() {
309+
if (DropDownAdapter) {
310+
return;
311+
}
312+
313+
class DropDownAdapterImpl extends android.widget.BaseAdapter implements android.widget.ISpinnerAdapter {
275314
constructor(private owner: WeakRef<DropDown>) {
276315
super();
277316

@@ -372,8 +411,25 @@ class DropDownAdapter extends android.widget.BaseAdapter implements android.widg
372411
}
373412
}
374413

414+
DropDownAdapter = DropDownAdapterImpl as any;
415+
}
416+
/* DropDownAdpater END */
417+
418+
/* A snapshot-friendly, lazy-loaded class for DropDownItemSelectedListener BEGIN */
419+
interface DropDownItemSelectedListener extends java.lang.Object, android.widget.AdapterView.OnItemSelectedListener {
420+
/*tslint:disable-next-line no-misused-new*/
421+
new(owner: WeakRef<DropDown>): DropDownItemSelectedListener;
422+
}
423+
424+
let DropDownItemSelectedListener: DropDownItemSelectedListener;
425+
426+
function initializeDropDownItemSelectedListener() {
427+
if (DropDownItemSelectedListener) {
428+
return;
429+
}
430+
375431
@Interfaces([android.widget.AdapterView.OnItemSelectedListener])
376-
class DropDownItemSelectedListener extends java.lang.Object implements android.widget.AdapterView.OnItemSelectedListener {
432+
class DropDownItemSelectedListenerImpl extends java.lang.Object implements android.widget.AdapterView.OnItemSelectedListener {
377433
constructor(private owner: WeakRef<DropDown>) {
378434
super();
379435

@@ -400,4 +456,8 @@ class DropDownItemSelectedListener extends java.lang.Object implements android.w
400456
public onNothingSelected() {
401457
/* Currently Not Needed */
402458
}
403-
}
459+
}
460+
461+
DropDownItemSelectedListener = DropDownItemSelectedListenerImpl as any;
462+
}
463+
/* DropDownItemSelectedListener END */

drop-down.d.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ import { CoercibleProperty, Property, View } from "ui/core/view";
1818
import { EventData } from "data/observable";
1919
import { ItemsSource } from "ui/list-picker";
2020

21-
export interface SelectedIndexChangedEventData extends EventData {
21+
export declare interface SelectedIndexChangedEventData extends EventData {
2222
oldIndex: number;
2323
newIndex: number;
2424
}
2525

26-
export interface ValueItem<T> {
26+
export declare interface ValueItem<T> {
2727
value: T;
2828
display: string;
2929
}
3030

31-
export class DropDown extends View {
32-
public static openedEvent: string;
33-
public static closedEvent: string;
34-
public static selectedIndexChangedEvent: string;
31+
export declare class DropDown extends View {
32+
public static openedEvent: "opened";
33+
public static closedEvent: "closed";
34+
public static selectedIndexChangedEvent: "selectedIndexChanged";
3535

3636
public items: any[] | ItemsSource;
3737
public selectedIndex: number;
@@ -41,21 +41,21 @@ export class DropDown extends View {
4141
public ios: any; /* UILabel */
4242
public android: any; /*android.widget.Spinner */
4343

44-
public on(eventNames: string, callback: (data: EventData) => void, thisArg?: any);
45-
public on(event: "opened", callback: (args: EventData) => void, thisArg?: any);
46-
public on(event: "closed", callback: (args: EventData) => void, thisArg?: any);
44+
public on(eventNames: string, callback: (data: EventData) => void, thisArg?: any);
45+
public on(event: "opened", callback: (args: EventData) => void, thisArg?: any);
46+
public on(event: "closed", callback: (args: EventData) => void, thisArg?: any);
4747
public on(event: "selectedIndexChanged", callback: (args: SelectedIndexChangedEventData) => void, thisArg?: any);
4848

4949
public open();
5050
public close();
5151
}
5252

53-
export class ValueList<T> extends ObservableArray<ValueItem<T>> implements ItemsSource {
53+
export declare class ValueList<T> extends ObservableArray<ValueItem<T>> implements ItemsSource {
5454
public getDisplay(index: number): string;
5555
public getValue(index: number): T;
56-
public getIndex(value: T): number;
56+
public getIndex(value: T): number;
5757
}
5858

59-
export const selectedIndexProperty: CoercibleProperty<DropDown, number>;
60-
export const itemsProperty: Property<DropDown, any[] | ItemsSource>;
61-
export const hintProperty: Property<DropDown, string>;
59+
export declare const selectedIndexProperty: CoercibleProperty<DropDown, number>;
60+
export declare const itemsProperty: Property<DropDown, any[] | ItemsSource>;
61+
export declare const hintProperty: Property<DropDown, string>;

0 commit comments

Comments
 (0)