@@ -12,7 +12,7 @@ import * as Cause from "effect/Cause";
1212import { useCallback , useEffect , useRef , useState , type RefObject } from "react" ;
1313import * as Haptics from "expo-haptics" ;
1414import { Accelerometer } from "expo-sensors" ;
15- import { AppState , BackHandler , Pressable , StatusBar , View } from "react-native" ;
15+ import { Pressable , StatusBar , View } from "react-native" ;
1616import Animated , { FadeIn , FadeOut , ReduceMotion } from "react-native-reanimated" ;
1717import { useSafeAreaInsets } from "react-native-safe-area-context" ;
1818
@@ -27,6 +27,8 @@ import {
2727 findHeaderMenuAction ,
2828} from "../../components/ui/headerMenu.android" ;
2929import type { AppBarMenuItem } from "../../components/ui/AppBar.types" ;
30+ import { useAppForeground } from "../../lib/appState" ;
31+ import { useHardwareBack } from "../../lib/backHandler" ;
3032import { deriveBottomControlInsets } from "../../lib/reserved-regions" ;
3133import { useNativeColumnLayoutMetrics } from "../../native/native-layout-metrics" ;
3234import { deviceEnvironment , refreshDeviceHubAccess , useDeviceHubAccess } from "../../state/device" ;
@@ -88,7 +90,7 @@ export function DevicePreviewScreen({
8890 const handleBounds = deriveBottomControlInsets ( useNativeColumnLayoutMetrics ( ) ) ;
8991 const { themeVariables } = useAppearancePreferences ( ) ;
9092 const focused = useIsFocused ( ) ;
91- const [ foreground , setForeground ] = useState ( AppState . currentState !== "background" ) ;
93+ const foreground = useAppForeground ( ) ;
9294 const [ selectedKey , setSelectedKey ] = useState < string | null > ( initialDeviceKey ?? null ) ;
9395 const [ inputConnected , setInputConnected ] = useState ( false ) ;
9496 const [ streamAttempt , setStreamAttempt ] = useState ( 0 ) ;
@@ -107,12 +109,6 @@ export function DevicePreviewScreen({
107109 } ,
108110 [ embedded ] ,
109111 ) ;
110- useEffect ( ( ) => {
111- const subscription = AppState . addEventListener ( "change" , ( state ) =>
112- setForeground ( state !== "background" ) ,
113- ) ;
114- return ( ) => subscription . remove ( ) ;
115- } , [ ] ) ;
116112 useEffect ( ( ) => {
117113 if ( focused && state . data !== null && previews . length === 0 ) onClose ( ) ;
118114 } , [ focused , state . data , previews . length , onClose ] ) ;
@@ -127,14 +123,10 @@ export function DevicePreviewScreen({
127123 } ) ;
128124 return ( ) => subscription . remove ( ) ;
129125 } , [ focused , foreground , embedded ] ) ;
130- useEffect ( ( ) => {
131- if ( controlsVisible ) return ;
132- const subscription = BackHandler . addEventListener ( "hardwareBackPress" , ( ) => {
133- setControlsVisible ( true ) ;
134- return true ;
135- } ) ;
136- return ( ) => subscription . remove ( ) ;
137- } , [ controlsVisible ] ) ;
126+ useHardwareBack ( ! controlsVisible , ( ) => {
127+ setControlsVisible ( true ) ;
128+ return true ;
129+ } ) ;
138130
139131 const shutDownDevice = async ( ) => {
140132 if ( ! preview || shuttingDown ) return ;
0 commit comments