|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { StyleSheet, Text, View } from 'react-native'; |
| 3 | +import Button from '../components/Button'; |
| 4 | +import { colors } from '../config/styles'; |
| 5 | +import TimerInput from '../components/TimerInput'; |
| 6 | + |
| 7 | +const styles = StyleSheet.create({ |
| 8 | + container: { |
| 9 | + flex: 1, |
| 10 | + justifyContent: 'center', |
| 11 | + alignItems: 'center', |
| 12 | + backgroundColor: colors.backgroundColor, |
| 13 | + }, |
| 14 | + instructions: { |
| 15 | + fontSize: 20, |
| 16 | + textAlign: 'left', |
| 17 | + margin: 10, |
| 18 | + }, |
| 19 | + component: { // TODO: need timer wheel and button components! |
| 20 | + fontSize: 20, |
| 21 | + textAlign: 'center', |
| 22 | + margin: 10, |
| 23 | + }, |
| 24 | +}); |
| 25 | + |
| 26 | +export default class App extends Component { |
| 27 | + constructor(props) { |
| 28 | + super(props); |
| 29 | + this.state = { |
| 30 | + time: '', |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + addColons = (time) => { |
| 35 | + if (time === "") |
| 36 | + return; |
| 37 | + |
| 38 | + let timeStr = time.replace(/[^0-9]/g, ""); |
| 39 | + timeStr = timeStr.replace(/:/g, ""); |
| 40 | + let ret = ""; |
| 41 | + |
| 42 | + if (timeStr.length === 0) { |
| 43 | + return timeStr; |
| 44 | + } else if (timeStr.length === 3 || timeStr.length === 5) { |
| 45 | + const left = timeStr.substr(0, 1); |
| 46 | + let right = timeStr.substr(1, timeStr.length); |
| 47 | + if (timeStr.length === 5) { |
| 48 | + right = right.match(/.{1,2}/g).join(":"); |
| 49 | + } |
| 50 | + ret = left + ":" + right; |
| 51 | + } else { |
| 52 | + ret = timeStr.match(/.{1,2}/g).join(":"); |
| 53 | + } |
| 54 | + return ret; |
| 55 | + }; |
| 56 | + |
| 57 | + formatTimeInput = (time) => { |
| 58 | + if (time === undefined) |
| 59 | + return; |
| 60 | + |
| 61 | + const timeArray = time.split(":"); |
| 62 | + let hours = -1; let minutes = -1; let seconds = -1; |
| 63 | + if (timeArray.length === 1) { |
| 64 | + seconds = parseInt(timeArray[0]); |
| 65 | + if (seconds - 60 >= 0) { |
| 66 | + seconds -= 60; |
| 67 | + minutes = 1; |
| 68 | + } |
| 69 | + } else if (timeArray.length === 2) { |
| 70 | + seconds = parseInt(timeArray[1]); |
| 71 | + minutes = parseInt(timeArray[0]); |
| 72 | + if (seconds - 60 >= 0) { |
| 73 | + seconds -= 60; |
| 74 | + console.log("MINUTES: " + minutes); |
| 75 | + minutes += 1; |
| 76 | + console.log("minnutes " + minutes + " seconds " + seconds); |
| 77 | + } |
| 78 | + if (minutes - 60 >= 0) { |
| 79 | + minutes -= 60; |
| 80 | + hours = 1; |
| 81 | + } |
| 82 | + } else if (timeArray.length === 3) { |
| 83 | + seconds = parseInt(timeArray[2]); |
| 84 | + minutes = parseInt(timeArray[1]); |
| 85 | + hours = parseInt(timeArray[0]); |
| 86 | + if (seconds - 60 >= 0) { |
| 87 | + seconds -= 60; |
| 88 | + minutes += 1; |
| 89 | + } |
| 90 | + if (minutes - 60 >= 0) { |
| 91 | + console.log("minutes before is: " + minutes); |
| 92 | + minutes -= 60; |
| 93 | + console.log("minutes after is: " + minutes); |
| 94 | + hours += 1; |
| 95 | + } |
| 96 | + if (hours > 99) { |
| 97 | + hours = 99; |
| 98 | + minutes = 59; |
| 99 | + seconds = 59; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (hours === -1) { |
| 104 | + hours = "00"; |
| 105 | + } else if (hours < 10) { |
| 106 | + hours = "0" + hours; |
| 107 | + } |
| 108 | + if (minutes === -1) { |
| 109 | + minutes = "00" ; |
| 110 | + } else if (minutes < 10) { |
| 111 | + minutes = "0" + minutes; |
| 112 | + } |
| 113 | + if (seconds === -1) { |
| 114 | + seconds = "00"; |
| 115 | + } else if (seconds < 10) { |
| 116 | + seconds = "0" + seconds; |
| 117 | + } |
| 118 | + console.log("hours, minutes, seconds: " + hours + " " + minutes + " " + seconds); |
| 119 | + return hours + ":" + minutes + ":" + seconds; |
| 120 | + }; |
| 121 | + |
| 122 | + render() { |
| 123 | + return ( |
| 124 | + <View style={styles.container}> |
| 125 | + <Text style={styles.instructions}>1. Pick a duration</Text> |
| 126 | + <TimerInput |
| 127 | + placeholder="00:00:00" |
| 128 | + onChangeText={(time) => { |
| 129 | + let formattedTime = this.addColons(time); |
| 130 | + this.setState({ time: formattedTime }); |
| 131 | + }} |
| 132 | + onSubmitEditing={(event) => { |
| 133 | + let time = this.state["time"]; |
| 134 | + time = this.formatTimeInput(time); |
| 135 | + this.setState({ time: time }); |
| 136 | + }} |
| 137 | + value={this.state.time} |
| 138 | + maxLength={8} |
| 139 | + /> |
| 140 | + <Text style={styles.instructions}>2. Start the timer!</Text> |
| 141 | + <Button text="Start timer" /> |
| 142 | + </View> |
| 143 | + ); |
| 144 | + } |
| 145 | +} |
0 commit comments