Skip to content
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

onLastPageReached callback added #44

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 26 additions & 12 deletions src/components/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import PropTypes from 'prop-types';
import React, { PureComponent, Children } from 'react';
import {
View,
ScrollView,
SafeAreaView,
Animated,
Platform,
ViewPropTypes,
} from 'react-native';
import { View, ScrollView, Animated, ViewPropTypes, SafeAreaView } from 'react-native';

import Indicator from '../indicator';
import styles from './styles';
Expand All @@ -18,6 +11,11 @@ function equal(a, b) {
return Math.abs(a - b) <= floatEpsilon * Math.max(Math.abs(a), Math.abs(b));
}

const position = {
next: 'next',
previous: 'previous',
}

export default class Pages extends PureComponent {
static defaultProps = {
pagingEnabled: true,
Expand All @@ -34,6 +32,7 @@ export default class Pages extends PureComponent {

horizontal: true,
rtl: false,
onLastPageReached: () => {},
};

static propTypes = {
Expand Down Expand Up @@ -64,8 +63,8 @@ export default class Pages extends PureComponent {
onLayout: PropTypes.func,
onScrollStart: PropTypes.func,
onScrollEnd: PropTypes.func,
onLastPageReached: PropTypes.func,
onHalfway: PropTypes.func,

renderPager: PropTypes.func,
};

Expand Down Expand Up @@ -141,9 +140,13 @@ export default class Pages extends PureComponent {

if (1 === this.scrollState && equal(discreteProgress, this.progress)) {
this.onScrollEnd();

this.currentChildNum = this.pageNumber(event.nativeEvent);
this.scrollState = -1;
}

var currentOffset = offset;
this.direction = currentOffset > this.offset ? position.next : position.previous;
this.offset = currentOffset;
}

onScrollBeginDrag() {
Expand All @@ -157,11 +160,15 @@ export default class Pages extends PureComponent {
}

onScrollEndDrag() {
let { horizontal } = this.props;
let { children, onLastPageReached } = this.props;

/* Vertical pagination is not working on android, scroll by hands */
if ('android' === Platform.OS && !horizontal) {
/*if ('android' === Platform.OS && !horizontal) {
this.scrollToPage(Math.round(this.progress));
}*/

if (this.currentChildNum == Children.count(children) - 1 && this.direction == position.next) {
onLastPageReached();
}

this.scrollState = 1;
Expand Down Expand Up @@ -210,6 +217,13 @@ export default class Pages extends PureComponent {
return 1 === this.scrollState;
}

pageNumber =({ layoutMeasurement, contentOffset })=> {
let { horizontal } = this.props;
var position = !horizontal ? Math.ceil(contentOffset.y) : Math.ceil(contentOffset.x);
var dimension = !horizontal ? Math.ceil(layoutMeasurement.height) : Math.ceil(layoutMeasurement.width);
return Math.ceil(position/dimension);
}

renderPage(page, index) {
let { width, height, progress } = this.state;
let { children, horizontal, rtl } = this.props;
Expand Down