@@ -5,8 +5,8 @@ import * as Notifications from "expo-notifications";
55import { useNavigation } from "@react-navigation/native" ;
66import * as Effect from "effect/Effect" ;
77import { AsyncResult } from "effect/unstable/reactivity" ;
8- import { useCallback , useEffect , useMemo , useState , useSyncExternalStore } from "react" ;
9- import { Alert , Linking , Platform } from "react-native" ;
8+ import { useCallback , useEffect , useMemo , useRef , useState , useSyncExternalStore } from "react" ;
9+ import { Alert , AppState , Linking , Platform } from "react-native" ;
1010import { useSafeAreaInsets } from "react-native-safe-area-context" ;
1111
1212import { AppText as Text } from "../../components/AppText" ;
@@ -90,10 +90,14 @@ function ConfiguredSettingsNotificationsRouteScreen() {
9090 const { savedConnectionsById } = useSavedRemoteConnections ( ) ;
9191 const [ notificationStatus , setNotificationStatus ] = useState < NotificationStatus > ( "checking" ) ;
9292 const [ liveActivityStatus , setLiveActivityStatus ] = useState < LiveActivityStatus > ( "checking" ) ;
93+ const liveActivityWriteInFlight = useRef ( false ) ;
9394 const deviceRegistered = useDeviceRegistered ( ) ;
9495 const liveActivitiesPreferenceEnabled = AsyncResult . isSuccess ( preferencesResult )
9596 ? preferencesResult . value . liveActivitiesEnabled !== false
9697 : true ;
98+ const canClearLiveActivitiesPreference =
99+ AsyncResult . isSuccess ( preferencesResult ) &&
100+ preferencesResult . value . liveActivitiesEnabled !== false ;
97101
98102 const connections = useMemo ( ( ) => Object . values ( savedConnectionsById ) , [ savedConnectionsById ] ) ;
99103 const environmentCount = connections . length ;
@@ -114,6 +118,10 @@ function ConfiguredSettingsNotificationsRouteScreen() {
114118
115119 useEffect ( ( ) => {
116120 void refreshNotifications ( ) ;
121+ const subscription = AppState . addEventListener ( "change" , ( state ) => {
122+ if ( state === "active" ) void refreshNotifications ( ) ;
123+ } ) ;
124+ return ( ) => subscription . remove ( ) ;
117125 } , [ refreshNotifications ] ) ;
118126
119127 useEffect ( ( ) => {
@@ -338,42 +346,50 @@ function ConfiguredSettingsNotificationsRouteScreen() {
338346
339347 const handleLiveActivitiesChange = useCallback (
340348 ( enabled : boolean ) => {
349+ if ( liveActivityWriteInFlight . current ) return ;
341350 if ( ! enabled ) {
342- setLiveActivityStatus ( "disabled" ) ;
351+ liveActivityWriteInFlight . current = true ;
352+ setLiveActivityStatus ( "linking" ) ;
343353 void ( async ( ) => {
344- let token : string | null = null ;
345- if ( isSignedIn ) {
346- const tokenResult = await settlePromise ( ( ) =>
347- getToken ( resolveRelayClerkTokenOptions ( ) ) ,
354+ try {
355+ let token : string | null = null ;
356+ if ( isSignedIn ) {
357+ const tokenResult = await settlePromise ( ( ) =>
358+ getToken ( resolveRelayClerkTokenOptions ( ) ) ,
359+ ) ;
360+ if ( tokenResult . _tag === "Failure" ) {
361+ setLiveActivityStatus ( "enabled" ) ;
362+ reportAtomCommandResult ( tokenResult , {
363+ label : "live activity disable token lookup" ,
364+ } ) ;
365+ return ;
366+ }
367+ token = tokenResult . value ;
368+ }
369+
370+ const updateResult = await settleAsyncResult ( ( ) =>
371+ runtime . runPromiseExit (
372+ setLiveActivityUpdatesEnabled ( {
373+ enabled : false ,
374+ previousEnabled : liveActivitiesPreferenceEnabled ,
375+ clerkToken : token ,
376+ connections,
377+ } ) ,
378+ ) ,
348379 ) ;
349- if ( tokenResult . _tag === "Failure" ) {
350- reportAtomCommandResult ( tokenResult , {
351- label : "live activity disable token lookup" ,
380+ if ( updateResult . _tag === "Failure" ) {
381+ setLiveActivityStatus ( isSignedIn ? "enabled" : "signed-out" ) ;
382+ reportAtomCommandResult ( updateResult , {
383+ label : "live activity disable" ,
352384 } ) ;
353385 return ;
354386 }
355- token = tokenResult . value ;
356- }
357-
358- const updateResult = await settleAsyncResult ( ( ) =>
359- runtime . runPromiseExit (
360- setLiveActivityUpdatesEnabled ( {
361- enabled : false ,
362- previousEnabled : liveActivitiesPreferenceEnabled ,
363- clerkToken : token ,
364- connections,
365- } ) ,
366- ) ,
367- ) ;
368- if ( updateResult . _tag === "Failure" ) {
369- setLiveActivityStatus ( "enabled" ) ;
370- reportAtomCommandResult ( updateResult , {
371- label : "live activity disable" ,
372- } ) ;
373- return ;
387+ savePreferences ( { liveActivitiesEnabled : false } ) ;
388+ refreshManagedRelayEnvironments ( ) ;
389+ setLiveActivityStatus ( "disabled" ) ;
390+ } finally {
391+ liveActivityWriteInFlight . current = false ;
374392 }
375- savePreferences ( { liveActivitiesEnabled : false } ) ;
376- refreshManagedRelayEnvironments ( ) ;
377393 } ) ( ) ;
378394 return ;
379395 }
@@ -383,7 +399,10 @@ function ConfiguredSettingsNotificationsRouteScreen() {
383399 return ;
384400 }
385401
386- void linkEnvironments ( ) ;
402+ liveActivityWriteInFlight . current = true ;
403+ void linkEnvironments ( ) . finally ( ( ) => {
404+ liveActivityWriteInFlight . current = false ;
405+ } ) ;
387406 } ,
388407 [
389408 connections ,
@@ -452,6 +471,13 @@ function ConfiguredSettingsNotificationsRouteScreen() {
452471 }
453472 onValueChange = { handleLiveActivitiesChange }
454473 />
474+ { liveActivityStatus === "signed-out" && canClearLiveActivitiesPreference ? (
475+ < SettingsRow
476+ icon = "bolt.circle"
477+ label = "Turn off Live Activity preference"
478+ onPress = { ( ) => handleLiveActivitiesChange ( false ) }
479+ />
480+ ) : null }
455481 { supportsAndroidLiveUpdateSettings ( ) ? (
456482 < SettingsRow
457483 icon = "bolt.circle"
0 commit comments