refactor(android-tabview): tab adapter data changed event is called too many times (#5364)

This commit is contained in:
Martin Yankov
2018-02-05 13:52:32 +02:00
committed by GitHub
parent 925c60677f
commit f042417f10

View File

@@ -414,11 +414,6 @@ export class TabView extends TabViewBase {
this._viewPager = viewPager; this._viewPager = viewPager;
this._pagerAdapter = (<any>viewPager).adapter; this._pagerAdapter = (<any>viewPager).adapter;
(<any>this._pagerAdapter).owner = this; (<any>this._pagerAdapter).owner = this;
const items = this.items;
if (items) {
this.setAdapterItems(items)
}
} }
public onLoaded(): void { public onLoaded(): void {
@@ -434,9 +429,7 @@ export class TabView extends TabViewBase {
} }
public disposeNativeView() { public disposeNativeView() {
(<any>this._pagerAdapter).items = null;
this._tabLayout.setItems(null, null); this._tabLayout.setItems(null, null);
this._pagerAdapter.notifyDataSetChanged();
(<any>this._pagerAdapter).owner = null; (<any>this._pagerAdapter).owner = null;
this._pagerAdapter = null; this._pagerAdapter = null;
@@ -454,7 +447,41 @@ export class TabView extends TabViewBase {
return false; return false;
} }
private shouldUpdateAdapter(items: Array<TabViewItemDefinition>) {
const currentPagerAdapterItems = (<any>this._pagerAdapter).items;
// if both values are null, should not update
if (!items && !currentPagerAdapterItems) {
return false;
}
// if one value is null, should update
if (!items || !currentPagerAdapterItems) {
return true;
}
// if both are Arrays but length doesn't match, should update
if (items.length !== currentPagerAdapterItems.length) {
return true;
}
const matchingItems = currentPagerAdapterItems.filter((currentItem) => {
return !!items.filter((item) => {
return item._domId === currentItem._domId
})[0];
});
// if both are Arrays and length matches, but not all items are the same, should update
if (matchingItems.length !== items.length) {
return true;
}
// if both are Arrays and length matches and all items are the same, should not update
return false;
}
private setAdapterItems(items: Array<TabViewItemDefinition>) { private setAdapterItems(items: Array<TabViewItemDefinition>) {
if (this.shouldUpdateAdapter(items)) {
(<any>this._pagerAdapter).items = items; (<any>this._pagerAdapter).items = items;
const length = items ? items.length : 0; const length = items ? items.length : 0;
@@ -481,6 +508,7 @@ export class TabView extends TabViewBase {
this._pagerAdapter.notifyDataSetChanged(); this._pagerAdapter.notifyDataSetChanged();
} }
}
public updateAndroidItemAt(index: number, spec: org.nativescript.widgets.TabItemSpec) { public updateAndroidItemAt(index: number, spec: org.nativescript.widgets.TabItemSpec) {
this._tabLayout.updateItemAt(index, spec); this._tabLayout.updateItemAt(index, spec);