Skip to content

Commit f042417

Browse files
authored
refactor(android-tabview): tab adapter data changed event is called too many times (NativeScript#5364)
1 parent 925c606 commit f042417

File tree

1 file changed

+55
-27
lines changed

1 file changed

+55
-27
lines changed

tns-core-modules/ui/tab-view/tab-view.android.ts

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,6 @@ export class TabView extends TabViewBase {
414414
this._viewPager = viewPager;
415415
this._pagerAdapter = (<any>viewPager).adapter;
416416
(<any>this._pagerAdapter).owner = this;
417-
418-
const items = this.items;
419-
if (items) {
420-
this.setAdapterItems(items)
421-
}
422417
}
423418

424419
public onLoaded(): void {
@@ -434,9 +429,7 @@ export class TabView extends TabViewBase {
434429
}
435430

436431
public disposeNativeView() {
437-
(<any>this._pagerAdapter).items = null;
438432
this._tabLayout.setItems(null, null);
439-
this._pagerAdapter.notifyDataSetChanged();
440433
(<any>this._pagerAdapter).owner = null;
441434
this._pagerAdapter = null;
442435

@@ -454,32 +447,67 @@ export class TabView extends TabViewBase {
454447
return false;
455448
}
456449

457-
private setAdapterItems(items: Array<TabViewItemDefinition>) {
458-
(<any>this._pagerAdapter).items = items;
450+
private shouldUpdateAdapter(items: Array<TabViewItemDefinition>) {
451+
const currentPagerAdapterItems = (<any>this._pagerAdapter).items;
459452

460-
const length = items ? items.length : 0;
461-
if (length === 0) {
462-
this._tabLayout.setItems(null, null);
463-
this._pagerAdapter.notifyDataSetChanged();
464-
return;
453+
// if both values are null, should not update
454+
if (!items && !currentPagerAdapterItems) {
455+
return false;
465456
}
466457

467-
const tabItems = new Array<org.nativescript.widgets.TabItemSpec>();
468-
items.forEach((item: TabViewItem, i, arr) => {
469-
const tabItemSpec = createTabItemSpec(item);
470-
item.index = i;
471-
item.tabItemSpec = tabItemSpec;
472-
tabItems.push(tabItemSpec);
473-
});
458+
// if one value is null, should update
459+
if (!items || !currentPagerAdapterItems) {
460+
return true;
461+
}
474462

475-
const tabLayout = this._tabLayout;
476-
tabLayout.setItems(tabItems, this._viewPager);
477-
items.forEach((item, i, arr) => {
478-
const tv = tabLayout.getTextViewForItemAt(i);
479-
item.setNativeView(tv);
463+
// if both are Arrays but length doesn't match, should update
464+
if (items.length !== currentPagerAdapterItems.length) {
465+
return true;
466+
}
467+
468+
const matchingItems = currentPagerAdapterItems.filter((currentItem) => {
469+
return !!items.filter((item) => {
470+
return item._domId === currentItem._domId
471+
})[0];
480472
});
481473

482-
this._pagerAdapter.notifyDataSetChanged();
474+
// if both are Arrays and length matches, but not all items are the same, should update
475+
if (matchingItems.length !== items.length) {
476+
return true;
477+
}
478+
479+
// if both are Arrays and length matches and all items are the same, should not update
480+
return false;
481+
}
482+
483+
private setAdapterItems(items: Array<TabViewItemDefinition>) {
484+
if (this.shouldUpdateAdapter(items)) {
485+
(<any>this._pagerAdapter).items = items;
486+
487+
const length = items ? items.length : 0;
488+
if (length === 0) {
489+
this._tabLayout.setItems(null, null);
490+
this._pagerAdapter.notifyDataSetChanged();
491+
return;
492+
}
493+
494+
const tabItems = new Array<org.nativescript.widgets.TabItemSpec>();
495+
items.forEach((item: TabViewItem, i, arr) => {
496+
const tabItemSpec = createTabItemSpec(item);
497+
item.index = i;
498+
item.tabItemSpec = tabItemSpec;
499+
tabItems.push(tabItemSpec);
500+
});
501+
502+
const tabLayout = this._tabLayout;
503+
tabLayout.setItems(tabItems, this._viewPager);
504+
items.forEach((item, i, arr) => {
505+
const tv = tabLayout.getTextViewForItemAt(i);
506+
item.setNativeView(tv);
507+
});
508+
509+
this._pagerAdapter.notifyDataSetChanged();
510+
}
483511
}
484512

485513
public updateAndroidItemAt(index: number, spec: org.nativescript.widgets.TabItemSpec) {

0 commit comments

Comments
 (0)