Skip to content

Commit 0b38d56

Browse files
committed
fix: indicator in circular mode
1 parent 7bb05e5 commit 0b38d56

File tree

4 files changed

+119
-115
lines changed

4 files changed

+119
-115
lines changed

src/ui-pager-indicator/index.common.ts

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,4 @@
1-
import {
2-
AddChildFromBuilder,
3-
Builder,
4-
CSSType,
5-
CoercibleProperty,
6-
Color,
7-
ContainerView,
8-
CoreTypes,
9-
GridLayout,
10-
ItemsSource,
11-
KeyedTemplate,
12-
Label,
13-
Length,
14-
Observable,
15-
ObservableArray,
16-
PercentLength,
17-
Property,
18-
Template,
19-
Trace,
20-
View,
21-
ViewBase,
22-
addWeakEventListener,
23-
makeParser,
24-
makeValidator,
25-
removeWeakEventListener
26-
} from '@nativescript/core';
1+
import { CSSType, Color, ItemsSource, Property, View, ViewBase } from '@nativescript/core';
272
import { IndicatorHolder } from '.';
283
export enum Indicator {
294
Disabled = 'disable',
@@ -62,6 +37,21 @@ export abstract class PagerIndicatorBase extends View {
6237
pagerView: View & IndicatorHolder;
6338
pagerViewId: string;
6439

40+
getPage() {
41+
if (this.page) {
42+
return this.page;
43+
} else {
44+
return this.getTopmost(this);
45+
}
46+
}
47+
getTopmost<View>(arg: ViewBase) {
48+
if (arg.parent) {
49+
return this.getTopmost(arg.parent);
50+
} else {
51+
return arg;
52+
}
53+
}
54+
6555
disposeNativeView() {
6656
this.pagerView = null;
6757
super.disposeNativeView();
@@ -70,12 +60,11 @@ export abstract class PagerIndicatorBase extends View {
7060
onLoaded() {
7161
super.onLoaded();
7262
if (this.pagerViewId) {
73-
this.setPagerView(this.page.getViewById(this.pagerViewId));
63+
this.setPagerView(this.getPage().getViewById(this.pagerViewId));
7464
}
7565
}
7666

7767
setPagerView(view: View) {
78-
console.log('setPagerView', view);
7968
if (this.pagerView !== view) {
8069
if (this.pagerView) {
8170
this.pagerView.setIndicator(null);
@@ -89,7 +78,9 @@ export abstract class PagerIndicatorBase extends View {
8978
}
9079

9180
[pagerViewIdProperty.setNative](value) {
92-
this.setPagerView(this.page.getViewById(value));
81+
if (this.page) {
82+
this.setPagerView(this.page?.getViewById(value));
83+
}
9384
}
9485

9586
abstract setSelection(index: number, animated?: boolean);

src/ui-pager-indicator/index.ios.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ export class PagerIndicator extends PagerIndicatorBase {
5858
}
5959
setInteractiveAnimation(animated?: boolean) {}
6060
setSelection(progress: number, animated: boolean = true) {
61-
console.log("Setting selection", progress, animated);
6261
NSCProgressHelper.setProgressWithPageControlProgressAnimated(this.nativeViewProtected, progress, animated);
6362
}
6463
setCount(count: number) {

src/ui-pager/index.android.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ export class Pager extends PagerBase {
255255
}
256256
this._initAutoPlay(this.autoPlay);
257257
}
258+
if (this.indicator && this.mObservableArrayInstance && this.mObservableArrayInstance.length) {
259+
this.indicator.setCount(this.mObservableArrayInstance.length);
260+
this.pagerAdapter.notifyDataSetChanged();
261+
this.scrollToIndexAnimated(0, false);
262+
}
258263
};
259264
disposeViewHolderViews() {
260265
this.enumerateViewHolders((v) => {
@@ -318,7 +323,7 @@ export class Pager extends PagerBase {
318323
const indicator = this.indicator;
319324
const toDo = () => {
320325
nativeView.setCurrentItem(index, false);
321-
indicator.setSelection(this.selectedIndex, false);
326+
if (indicator) indicator.setSelection(this.selectedIndex, false);
322327
};
323328
if (indicator) {
324329
indicator.withoutAnimation(toDo);
@@ -363,10 +368,13 @@ export class Pager extends PagerBase {
363368
public scrollToIndexAnimated(index: number, animate: boolean) {
364369
const nativeView = this.nativeViewProtected;
365370
if (nativeView) {
366-
nativeView.setCurrentItem(index, animate);
371+
nativeView.setCurrentItem(this.pagerAdapter.getIndex(index), animate);
367372
if (!animate) {
368373
// without animate we wont go through the delegate
369374
selectedIndexProperty.nativeValueChange(this, index);
375+
if (this.indicator) {
376+
this.indicator.setSelection(index, false);
377+
}
370378
}
371379
}
372380
}
@@ -527,9 +535,6 @@ export class Pager extends PagerBase {
527535
if (isRightOverScrolled || isLeftOverScrolled) {
528536
selectedPosition = position;
529537
}
530-
if (isRightOverScrolled) {
531-
indicator.setSelection(selectedPosition);
532-
}
533538

534539
const slideToRightSide = selectedPosition === position && positionOffset !== 0;
535540
let selectingPosition;
@@ -586,7 +591,9 @@ function initPagerChangeCallback() {
586591
if (owner.lastEvent === 0 && !owner.circularMode) {
587592
// page changing without scroll so do the indicator etc.
588593
selectedIndexProperty.nativeValueChange(owner, position);
589-
owner.indicator?.setSelection(position, true);
594+
if (owner.indicator) {
595+
owner.indicator.setSelection(position, true);
596+
}
590597
}
591598

592599
owner.notify({
@@ -627,7 +634,10 @@ function initPagerChangeCallback() {
627634
const selectingPosition = progress[0];
628635
const selectingProgress = progress[1];
629636
indicator.setInteractiveAnimation(true);
630-
indicator.setProgress(selectingPosition, selectingProgress);
637+
if (position < owner.lastIndex) {
638+
indicator.setSelection(position, false);
639+
indicator.setProgress(selectingPosition, selectingProgress);
640+
}
631641
}
632642
}
633643
}
@@ -710,6 +720,7 @@ interface PagerRecyclerAdapter extends androidx.recyclerview.widget.RecyclerView
710720
new (owner: WeakRef<Pager>): PagerRecyclerAdapter;
711721
getPosition(index: number): number;
712722
lastIndex(): number;
723+
getIndex(index: number): number;
713724
}
714725
// eslint-disable-next-line no-redeclare
715726
let PagerRecyclerAdapter: PagerRecyclerAdapter;
@@ -775,7 +786,27 @@ function initPagerRecyclerAdapter() {
775786
}
776787
return position;
777788
}
778-
789+
/**
790+
*
791+
* Get the position in the CollectionView from the selected index
792+
*
793+
* @param index The position in the collectionView
794+
* @returns The selected Index ( i.e. the number in the slides as the user would view it).
795+
*/
796+
getIndex(index: number): number {
797+
let position = index;
798+
const owner = this.owner && this.owner.get();
799+
if (owner && owner.circularMode) {
800+
if (position === 0) {
801+
position = 1;
802+
} else if (position === this.firstDummy()) {
803+
position = 0;
804+
} else {
805+
position = position + 1;
806+
}
807+
}
808+
return position;
809+
}
779810
onBindViewHolder(holder: any, index: number): void {
780811
const owner = this.owner ? this.owner.get() : null;
781812
if (owner) {

0 commit comments

Comments
 (0)