From d8e9eef8b1e59aee678ed82b3220cdaab3c47b10 Mon Sep 17 00:00:00 2001 From: Dean Date: Tue, 20 Feb 2024 12:36:20 -0800 Subject: [PATCH 1/4] add orientation context --- src/context/OrientationContext.tsx | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/context/OrientationContext.tsx diff --git a/src/context/OrientationContext.tsx b/src/context/OrientationContext.tsx new file mode 100644 index 0000000..9ebf2a3 --- /dev/null +++ b/src/context/OrientationContext.tsx @@ -0,0 +1,36 @@ +import React, { createContext, useContext, useEffect, useState } from "react"; +import { Dimensions } from "react-native"; + +// Create the context +const OrientationContext = createContext(); + +// Custom hook for consuming the context +export const useOrientation = () => useContext(OrientationContext); + +// Provider component +export const OrientationProvider = ({ children }) => { + const [orientation, setOrientation] = useState(getOrientation()); + + function getOrientation() { + const { width, height } = Dimensions.get("window"); + return width < height ? "portrait" : "landscape"; + } + + useEffect(() => { + const updateOrientation = () => { + setOrientation(getOrientation()); + }; + + const subscription = Dimensions.addEventListener( + "change", + updateOrientation, + ); + return () => subscription.remove(); + }, []); + + return ( + + {children} + + ); +}; From 61f9add7419627eae5b2db556157e39c462a70dd Mon Sep 17 00:00:00 2001 From: Dean Date: Tue, 20 Feb 2024 12:50:34 -0800 Subject: [PATCH 2/4] deleted unnecessary comments --- src/context/OrientationContext.tsx | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/context/OrientationContext.tsx b/src/context/OrientationContext.tsx index 9ebf2a3..551e99a 100644 --- a/src/context/OrientationContext.tsx +++ b/src/context/OrientationContext.tsx @@ -1,13 +1,10 @@ import React, { createContext, useContext, useEffect, useState } from "react"; import { Dimensions } from "react-native"; -// Create the context const OrientationContext = createContext(); -// Custom hook for consuming the context export const useOrientation = () => useContext(OrientationContext); -// Provider component export const OrientationProvider = ({ children }) => { const [orientation, setOrientation] = useState(getOrientation()); From b802bb01e633b53d715d979b1dc562cd2e0cd423 Mon Sep 17 00:00:00 2001 From: Dean Date: Tue, 20 Feb 2024 13:06:01 -0800 Subject: [PATCH 3/4] best practices cleanup chats code review --- src/context/OrientationContext.tsx | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/context/OrientationContext.tsx b/src/context/OrientationContext.tsx index 551e99a..2e040e9 100644 --- a/src/context/OrientationContext.tsx +++ b/src/context/OrientationContext.tsx @@ -1,9 +1,10 @@ import React, { createContext, useContext, useEffect, useState } from "react"; import { Dimensions } from "react-native"; -const OrientationContext = createContext(); +const OrientationContext = createContext(null); -export const useOrientation = () => useContext(OrientationContext); +export const useOrientation = (): string | null => + useContext(OrientationContext); export const OrientationProvider = ({ children }) => { const [orientation, setOrientation] = useState(getOrientation()); @@ -18,10 +19,8 @@ export const OrientationProvider = ({ children }) => { setOrientation(getOrientation()); }; - const subscription = Dimensions.addEventListener( - "change", - updateOrientation, - ); + const subscription = Dimensions.addChangeListener(updateOrientation); + return () => subscription.remove(); }, []); From c77bc476aa9724cba2e0c3e098fb55a518cf617d Mon Sep 17 00:00:00 2001 From: Dean Date: Tue, 20 Feb 2024 13:12:40 -0800 Subject: [PATCH 4/4] undo chat hallucination --- src/context/OrientationContext.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/context/OrientationContext.tsx b/src/context/OrientationContext.tsx index 2e040e9..88ddb39 100644 --- a/src/context/OrientationContext.tsx +++ b/src/context/OrientationContext.tsx @@ -19,7 +19,10 @@ export const OrientationProvider = ({ children }) => { setOrientation(getOrientation()); }; - const subscription = Dimensions.addChangeListener(updateOrientation); + const subscription = Dimensions.addEventListener( + "change", + updateOrientation, + ); return () => subscription.remove(); }, []);