@@ -5,12 +5,13 @@ import {
55 HoverGestureHandlerOnChangeEventPayload ,
66} from "@codeherence/react-native-graph" ;
77import { LinearGradient , vec } from "@shopify/react-native-skia" ;
8- import { useCallback , useMemo } from "react" ;
9- import { StyleSheet , Text , View , useWindowDimensions } from "react-native" ;
8+ import { useCallback , useMemo , useReducer } from "react" ;
9+ import { Button , StyleSheet , Text , View , useWindowDimensions } from "react-native" ;
1010import { useAnimatedProps , useSharedValue } from "react-native-reanimated" ;
1111import { useSafeAreaInsets } from "react-native-safe-area-context" ;
1212
1313import AnimatedText from "../components/AnimatedText" ;
14+ import aapl_prices from "../data/aapl_prices.json" ;
1415import msft_prices from "../data/msft_prices.json" ;
1516
1617const formatter = new Intl . NumberFormat ( "en-US" , {
@@ -31,16 +32,23 @@ const uiFormatter = (price: number) => {
3132 } ) . format ( price ) ;
3233} ;
3334
35+ const priceMap = {
36+ msft : msft_prices . results
37+ . reverse ( )
38+ . map < [ number , number ] > ( ( r ) => [ new Date ( r . date ) . getTime ( ) , r . close ] ) ,
39+ aapl : aapl_prices . results
40+ . reverse ( )
41+ . map < [ number , number ] > ( ( r ) => [ new Date ( r . date ) . getTime ( ) , r . close ] ) ,
42+ } ;
43+
3444export default ( ) => {
45+ const [ counter , increment ] = useReducer ( ( s : number ) => s + 1 , 0 ) ;
46+
3547 const { width } = useWindowDimensions ( ) ;
3648 const { top, bottom } = useSafeAreaInsets ( ) ;
37- const data : [ number , number ] [ ] = useMemo (
38- ( ) =>
39- msft_prices . results
40- . reverse ( )
41- . map < [ number , number ] > ( ( r ) => [ new Date ( r . date ) . getTime ( ) , r . close ] ) ,
42- [ ]
43- ) ;
49+
50+ const symbol : "msft" | "aapl" = counter % 2 === 0 ? "msft" : "aapl" ;
51+ const data : [ number , number ] [ ] = useMemo ( ( ) => priceMap [ symbol ] , [ symbol ] ) ;
4452
4553 const latestPrice = useSharedValue ( "0" ) ;
4654
@@ -65,6 +73,7 @@ export default () => {
6573
6674 return (
6775 < View style = { [ styles . container , { paddingTop : top , paddingBottom : bottom } ] } >
76+ < Button title = { `Showing ${ symbol } . Click to switch.` } onPress = { increment } />
6877 < AnimatedText style = { styles . price } animatedProps = { animatedProps } />
6978 < LineChart
7079 points = { data }
0 commit comments