diff options
author | Thomas F. K. Jorna <[email protected]> | 2021-07-15 20:06:10 +0200 |
---|---|---|
committer | Thomas F. K. Jorna <[email protected]> | 2021-07-15 20:06:10 +0200 |
commit | f49e466bf4a7e5f254c34d586735beeb94e58efd (patch) | |
tree | 9a73fa2ea4f6c62865f0203775216ee946fe1556 | |
parent | f07cb0d2745f59fe07254698402b160e7535c641 (diff) |
very basic async storage of physics values
-rw-r--r-- | app/screens/graph/graph-screen.tsx | 71 |
1 files 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> ); |