diff --git a/src/components/NetworkService.ts b/src/components/NetworkService.ts index 69a6ae0cc..7d77d1c46 100644 --- a/src/components/NetworkService.ts +++ b/src/components/NetworkService.ts @@ -1,14 +1,9 @@ -import { - useIconicTaxa, - useObservationUpdatesWhenFocused, -} from "sharedHooks"; +import { useIconicTaxa } from "sharedHooks"; import useTaxonCommonNames from "./hooks/useTaxonCommonNames"; import useWorkQueue from "./hooks/useWorkQueue"; const NetworkService = ( ) => { - useObservationUpdatesWhenFocused( ); - useIconicTaxa( { reload: true } ); // This only runs when App updates... which is rarely. It works for Settings // b/c it generally updates currentUser diff --git a/src/sharedHooks/index.ts b/src/sharedHooks/index.ts index 7b426c9c9..a2a93d5d0 100644 --- a/src/sharedHooks/index.ts +++ b/src/sharedHooks/index.ts @@ -22,7 +22,6 @@ export { default as useLocalObservations } from "./useLocalObservations"; export { default as useLocationPermission } from "./useLocationPermission"; export { default as useNavigateToObsEdit } from "./useNavigateToObsEdit"; export { default as useObservationsUpdates } from "./useObservationsUpdates"; -export { default as useObservationUpdatesWhenFocused } from "./useObservationUpdatesWhenFocused"; export { default as useQuery } from "./useQuery"; export { default as useRemoteObservation } from "./useRemoteObservation"; export { default as useScrollToOffset } from "./useScrollToOffset"; diff --git a/src/sharedHooks/useObservationUpdatesWhenFocused.js b/src/sharedHooks/useObservationUpdatesWhenFocused.js deleted file mode 100644 index d46840732..000000000 --- a/src/sharedHooks/useObservationUpdatesWhenFocused.js +++ /dev/null @@ -1,50 +0,0 @@ -// @flow - -import { RealmContext } from "providers/contexts"; -import { useCallback, useEffect } from "react"; -import { AppState } from "react-native"; -import safeRealmWrite from "sharedHelpers/safeRealmWrite"; - -const { useRealm } = RealmContext; - -const useObservationUpdatesWhenFocused = () => { - const realm = useRealm(); - - const setObservationUpdatesViewedRealm = useCallback( () => { - // Set all observations to viewed - const observations = realm - .objects( "Observation" ) - .filtered( "comments_viewed == false OR identifications_viewed == false" ); - if ( observations.length === 0 ) { return; } - safeRealmWrite( realm, () => { - observations.forEach( observation => { - observation.comments_viewed = true; - observation.identifications_viewed = true; - } ); - }, "setting comments_viewed and ids_viewed to true in useObservationsUpdatesWhenFocused" ); - }, [realm] ); - - const onAppStateChange = useCallback( - status => { - // if the app is coming back from the background, set all observations to viewed - if ( status === "active" ) { - setObservationUpdatesViewedRealm(); - } - }, - [setObservationUpdatesViewedRealm], - ); - - useEffect( () => { - // subscribe to app state changes - const subscription = AppState.addEventListener( - "change", - onAppStateChange, - ); - setObservationUpdatesViewedRealm(); - - // unsubscribe on unmount - return () => subscription?.remove(); - }, [onAppStateChange, setObservationUpdatesViewedRealm] ); -}; - -export default useObservationUpdatesWhenFocused; diff --git a/tests/integration/sharedHooks/useObservationUpdatesWhenFocused.test.js b/tests/integration/sharedHooks/useObservationUpdatesWhenFocused.test.js deleted file mode 100644 index 064987a74..000000000 --- a/tests/integration/sharedHooks/useObservationUpdatesWhenFocused.test.js +++ /dev/null @@ -1,63 +0,0 @@ -import { renderHook } from "@testing-library/react-native"; -import safeRealmWrite from "sharedHelpers/safeRealmWrite"; -import useObservationUpdatesWhenFocused from "sharedHooks/useObservationUpdatesWhenFocused"; -import factory from "tests/factory"; -import setupUniqueRealm from "tests/helpers/uniqueRealm"; - -// UNIQUE REALM SETUP -const mockRealmIdentifier = __filename; -const { mockRealmModelsIndex, uniqueRealmBeforeAll, uniqueRealmAfterAll } = setupUniqueRealm( - mockRealmIdentifier, -); -jest.mock( "realmModels/index", ( ) => mockRealmModelsIndex ); -jest.mock( "providers/contexts", ( ) => { - const originalModule = jest.requireActual( "providers/contexts" ); - return { - __esModule: true, - ...originalModule, - RealmContext: { - ...originalModule.RealmContext, - useRealm: ( ) => global.mockRealms[mockRealmIdentifier], - useQuery: ( ) => [], - }, - }; -} ); -beforeAll( uniqueRealmBeforeAll ); -afterAll( uniqueRealmAfterAll ); -// /UNIQUE REALM SETUP - -jest.mock( "react-native/Libraries/AppState/AppState", () => ( { - __esModule: true, - default: { - addEventListener: jest.fn( ( event, callback ) => { - callback( "active" ); - } ), - }, -} ) ); - -const mockObservations = [ - factory( "LocalObservation", { comments_viewed: false, identifications_viewed: false } ), - factory( "LocalObservation", { comments_viewed: true, identifications_viewed: false } ), - factory( "LocalObservation", { comments_viewed: false, identifications_viewed: true } ), - factory( "LocalObservation", { comments_viewed: true, identifications_viewed: true } ), -]; - -describe( "useObservationUpdatesWhenFocused", () => { - beforeAll( ( ) => { - // Write mock observations to realm - safeRealmWrite( global.mockRealms[mockRealmIdentifier], ( ) => { - mockObservations.forEach( o => { - global.mockRealms[mockRealmIdentifier].create( "Observation", o ); - } ); - }, "write observations to realm, useObservationUpdatesWhenFocused test" ); - } ); - - it( "should reset state of all observations in realm", () => { - renderHook( () => useObservationUpdatesWhenFocused() ); - const observations = global.mockRealms[mockRealmIdentifier].objects( "Observation" ); - observations.forEach( o => { - expect( o.comments_viewed ).toBe( true ); - expect( o.identifications_viewed ).toBe( true ); - } ); - } ); -} );