Skip to content

fix for Android RTL layout #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions src/components/carousel/CarouselPresenter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _ from 'lodash';
import {Constants} from '../../helpers';


export function getChildrenLength(props) {
Expand All @@ -10,27 +9,12 @@ export function getChildrenLength(props) {
export function calcOffset(props, state) {
const {currentPage, pageWidth} = state;
const {loop} = props;

const actualCurrentPage = loop ? currentPage + 1 : currentPage;

let offset = pageWidth * actualCurrentPage;
offset = getDirectionOffset(offset, props);
const offset = pageWidth * actualCurrentPage;

return offset;
}

export function getDirectionOffset(offset, props, pageWidth) {
let fixedOffset = offset;

if (Constants.isRTL && Constants.isAndroid) {
const {loop} = props;
const totalWidth = ((getChildrenLength(props) - 1) + (loop ? 2 : 0)) * pageWidth;
fixedOffset = Math.abs(totalWidth - offset);
}

return fixedOffset;
}

export function calcPageIndex(offset, props, pageWidth) {
const pagesCount = getChildrenLength(props);
const {loop} = props;
Expand All @@ -42,7 +26,6 @@ export function calcPageIndex(offset, props, pageWidth) {
} else {
actualPageIndex = Math.min(pagesCount - 1, pageIndexIncludingClonedPages);
}

return actualPageIndex;
}

Expand All @@ -57,6 +40,7 @@ export function isOutOfBounds(offset, props, pageWidth) {
// TODO: need to support more cases of page width in loop mode
export function calcCarouselWidth(props) {
const {pageWidth, loop} = props;

let length = getChildrenLength(props);
length = loop ? length + 2 : length;
return pageWidth * length;
Expand Down
24 changes: 18 additions & 6 deletions src/components/carousel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export default class Carousel extends BaseComponent {

if (this.carousel) {
this.carousel.current.scrollTo({x, animated});

if (Constants.isAndroid) {
// this is done to handle onMomentumScrollEnd not being called in Android:
// https://github.com/facebook/react-native/issues/11693
Expand All @@ -98,21 +99,32 @@ export default class Carousel extends BaseComponent {
}

goToPage(pageIndex, animated = true) {
this.setState({currentPage: pageIndex}, () => this.updateOffset(animated));
this.setState({currentPage: this.getCalcIndex(pageIndex)}, () => this.updateOffset(animated));
}

getCalcIndex(index) {
// to handle scrollView index issue in Android's RTL layout
if (Constants.isRTL && Constants.isAndroid) {
const length = presenter.getChildrenLength(this.props) - 1;
return length - index;
}
return index;
}

onContentSizeChange = () => {
// this is to handle initial scroll position (content offset)
if (Constants.isAndroid) {
this.updateOffset();
}
}

// finished full page scroll
onMomentumScrollEnd = () => {
// finished full page scroll
const {currentStandingPage, currentPage} = this.state;
this.setState({currentStandingPage: currentPage});
if (currentStandingPage !== currentPage) {
_.invoke(this.props, 'onChangePage', currentPage, currentStandingPage);
const index = this.getCalcIndex(currentPage);
this.setState({currentStandingPage: index});
if (currentStandingPage !== index) {
_.invoke(this.props, 'onChangePage', index, currentStandingPage);
}
}

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

const {loop} = this.props;
const {pageWidth} = this.state;
const offsetX = presenter.getDirectionOffset(event.nativeEvent.contentOffset.x, this.props, pageWidth);
const offsetX = event.nativeEvent.contentOffset.x;

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