<commit_before>import React, {Component, StyleSheet, Text, View} from 'react-native'

export default class CountdownTimer extends Component {
  constructor(props) {
    super(props)
    this.state = {
      timeRemaining: this.getInitialTimeRemaining(this.props.till),
      timeoutId: null,
      prevTime: null,
      isMounted: true
    }
  }

  componentDidMount() {
    this.tick()
  }

  componentWillReceiveProps(newProps) {
    if (this.state.timeoutId) clearTimeout(this.state.timeoutId)
    this.setState({prevTime: null, timeRemaining: newProps.initialTimeRemaining})
  }

  componentDidUpdate() {
    if ((!this.state.prevTime) && this.state.timeRemaining > 0 && this.state.isMounted) {
      this.tick()
    }
  }

  componentWillUnmount() {
    clearTimeout(this.state.timeoutId)
    this.setState({isMounted: false})
  }

  getInitialTimeRemaining(till) {
    return Math.abs(till.getTime() - new Date().getTime())
  }

  tick() {
    var currentTime = Date.now()
    var dt = this.state.prevTime ? (currentTime - this.state.prevTime) : 0
    var interval = this.props.interval

    var timeRemainingInInterval = (interval - (dt % interval))
    var timeout = timeRemainingInInterval

    if (timeRemainingInInterval < (interval / 2.0)) {
      timeout += interval
    }

    var timeRemaining = Math.max(this.state.timeRemaining - dt, 0)
    var countdownComplete = (this.state.prevTime && timeRemaining <= 0)

    if (this.state.isMounted) {
      if (this.state.timeoutId) { clearTimeout(this.state.timeoutId) }
      this.setState({
        timeoutId: countdownComplete ? null : setTimeout(this.tick.bind(this), timeout),
        prevTime: currentTime,
        timeRemaining: timeRemaining
      })
    }

    if (countdownComplete) {
      if (this.props.onFinish) { this.props.onFinish() }
      return
    }

    if (this.props.onTick) {
      this.props.onTick(timeRemaining)
    }
  }

  getFormattedTime(milliseconds) {
    var totalSeconds = Math.round(milliseconds / 1000)

    var seconds = parseInt(totalSeconds % 60, 10)
    var minutes = parseInt(totalSeconds / 60, 10) % 60
    var hours = parseInt(totalSeconds / 3600, 10)

    seconds = seconds < 10 ? '0' + seconds : seconds
    minutes = minutes < 10 ? '0' + minutes : minutes
    hours = hours < 10 ? '0' + hours : hours

    return (
      hours + ':' + minutes + ':' + seconds
    )
  }

  render() {
    const timeRemaining = this.state.timeRemaining
    return (
      <View>
        <Text>
          {this.getFormattedTime(timeRemaining)}
        </Text>
      </View>
    )
  }
}

//  PropTypes

CountdownTimer.propTypes = {
  till: React.PropTypes.object.isRequired,
  interval: React.PropTypes.number,
  onTick: React.PropTypes.func,
  onFinish: React.PropTypes.func
}

//  Default props

CountdownTimer.defaultProps = {
  interval: 1000,
  onTick: null,
  onFinish: null
}

//  Styles

const styles = StyleSheet.create({
  container: {<commit_msg>Added option to render custom timer<commit_after>import React, {Component} from 'react'
import {StyleSheet, Text, View} from 'react-native'

export default class CountdownTimer extends Component {
  constructor(props) {
    super(props)
    this.state = {
      timeRemaining: this.getInitialTimeRemaining(this.props.till),
      timeoutId: null,
      prevTime: null,
      isMounted: true
    }
  }

  componentDidMount() {
    this.tick()
  }

  componentWillReceiveProps(newProps) {
    if (this.state.timeoutId) clearTimeout(this.state.timeoutId)
    this.setState({prevTime: null, timeRemaining: newProps.initialTimeRemaining})
  }

  componentDidUpdate() {
    if ((!this.state.prevTime) && this.state.timeRemaining > 0 && this.state.isMounted) {
      this.tick()
    }
  }

  componentWillUnmount() {
    clearTimeout(this.state.timeoutId)
    this.setState({isMounted: false})
  }

  getInitialTimeRemaining(till) {
    return Math.abs(till.getTime() - new Date().getTime())
  }

  tick() {
    const currentTime = Date.now()
    const dt = this.state.prevTime ? (currentTime - this.state.prevTime) : 0
    const interval = this.props.interval

    const timeRemainingInInterval = (interval - (dt % interval))
    let timeout = timeRemainingInInterval

    if (timeRemainingInInterval < (interval / 2.0)) {
      timeout += interval
    }

    const timeRemaining = Math.max(this.state.timeRemaining - dt, 0)
    const countdownComplete = (this.state.prevTime && timeRemaining <= 0)

    if (this.state.isMounted) {
      if (this.state.timeoutId) { clearTimeout(this.state.timeoutId) }
      this.setState({
        timeoutId: countdownComplete ? null : setTimeout(this.tick.bind(this), timeout),
        prevTime: currentTime,
        timeRemaining: timeRemaining
      })
    }

    if (countdownComplete) {
      if (this.props.onFinish) {
        this.props.onFinish()
      }
      return
    }

    if (this.props.onTick) {
      this.props.onTick(timeRemaining)
    }
  }

  getFormattedTime(milliseconds) {
    const totalSeconds = Math.round(milliseconds / 1000)

    let seconds = parseInt(totalSeconds % 60, 10)
    let minutes = parseInt(totalSeconds / 60, 10) % 60
    let hours = parseInt(totalSeconds / 3600, 10)

    seconds = seconds < 10 ? '0' + seconds : seconds
    minutes = minutes < 10 ? '0' + minutes : minutes
    hours = hours < 10 ? '0' + hours : hours

    return (
      hours + ':' + minutes + ':' + seconds
    )
  }

  renderTick(data) {
    const time = {time: this.getFormattedTime(data)}
    return this.props.renderTick(time)
  }

  render() {
    return (
      <View>
          {this.renderTick(this.state.timeRemaining)}
      </View>
    )
  }
}

//  PropTypes

CountdownTimer.propTypes = {
  till: React.PropTypes.object.isRequired,
  interval: React.PropTypes.number,
  onTick: React.PropTypes.func,
  onFinish: React.PropTypes.func,
  renderTick: React.PropTypes.func.isRequired
}

//  Default props

CountdownTimer.defaultProps = {
  interval: 1000,
  onTick: null,
  onFinish: null
}

//  Styles

const styles = StyleSheet.create({
  container: {
{'fraction_non_alphanumeric': 0.12125723738626965, 'fraction_numerical': 0.012903225806451613, 'mean_word_length': 2.933636955107352, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6492067', 'n_tokens_mistral': 2021, 'n_tokens_neox': 1925, 'n_words': 390}