leecade / leecade/react-native-swiper

Delay in loop mode and Arial trouble

Open
#586 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Need answer from author
Dominant language
JavaScript
Stars
10.5k
Forks
2.3k
PR merge metrics
No merged PRs in 30d

Description

  • android
  • react-native-swiper v1.5.12
  • react-native v0.47.0
  • expo v20.0.0
  1. First thing I encountered was lack of Arial in my stock based rom. I attempted to download font file but found that it is commercial and price is incredible. Anyway I don't need this functionality, so I just deleted style rule. It will be much better if you change Arial to something free/open-source so this font could be added to app package for better rom support.

  2. Second thing was delay while loop. I use loop and dynamic content change to create infinity scroll effect. My app is a kind of calendar and its draft looks so:

//functions to screen weekends 
function getDays(c){
  let center = findNext(c,1);
  let arr = [center];
  for(let i=0;i<3;i++){
    arr.push(findNext(arr[i * 2] + 1000 * 60 * 60 * 24,1));
    arr.unshift(findNext(arr[0] - 1000 * 60 * 60 * 24,-1));
  }
  return arr;
}
function findNext(candidate,direction){
  if(new Date(candidate).getDay() != 0 && new Date(candidate).getDay() !=6){
    return candidate;
  }
  else {
    if(new Date(candidate + 1000 * 60 * 60 * 24 * direction).getDay() == new Date(candidate).getDay()){
      return findNext(candidate + 1000 * 60 * 60 * 24 * direction + 1000*60*60* direction,direction);
    }
    return findNext(candidate + 1000 * 60 * 60 * 24 * direction,direction);
  }
}



export default class App extends React.Component {
  constructor(props) {
    super(props);
    let days = getDays(Date.now());

    this.state = {
      dates:days,
      current: 3,
    };
  }

//dynamic content changing
  onScrollEnd (e,state) {
    let newDates = getDays(this.state.dates[state.index]);

//shifting array to match old locations
    for(let i=0; newDates[state.index] != this.state.dates[state.index] ;i++){
        newDates.push(newDates.shift());
    }
    
    this.setState({
      dates: newDates,
      current: state.index,
    });
  }
  render() {
    return (
      <View style={styles.container}>
        <Swiper style={styles.wrapper} loop={true} index={3} showsPagination={false} onMomentumScrollEnd ={this.onScrollEnd.bind(this)}>
          <View style={styles.slide1}>
            <Card date={this.state.dates[0]}/>
          </View>
          <View style={styles.slide2}>
            <Card date={this.state.dates[1]}/>
          </View>
          <View style={styles.slide3}>
            <Card date={this.state.dates[2]}/>
          </View>
          <View style={styles.slide4}>
            <Card date={this.state.dates[3]}/>
          </View>
          <View style={styles.slide5}>
            <Card date={this.state.dates[4]}/>
          </View>
          <View style={styles.slide6}>
            <Card date={this.state.dates[5]}/>
          </View>
          <View style={styles.slide7}>
            <Card date={this.state.dates[6]}/>
          </View>
        </Swiper>
      </View>
    );
  }
}

In first draft I used only 3 exemplars of <Card/> and loop worked perfect but contemplation of content rendering was disgusting. To hide it I added 4 extra cards. I achieved my goal, but then I spotted delay while loop. It wasn't graphic freeze or lag: android showed end of list ripple. Delay was from 0.5 to 2-3 seconds and only on 6>5 and 0>1 transitions. It was shot in the dark, but I overcame this trouble with changing timeout delay to 0 in loopJump method:

//react-native-swiper/src/index.js
loopJump = () => {
    if (!this.state.loopJump) return
    const i = this.state.index + (this.props.loop ? 1 : 0)
    const scrollView = this.scrollView
    this.loopJumpTimer = setTimeout(() => scrollView.setPageWithoutAnimation &&
      scrollView.setPageWithoutAnimation(i), 0)
  }

Obviously there was a reason to set initially this timeout different from 0, can you clarify that?

Anyway thanks for great work!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/index.js and the loopJump method; reproduce the reported 6→5 and 0→1 transitions on the listed React Native and Android versions, then compare the behavior with the timeout set to 0. Also locate the style rule using Arial. Done means the affected loop transitions no longer show the reported delay and the font choice does not require a commercial font.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, javascript, react-native
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.