From 49a686ac78b21be77d1192ae7f9de247a7650d80 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Taneli=20Riihim=C3=A4ki?= <m3034@student.jamk.fi>
Date: Wed, 3 Jul 2019 14:13:50 +0300
Subject: [PATCH] Tracking should work, need to implement endtoend

---
 App.js                        |  3 +-
 components/GameList.js        | 77 ++++++++++++++++++++++++++++++-----
 components/LocationTracker.js | 27 ++++++++++++
 3 files changed, 95 insertions(+), 12 deletions(-)
 create mode 100644 components/LocationTracker.js

diff --git a/App.js b/App.js
index a571093..a8fdf01 100755
--- a/App.js
+++ b/App.js
@@ -6,8 +6,7 @@ import Login from "./components/Login";
 
 export default class App extends React.Component {
   state = {
-    userLocation: null,
-    usersHistory: []
+    userLocation: null
   };
 
   userTrackingHandler = () => {
diff --git a/components/GameList.js b/components/GameList.js
index f6ecf08..ae1c7a6 100644
--- a/components/GameList.js
+++ b/components/GameList.js
@@ -1,14 +1,19 @@
 import React, { Component, Fragment } from "react";
-import { FlatList, Button } from "react-native";
+import { FlatList, Button, Picker, Text, StyleSheet } from "react-native";
+
+import LocationTracker from "./LocationTracker";
 
 class GameList extends Component {
   constructor(props) {
     super(props);
     this.state = {
       games: [],
+      tracking: false,
       selectedGame: null,
       editForm: false
     };
+
+    intervalID = 0;
   }
 
   componentDidMount() {
@@ -28,17 +33,69 @@ class GameList extends Component {
       });
   }
 
+  handleTrackingChange() {
+    this.setState({ tracking: !this.state.tracking });
+  }
+
+  startTracking() {
+    this.intervalID = setInterval(() => {
+      console.log("Nyt träkätää nim birusti.");
+    }, 3000);
+  }
+
+  stopTracking() {
+    clearInterval(this.intervalID);
+    console.log("Nyt lopetetaa seuranta.");
+  }
+
+  componentDidUpdate() {
+    if (this.state.tracking) {
+      this.startTracking();
+    } else if (!this.state.tracking) {
+      this.stopTracking();
+    }
+  }
+  componentWillUnmount() {
+    clearInterval(this.intervalID);
+  }
   render() {
-    return (
-      <Fragment>
-        {this.state.games.length > 0 && (
-          <FlatList
-            data={this.state.games}
-            renderItem={({ item }) => <Button title={item.name} />}
+    if (this.state.games.length > 0) {
+      const items = (
+        <Fragment>
+          <Picker
+            style={styles.picker}
+            selectedValue={this.state.selectedGame}
+            onValueChange={(selectedGame, itemIndex) =>
+              this.setState({ selectedGame })
+            }
+          >
+            {this.state.games.map(game => (
+              <Picker.Item label={game.name} value={game.id} />
+            ))}
+          </Picker>
+          <LocationTracker
+            tracking={this.state.tracking}
+            handleTrackingChange={this.handleTrackingChange.bind(this)}
           />
-        )}
-      </Fragment>
-    );
+        </Fragment>
+      );
+      return <Fragment>{items}</Fragment>;
+    } else return null;
   }
 }
+
+const styles = StyleSheet.create({
+  picker: {
+    width: 200,
+    height: 100
+  }
+});
+
 export default GameList;
+
+// {this.state.games.length > 0 && (
+//           <FlatList
+//             data={this.state.games}
+//             renderItem={({ item }) => <Button title={item.name} />}
+//           />
+//         )}
diff --git a/components/LocationTracker.js b/components/LocationTracker.js
new file mode 100644
index 0000000..1a20e54
--- /dev/null
+++ b/components/LocationTracker.js
@@ -0,0 +1,27 @@
+import React, { Fragment } from "react";
+import { Switch, Text, StyleSheet } from "react-native";
+
+const LocationTracker = props => {
+  return (
+    <Fragment>
+      <Text style={styles.titleText}>Tracking:</Text>
+      <Switch
+        style={styles.switch}
+        value={props.tracking}
+        onValueChange={props.handleTrackingChange}
+      />
+    </Fragment>
+  );
+};
+
+const styles = StyleSheet.create({
+  switch: {},
+  titleText: {
+    fontSize: 40,
+    fontWeight: "bold",
+    textAlign: "center",
+    textAlignVertical: "center"
+  }
+});
+
+export default LocationTracker;
-- 
GitLab