Skip to content

Commit 7b50e07

Browse files
committed
chore: refactor
1 parent ff9fbc3 commit 7b50e07

File tree

3 files changed

+46
-93
lines changed

3 files changed

+46
-93
lines changed

src/core-tabs/tab-navigation/index-common.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { cssProperty } from '@nativescript-community/ui-material-core';
22
// Types
3-
import { Color, CoreTypes, Property, booleanConverter } from '@nativescript/core';
3+
import { Color, CoreTypes, Frame, Property, booleanConverter } from '@nativescript/core';
44
import { TabNavigationBase as TabNavigationBaseBase } from '../tab-navigation-base';
55

66
import { TabNavigation as TabsDefinition } from '.';
77
import { TabStripItem } from '../tab-strip-item';
8+
import { TabContentItem } from '../tab-content-item';
89

910
export namespace knownCollections {
1011
export const items = 'items';
@@ -41,6 +42,42 @@ export class TabNavigationBase extends TabNavigationBaseBase implements TabsDefi
4142
public getTabBarItemTextTransform(tabStripItem: TabStripItem): CoreTypes.TextTransformType {
4243
return this.getItemLabelTextTransform(tabStripItem);
4344
}
45+
46+
public _loadUnloadTabItems(newIndex: number) {
47+
const items = this.items;
48+
if (!items) {
49+
return;
50+
}
51+
52+
const offsideItems = this.offscreenTabLimit;
53+
54+
const toLoad: TabContentItem[] = [];
55+
const toUnload: TabContentItem[] = [];
56+
for (let index = 0; index < items.length; index++) {
57+
const item = items[index];
58+
if (item) {
59+
if (Math.abs(index - newIndex) <= offsideItems) {
60+
toLoad.push(item);
61+
} else {
62+
toUnload.push(item);
63+
}
64+
}
65+
}
66+
67+
if (this.unloadOnTabChange) {
68+
toUnload.forEach((item) => item.unloadView(item.content));
69+
}
70+
71+
const newItem = items[newIndex];
72+
const selectedView = newItem && newItem.content;
73+
if (selectedView instanceof Frame) {
74+
selectedView._pushInFrameStackRecursive();
75+
}
76+
77+
if (this.isLoaded) {
78+
toLoad.forEach((item) => item.loadView(item.content));
79+
}
80+
}
4481
}
4582

4683
export const swipeEnabledProperty = new Property<TabNavigationBase, boolean>({

src/core-tabs/tab-navigation/index.android.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -385,49 +385,6 @@ export abstract class TabNavigation<T extends android.view.ViewGroup = any> exte
385385
super.disposeNativeView();
386386
}
387387

388-
public _loadUnloadTabItems(newIndex: number) {
389-
const items = this.items;
390-
if (!items) {
391-
return;
392-
}
393-
394-
const lastIndex = items.length - 1;
395-
const offsideItems = this.offscreenTabLimit;
396-
397-
const toUnload = [];
398-
const toLoad = [];
399-
400-
iterateIndexRange(newIndex, offsideItems, lastIndex, (i) => toLoad.push(i));
401-
402-
if (this.unloadOnTabChange) {
403-
items.forEach((item, i) => {
404-
const indexOfI = toLoad.indexOf(i);
405-
if (indexOfI < 0) {
406-
toUnload.push(i);
407-
}
408-
toUnload.forEach((index) => {
409-
const item = items[index];
410-
if (items[index]) {
411-
item.unloadView(item.content);
412-
}
413-
});
414-
});
415-
}
416-
417-
const newItem = items[newIndex];
418-
const selectedView = newItem && newItem.content;
419-
if (selectedView instanceof Frame) {
420-
selectedView._pushInFrameStackRecursive();
421-
}
422-
423-
toLoad.forEach((index) => {
424-
const item = items[index];
425-
if (this.isLoaded && items[index]) {
426-
item.loadView(item.content);
427-
}
428-
});
429-
}
430-
431388
public onLoaded(): void {
432389
super.onLoaded();
433390

src/core-tabs/tab-navigation/index.ios.ts

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,15 @@ export abstract class TabNavigation<
385385

386386
const tabStripItems = this.tabStrip && this.tabStrip.items;
387387
if (tabStripItems) {
388-
if (tabStripItems[newIndex]) {
389-
tabStripItems[newIndex]._emit(TabStripItem.selectEvent);
390-
this.updateItemColors(tabStripItems[newIndex]);
388+
const newItem = tabStripItems[newIndex];
389+
if (newItem) {
390+
newItem._emit(TabStripItem.selectEvent);
391+
this.updateItemColors(newItem);
391392
}
392-
393-
if (tabStripItems[oldIndex]) {
394-
tabStripItems[oldIndex]._emit(TabStripItem.unselectEvent);
395-
this.updateItemColors(tabStripItems[oldIndex]);
393+
const oldItem = tabStripItems[oldIndex];
394+
if (oldItem) {
395+
oldItem._emit(TabStripItem.unselectEvent);
396+
this.updateItemColors(oldItem);
396397
}
397398
}
398399

@@ -401,48 +402,6 @@ export abstract class TabNavigation<
401402
super.onSelectedIndexChanged(oldIndex, newIndex);
402403
}
403404

404-
public _loadUnloadTabItems(newIndex: number) {
405-
const items = this.items;
406-
if (!items) {
407-
return;
408-
}
409-
410-
const lastIndex = items.length - 1;
411-
const offsideItems = this.offscreenTabLimit;
412-
413-
const toUnload = [];
414-
const toLoad = [];
415-
416-
iterateIndexRange(newIndex, offsideItems, lastIndex, (i) => toLoad.push(i));
417-
if (this.unloadOnTabChange) {
418-
items.forEach((item, i) => {
419-
const indexOfI = toLoad.indexOf(i);
420-
if (indexOfI < 0) {
421-
toUnload.push(i);
422-
}
423-
});
424-
425-
toUnload.forEach((index) => {
426-
const item = items[index];
427-
if (items[index]) {
428-
item.unloadView(item.content);
429-
}
430-
});
431-
}
432-
const newItem = items[newIndex];
433-
const selectedView = newItem && newItem.content;
434-
if (selectedView instanceof Frame) {
435-
selectedView._pushInFrameStackRecursive();
436-
}
437-
438-
toLoad.forEach((index) => {
439-
const item = items[index];
440-
if (this.isLoaded && items[index]) {
441-
item.loadView(item.content);
442-
}
443-
});
444-
}
445-
446405
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
447406
const width = Utils.layout.getMeasureSpecSize(widthMeasureSpec);
448407
const widthMode = Utils.layout.getMeasureSpecMode(widthMeasureSpec);

0 commit comments

Comments
 (0)