Skip to content

Commit 36a5bbd

Browse files
Inbal-Tishethanshar
authored andcommitted
fix for Android RTL layout (#514)
1 parent 63fe2ac commit 36a5bbd

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

src/components/carousel/CarouselPresenter.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import _ from 'lodash';
2-
import {Constants} from '../../helpers';
32

43

54
export function getChildrenLength(props) {
@@ -10,27 +9,12 @@ export function getChildrenLength(props) {
109
export function calcOffset(props, state) {
1110
const {currentPage, pageWidth} = state;
1211
const {loop} = props;
13-
1412
const actualCurrentPage = loop ? currentPage + 1 : currentPage;
15-
16-
let offset = pageWidth * actualCurrentPage;
17-
offset = getDirectionOffset(offset, props);
13+
const offset = pageWidth * actualCurrentPage;
1814

1915
return offset;
2016
}
2117

22-
export function getDirectionOffset(offset, props, pageWidth) {
23-
let fixedOffset = offset;
24-
25-
if (Constants.isRTL && Constants.isAndroid) {
26-
const {loop} = props;
27-
const totalWidth = ((getChildrenLength(props) - 1) + (loop ? 2 : 0)) * pageWidth;
28-
fixedOffset = Math.abs(totalWidth - offset);
29-
}
30-
31-
return fixedOffset;
32-
}
33-
3418
export function calcPageIndex(offset, props, pageWidth) {
3519
const pagesCount = getChildrenLength(props);
3620
const {loop} = props;
@@ -42,7 +26,6 @@ export function calcPageIndex(offset, props, pageWidth) {
4226
} else {
4327
actualPageIndex = Math.min(pagesCount - 1, pageIndexIncludingClonedPages);
4428
}
45-
4629
return actualPageIndex;
4730
}
4831

@@ -57,6 +40,7 @@ export function isOutOfBounds(offset, props, pageWidth) {
5740
// TODO: need to support more cases of page width in loop mode
5841
export function calcCarouselWidth(props) {
5942
const {pageWidth, loop} = props;
43+
6044
let length = getChildrenLength(props);
6145
length = loop ? length + 2 : length;
6246
return pageWidth * length;

src/components/carousel/index.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export default class Carousel extends BaseComponent {
8888

8989
if (this.carousel) {
9090
this.carousel.current.scrollTo({x, animated});
91+
9192
if (Constants.isAndroid) {
9293
// this is done to handle onMomentumScrollEnd not being called in Android:
9394
// https://github.com/facebook/react-native/issues/11693
@@ -98,21 +99,32 @@ export default class Carousel extends BaseComponent {
9899
}
99100

100101
goToPage(pageIndex, animated = true) {
101-
this.setState({currentPage: pageIndex}, () => this.updateOffset(animated));
102+
this.setState({currentPage: this.getCalcIndex(pageIndex)}, () => this.updateOffset(animated));
103+
}
104+
105+
getCalcIndex(index) {
106+
// to handle scrollView index issue in Android's RTL layout
107+
if (Constants.isRTL && Constants.isAndroid) {
108+
const length = presenter.getChildrenLength(this.props) - 1;
109+
return length - index;
110+
}
111+
return index;
102112
}
103113

104114
onContentSizeChange = () => {
115+
// this is to handle initial scroll position (content offset)
105116
if (Constants.isAndroid) {
106117
this.updateOffset();
107118
}
108119
}
109120

110-
// finished full page scroll
111121
onMomentumScrollEnd = () => {
122+
// finished full page scroll
112123
const {currentStandingPage, currentPage} = this.state;
113-
this.setState({currentStandingPage: currentPage});
114-
if (currentStandingPage !== currentPage) {
115-
_.invoke(this.props, 'onChangePage', currentPage, currentStandingPage);
124+
const index = this.getCalcIndex(currentPage);
125+
this.setState({currentStandingPage: index});
126+
if (currentStandingPage !== index) {
127+
_.invoke(this.props, 'onChangePage', index, currentStandingPage);
116128
}
117129
}
118130

@@ -124,7 +136,7 @@ export default class Carousel extends BaseComponent {
124136

125137
const {loop} = this.props;
126138
const {pageWidth} = this.state;
127-
const offsetX = presenter.getDirectionOffset(event.nativeEvent.contentOffset.x, this.props, pageWidth);
139+
const offsetX = event.nativeEvent.contentOffset.x;
128140

129141
if (offsetX >= 0) {
130142
const newPage = presenter.calcPageIndex(offsetX, this.props, pageWidth);

0 commit comments

Comments
 (0)