mirror of
https://github.com/inaturalist/iNaturalistReactNative.git
synced 2026-05-08 07:36:48 -04:00
* Add a safeRealmWrite transaction for better logging around writes; code cleanup and realm update * Add safeRealmWrite to tests and make sure action is called synchronously * Fix final test * Only write to realm when useObservationsUpdates data changes; code cleanup * Code cleanup
51 lines
1.5 KiB
JavaScript
51 lines
1.5 KiB
JavaScript
// @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;
|