Skip to content

Commit

Permalink
feat: preserveIndexOnItemsChange property
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefuge committed Dec 14, 2023
1 parent 7d4bb6f commit 696c611
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
15 changes: 8 additions & 7 deletions src/ui-pager/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,6 @@ export class Pager extends PagerBase {
this.nativeViewProtected.setUserInputEnabled(!value);
}

[itemsProperty.getDefault](): any {
return null;
}

[itemsProperty.setNative](value: any) {
this.setObservableArrayInstance(value);
}
Expand Down Expand Up @@ -357,11 +353,16 @@ export class Pager extends PagerBase {
this.initStaticPagerAdapter();
}

[selectedIndexProperty.setNative](value: number) {
[selectedIndexProperty.setNative](value: number, animated = true, requestTransform = false) {
const nativeView = this.nativeViewProtected;
if (this.isLoaded && nativeView) {
const index = this.circularMode ? value + 1 : value;
nativeView.setCurrentItem(index, !this.disableAnimation);
nativeView.setCurrentItem(index, animated && !this.disableAnimation);
if (requestTransform) {
setTimeout(() => {
nativeView.requestTransform();
}, 0);
}
}
}

Expand Down Expand Up @@ -397,7 +398,7 @@ export class Pager extends PagerBase {
const ids = Array.from(this.bindedViewHolders).sort((a, b) => a - b);
this.pagerAdapter.notifyItemRangeChanged(ids[0], ids[ids.length - 1] - ids[0] + 1);
}

getViewForItemAtIndex(index: number) {
return this.getChildView(index);
}
Expand Down
33 changes: 19 additions & 14 deletions src/ui-pager/index.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export abstract class PagerBase extends ContainerView implements AddChildFromBui
public circularMode: boolean;
public autoPlayDelay: number;
public autoPlay: boolean;
public preserveIndexOnItemsChange: boolean = false;
// This one works along with existing NS property change event system
public static selectedIndexChangeEvent = 'selectedIndexChange';
public static scrollEvent = 'scroll';
Expand Down Expand Up @@ -143,10 +144,14 @@ export abstract class PagerBase extends ContainerView implements AddChildFromBui
if (value instanceof ObservableArray) {
this.mObservableArrayInstance = value as any;
this.mObservableArrayInstance.on(ObservableArray.changeEvent, this._observableArrayHandler);
// } else {
}
this.refresh();
if (this.preserveIndexOnItemsChange) {
this[selectedIndexProperty.setNative](this.selectedIndex, false /* disableAnimation */, true /* requestTransform(Android) */);
} else {
this.refresh();
selectedIndexProperty.coerce(this);
}
selectedIndexProperty.coerce(this);
}

getChildView(index: number): View {
Expand Down Expand Up @@ -336,22 +341,22 @@ export abstract class PagerBase extends ContainerView implements AddChildFromBui
return converted;
}

abstract _onItemsChanged(oldValue: any, newValue: any): void;
// abstract _onItemsChanged(oldValue: any, newValue: any): void;
}

export class PagerItem extends GridLayout {}

function onItemsChanged(pager: PagerBase, oldValue, newValue) {
if (oldValue instanceof Observable) {
removeWeakEventListener(oldValue, ObservableArray.changeEvent, pager.refresh, pager);
}
// function onItemsChanged(pager: PagerBase, oldValue, newValue) {
// if (oldValue instanceof Observable) {
// removeWeakEventListener(oldValue, ObservableArray.changeEvent, pager.refresh, pager);
// }

if (newValue instanceof Observable && !(newValue instanceof ObservableArray)) {
addWeakEventListener(newValue, ObservableArray.changeEvent, pager.refresh, pager);
}
// if (newValue instanceof Observable && !(newValue instanceof ObservableArray)) {
// addWeakEventListener(newValue, ObservableArray.changeEvent, pager.refresh, pager);
// }

pager.refresh();
}
// // pager.refresh();
// }

function onItemTemplateChanged(pager: PagerBase, oldValue, newValue) {
pager.itemTemplateUpdated(oldValue, newValue);
Expand Down Expand Up @@ -407,8 +412,8 @@ peakingProperty.register(PagerBase);

export const itemsProperty = new Property<PagerBase, any>({
name: 'items',
affectsLayout: true,
valueChanged: onItemsChanged
affectsLayout: true
// valueChanged: onItemsChanged
});
itemsProperty.register(PagerBase);

Expand Down
4 changes: 2 additions & 2 deletions src/ui-pager/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@ export class Pager extends PagerBase {
this.scrollToIndexAnimated(this.selectedIndex, false);
}

[selectedIndexProperty.setNative](value: number) {
[selectedIndexProperty.setNative](value: number, animated = true) {
if (this.isLoaded) {
this.scrollToIndexAnimated(value, !this.disableAnimation);
this.scrollToIndexAnimated(value, animated && !this.disableAnimation);
}
}

Expand Down

0 comments on commit 696c611

Please sign in to comment.