mirror of
https://github.com/inaturalist/iNaturalistReactNative.git
synced 2026-08-02 02:17:30 -04:00
remove hook that sets observations to viewed on foreground
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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;
|
||||
@@ -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 );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
Reference in New Issue
Block a user