|
| 1 | +// Begin: spaghetti code |
| 2 | +import React, { Component } from 'react'; |
| 3 | +import { StyleSheet, Text, View, Image, ImageBackground, Alert, TouchableOpacity } from 'react-native'; |
| 4 | +import * as Location from 'expo-location'; |
| 5 | +import * as Permissions from 'expo-permissions'; |
| 6 | + |
| 7 | + |
| 8 | +export default class App extends Component { |
| 9 | + |
| 10 | + // init states |
| 11 | + state = { |
| 12 | + locationLat: null, |
| 13 | + locationLon: null, |
| 14 | + errorMessage: null, |
| 15 | + flagImage: true, |
| 16 | + textColour: '#FA5695' |
| 17 | + }; |
| 18 | + |
| 19 | + |
| 20 | + // location functions |
| 21 | + |
| 22 | + _getLocationPerms = async () => { |
| 23 | + let { status } = await Permissions.askAsync(Permissions.LOCATION); |
| 24 | + if (status !== 'granted') { |
| 25 | + this.setState({ |
| 26 | + errorMessage: 'Permissions to access location was denied', |
| 27 | + }); |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + findCoordinates = async () => { |
| 32 | + navigator.geolocation.getCurrentPosition( |
| 33 | + position => { |
| 34 | + const locationLat = JSON.stringify(position.coords.latitude); |
| 35 | + const locationLon = JSON.stringify(position.coords.longitude); |
| 36 | + |
| 37 | + this.setState({ locationLat }); |
| 38 | + this.setState({ locationLon }); |
| 39 | + }, |
| 40 | + error => Alert.alert(error.message), |
| 41 | + { enableHighAccuracy: false, maximumAge: 10 } |
| 42 | + ); |
| 43 | + }; |
| 44 | + |
| 45 | + changeImage() { |
| 46 | + if (this.state.textColour === 'white') { |
| 47 | + var textColour = '#FA5695'; |
| 48 | + } else { |
| 49 | + var textColour = 'white'; |
| 50 | + } |
| 51 | + this.setState({ |
| 52 | + flagImage:!this.state.flagImage, |
| 53 | + textColour: textColour |
| 54 | + }); |
| 55 | + }; |
| 56 | + |
| 57 | + tekstiStyle = function(options) { |
| 58 | + return { |
| 59 | + fontFamily: 'sans-serif', // android default font |
| 60 | + fontSize: 30, |
| 61 | + textAlign: "center", |
| 62 | + margin: 10, |
| 63 | + color: this.state.textColour // muuhun #F80160 |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + render() { |
| 68 | + |
| 69 | + const haversine = require('haversine'); |
| 70 | + |
| 71 | + setTimeout(this._getLocationPerms, 1000); // 2s |
| 72 | + setTimeout(this.findCoordinates, 1000); |
| 73 | + |
| 74 | + let toripolliisiLocation = { |
| 75 | + latitude: 65.0126795, |
| 76 | + longitude: 25.4649844 |
| 77 | + }; |
| 78 | + |
| 79 | + let start = { |
| 80 | + latitude: this.state.locationLat, |
| 81 | + longitude: this.state.locationLon |
| 82 | + }; |
| 83 | + |
| 84 | + return ( |
| 85 | + <ImageBackground source={ this.state.flagImage === true ? |
| 86 | + require('./assets/backgroundWhite.png') : |
| 87 | + require('./assets/backgroundPink.png')} |
| 88 | + style={{flex: 1, width: '100%', height: '100%'}}> |
| 89 | + <View style={styles.container}> |
| 90 | + |
| 91 | + <TouchableOpacity onPress={ this.changeImage.bind(this) }> |
| 92 | + <Image source={ this.state.flagImage === true ? |
| 93 | + require('./assets/ToripolliisiPink.png') : |
| 94 | + require('./assets/ToripolliisiWhite.png')} |
| 95 | + style={{width: 400, height: 500, resizeMode: 'center'}} /> |
| 96 | + </TouchableOpacity> |
| 97 | + |
| 98 | + <Text style={this.tekstiStyle()}> |
| 99 | + {"\n"}Etäisyys Toripolliisiin on |
| 100 | + </Text> |
| 101 | + <Text style={styles.etaisyys}> |
| 102 | + {String(haversine(start, toripolliisiLocation)).substring(0,6)} km |
| 103 | + </Text> |
| 104 | + |
| 105 | + </View> |
| 106 | + </ImageBackground> |
| 107 | + ); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +const styles = StyleSheet.create({ |
| 112 | + container: { |
| 113 | + flex: 1, |
| 114 | + // backgroundColor: '#fff', |
| 115 | + alignItems: 'center', |
| 116 | + justifyContent: 'center', |
| 117 | + }, |
| 118 | + |
| 119 | + teksti: { |
| 120 | + fontFamily: 'sans-serif', // android default font |
| 121 | + fontSize: 30, |
| 122 | + textAlign: "center", |
| 123 | + margin: 10, |
| 124 | + color: '#FA5695' // muuhun #F80160 |
| 125 | + }, |
| 126 | + |
| 127 | + etaisyys: { |
| 128 | + fontFamily: 'sans-serif-medium', |
| 129 | + fontSize: 42, |
| 130 | + textAlign: "center", |
| 131 | + fontWeight: 'bold', |
| 132 | + color: 'black' |
| 133 | + }, |
| 134 | +}); |
0 commit comments