From f49e466bf4a7e5f254c34d586735beeb94e58efd Mon Sep 17 00:00:00 2001
From: "Thomas F. K. Jorna" <thomasfkjorna@gmail.com>
Date: Thu, 15 Jul 2021 20:06:10 +0200
Subject: very basic async storage of physics values

---
 app/screens/graph/graph-screen.tsx | 71 +++++++++++++++++++++++++++++++-------
 1 file changed, 58 insertions(+), 13 deletions(-)

diff --git a/app/screens/graph/graph-screen.tsx b/app/screens/graph/graph-screen.tsx
index 289f371..e6fbc19 100644
--- a/app/screens/graph/graph-screen.tsx
+++ b/app/screens/graph/graph-screen.tsx
@@ -1,4 +1,4 @@
-import React, { useState } from "react"
+import React, { useEffect, useState } from "react"
 import { observer } from "mobx-react-lite"
 import { ViewStyle } from "react-native"
 import { Screen, Text } from "../../components"
@@ -11,6 +11,7 @@ import { Tweaks } from "../../components"
 
 import genRandomTree from "../../data/randomdata";
 
+import AsyncStorage from "@react-native-async-storage/async-storage"
 
 const ROOT: ViewStyle = {
   backgroundColor: color.palette.black,
@@ -28,18 +29,62 @@ export const GraphScreen = observer(function GraphScreen() {
   const [linkStrength, setLinkStrength] = useState(1);
   const [linkIts, setLinkIts] = useState(1);
 
-  const [physics, setPhysics] = useState(
-  {
-      charge: -30,
-      collision: false,
-      linkStrength: 1,
-      linkIts: 1,
-      collapse: false,
-      threedim: false,
-      particles: 2,
-  });
+    const [physics, setPhysics] = useState({});
 
-    const gData = genRandomTree();
+  const getData = async () => {
+    try {
+      const value = await AsyncStorage.getItem('@physics')
+      if (value !== null) {
+          return JSON.parse(value);
+      } else {
+        return (
+          {
+            charge: -30,
+            collision: false,
+            linkStrength: 1,
+            linkIts: 1,
+            collapse: false,
+            threedim: false,
+            particles: 2,
+          });
+      }
+    } catch (e) {
+      console.log(e);
+    }
+  };
+
+    useEffect(() => {
+        getData()
+        .then(data => setPhysics(data));
+    }, []);
+
+        const storeData = async (value) => {
+            try {
+              const jsonValue = JSON.stringify(value);
+              await AsyncStorage.setItem('@physics', jsonValue);
+              console.log("Writing " + jsonValue);
+            } catch(e) {
+                console.log(e);
+            }
+        }
+  /* const [physics, setPhysics] = useState(
+*     {
+*         charge: -30,
+*         collision: false,
+*         linkStrength: 1,
+*         linkIts: 1,
+*         collapse: false,
+*         threedim: false,
+*         particles: 2,
+*       }); */
+    useEffect(() => {
+      console.log("Physics changed");
+      storeData(physics);
+      const test = getData();
+      console.log(test);
+    }, [physics]);
+
+  const gData = genRandomTree();
 
   return (
     <Screen style={ROOT} preset="scroll">
@@ -50,7 +95,7 @@ export const GraphScreen = observer(function GraphScreen() {
       />
       <Graph
         physics={physics}
-        gData = {gData}
+        gData={gData}
       />
     </Screen>
   );
-- 
cgit v1.2.3