<commit_before>/* @flow */
'use strict';

var React = require('react-native');
var Moment = require('moment');
var TimerMixin = require('react-native-timer-mixin');
var AudioPlayer = require('react-native-audioplayer');
var _ = require('lodash');

var formatTime = require('./common/format-time');

var {
  AppRegistry,
  PickerIOS,
  Text,
  View,
  TouchableHighlight,
  AlertIOS,
  PushNotificationIOS
} = React;


var PickerItemIOS = PickerIOS.Item;

var PRESSURES_AND_MINUTES = {
  '300': {
    bar: 300,
    minutes: 51,
  },
  '290': {
    bar: 290,
    minutes: 49,
  },
  '280': {
    bar: 280,
    minutes: 47,
  },
  '270': {
    bar: 270,
    minutes: 45,
  },
  '260': {
    bar: 260,
    minutes: 44,
  },
  '250': {
    bar: 250,
    minutes: 42,
  },
  '240': {
    bar: 240,
    minutes: 40,
  },
  '230': {
    bar: 230,
    minutes: 38,
  },
  '220': {
    bar: 220,
    minutes: 36,
  },
  '210': {
    bar: 210,
    minutes: 34,
  },
  '200': {
    bar: 200,
    minutes: 33,
  },
  '190': {
    bar: 190,
    minutes: 31,
  },
  '180': {
    bar: 180,
    minutes: 29,
  },
  '170': {
    bar: 170,
    minutes: 27,
  },
  '160': {
    bar: 160,
    minutes: 25,
  },
  '150': {
    bar: 150,
    minutes: 23,
  },
  '140': {
    bar: 140,
    minutes: 21,
  },
  '130': {
    bar: 130,
    minutes: 19,
  },
  '120': {
    bar: 120,
    minutes: 17,
  },
  '110': {
    bar: 110,
    minutes: 15,
  },
};

var SaveTheShoes = React.createClass({
  mixins: [TimerMixin],

  pressure: function() {
    var pressure = PRESSURES_AND_MINUTES[this.state.barPressure];

    return {
      pressureData: pressure,
      selectedBar: pressure.bar,
      minutesOfAir: pressure.minutes
    };
  },

  componentDidMount () {
    this.setInterval(this.decrementTimer, 1000);
  },

  startTimer: function () {
    if (this.state.timerRunning) {
      AlertIOS.alert(
          'Stop the timer?',
          'Are you sure you want to stop the timer?', [
          {text: 'Yes', onPress: () => this.setState({timerRunning: false, displayTimerScreen: false})},
          {text: 'No'},
          ]);

      return;
    }

    this.setState({timerRunning: !this.state.timerRunning, inTime: Moment()});

    this.setState({displayTimerScreen: true});

    // TODO make a timer for each stage (- 15 for RA time currently)
    var timeInMinutes = PRESSURES_AND_MINUTES[this.state.barPressure].minutes - 15;
    this.setState({timeRemaining: Moment.duration(timeInMinutes, 'minutes')});
  },

  timerScreen: function() {
    this.setState({displayTimerScreen: false});
  },

  decrementTimer: function() {
    if (!this.state.timerRunning) {
      return false;
    }

    // TODO - deltatime
    this.setState({timeRemaining: this.state.timeRemaining.subtract(1, 'second')});

    if (this.state.timeRemaining <= 0) {
      AudioPlayer.play('alarm.mp3');

      AlertIOS.alert(
        'Beep beep',
        "Time for relief assembly!"
      );

      this.setState({timerRunning: false});
    }
  },

  getInitialState: function() {
    return {
      barPressure: '110',
      timerRunning: false,
      inTime: null,
      timeRemaining: Moment.duration(0)
    };
  },

  timesRunning: function(pressure) {
    if(this.state.inTime != null){
      var inTime = Moment(this.state.inTime);
      var outTime = Moment(this.state.inTime).add(pressure.minutes, 'minutes');<commit_msg>Fix #20, adds staged alarm sounds<commit_after>/* @flow */
'use strict';

var React = require('react-native');
var Moment = require('moment');
var TimerMixin = require('react-native-timer-mixin');
var AudioPlayer = require('react-native-audioplayer');
var _ = require('lodash');

var formatTime = require('./common/format-time');

var {
  AppRegistry,
  PickerIOS,
  Text,
  View,
  TouchableHighlight,
  AlertIOS,
  PushNotificationIOS
} = React;


var PickerItemIOS = PickerIOS.Item;

var PRESSURES_AND_MINUTES = {
  '300': {
    bar: 300,
    minutes: 51,
  },
  '290': {
    bar: 290,
    minutes: 49,
  },
  '280': {
    bar: 280,
    minutes: 47,
  },
  '270': {
    bar: 270,
    minutes: 45,
  },
  '260': {
    bar: 260,
    minutes: 44,
  },
  '250': {
    bar: 250,
    minutes: 42,
  },
  '240': {
    bar: 240,
    minutes: 40,
  },
  '230': {
    bar: 230,
    minutes: 38,
  },
  '220': {
    bar: 220,
    minutes: 36,
  },
  '210': {
    bar: 210,
    minutes: 34,
  },
  '200': {
    bar: 200,
    minutes: 33,
  },
  '190': {
    bar: 190,
    minutes: 31,
  },
  '180': {
    bar: 180,
    minutes: 29,
  },
  '170': {
    bar: 170,
    minutes: 27,
  },
  '160': {
    bar: 160,
    minutes: 25,
  },
  '150': {
    bar: 150,
    minutes: 23,
  },
  '140': {
    bar: 140,
    minutes: 21,
  },
  '130': {
    bar: 130,
    minutes: 19,
  },
  '120': {
    bar: 120,
    minutes: 17,
  },
  '110': {
    bar: 110,
    minutes: 15,
  },
};

var SaveTheShoes = React.createClass({
  mixins: [TimerMixin],

  pressure: function() {
    var pressure = PRESSURES_AND_MINUTES[this.state.barPressure];

    return {
      pressureData: pressure,
      selectedBar: pressure.bar,
      minutesOfAir: pressure.minutes
    };
  },

  componentDidMount () {
    this.setInterval(this.decrementTimer, 1000);
  },

  startTimer: function () {
    if (this.state.timerRunning) {
      AlertIOS.alert(
          'Stop the timer?',
          'Are you sure you want to stop the timer?', [
          {text: 'Yes', onPress: () => this.setState({timerRunning: false, displayTimerScreen: false})},
          {text: 'No'},
          ]);

      return;
    }

    this.setState({timerRunning: !this.state.timerRunning, inTime: Moment()});

    this.setState({displayTimerScreen: true});

    // TODO make a timer for each stage (- 15 for RA time currently)
    var timeInMinutes = PRESSURES_AND_MINUTES[this.state.barPressure].minutes - 15;
    this.setState({timeRemaining: Moment.duration(timeInMinutes, 'minutes')});
  },

  timerScreen: function() {
    this.setState({displayTimerScreen: false});
  },

  decrementTimer: function() {
    if (!this.state.timerRunning) {
      return false;
    }

    this.setState({timeRemaining: this.state.timeRemaining.subtract(1, 'second')});

    if(this.state.inTime != null && this.state.alarmsRemaining.length > 0) {
      var currentAlarm = this.state.alarmsRemaining[0];
      var alarmTime = Moment(this.state.inTime).add(this.pressure().pressureData.minutes - currentAlarm.offset, 'minutes');
      var remainingTime = Moment(alarmTime).diff(Moment(), 'seconds');

      if(remainingTime < 0) {
        this.setState({
          alarmsRemaining: this.state.alarmsRemaining.filter((_, i) => i !== 0)
        })

        AudioPlayer.play(currentAlarm.alarmSound);

        if(currentAlarm.offset == 0){
          // Final alarm (time due out), display popup
          AlertIOS.alert(
              'Beep beep',
              "Time for relief assembly!"
              );
          this.setState({timerRunning: false});
        }
      }
    }
  },

  getInitialState: function() {
    return {
      barPressure: '110',
      timerRunning: false,
      inTime: null,
      timeRemaining: Moment.duration(0),
      alarmsRemaining: [
        {alarmSound: 'relief_assembly.mp3', offset: 15},
        {alarmSound: 'relief_in.mp3', offset: 10},
        {alarmSound: 'alarm.mp3', offset: 0}
      ]
    };
  },

  timesRunning: function(pressure) {
    if(this.state.inTime != null){
      var inTime = Moment(this.state.inTime);
      var outTime = Moment(this.state.inTime).add(pressure.minutes, 'minutes');
{'fraction_non_alphanumeric': 0.15827911299960165, 'fraction_numerical': 0.04820077015004647, 'mean_word_length': 2.487037037037037, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1488040', 'n_tokens_mistral': 3002, 'n_tokens_neox': 2616, 'n_words': 559}