From 283b59cb45309bdc6a6d536a1adbcf341a283fda Mon Sep 17 00:00:00 2001 From: Amanda Bullington <35536439+albullington@users.noreply.github.com> Date: Wed, 31 Jul 2024 12:17:25 -0700 Subject: [PATCH] Keep app awake during bulk uploads (#1898) * Keep app awake while uploading; closes #1893 * Only activate keep awake during manual sync, not automatic sync --- .../hooks/useSyncObservations.ts | 3 +++ .../hooks/useUploadObservations.ts | 2 -- src/stores/createSyncObservationsSlice.ts | 26 +++++++------------ src/stores/createUploadObservationsSlice.ts | 18 ++++++++++--- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/components/MyObservations/hooks/useSyncObservations.ts b/src/components/MyObservations/hooks/useSyncObservations.ts index 6b4234e19..779ea5ec0 100644 --- a/src/components/MyObservations/hooks/useSyncObservations.ts +++ b/src/components/MyObservations/hooks/useSyncObservations.ts @@ -1,6 +1,7 @@ import { useNetInfo } from "@react-native-community/netinfo"; +import { deactivateKeepAwake } from "@sayem314/react-native-keep-awake"; import { INatApiError } from "api/error"; import { deleteRemoteObservation } from "api/observations"; import { RealmContext } from "providers/contexts"; @@ -162,6 +163,8 @@ const useSyncObservations = ( currentUserId, uploadObservations ): Object => { // being offline, so we're not checking internet connectivity here if ( loggedIn ) { await uploadObservations( ); + } else { + deactivateKeepAwake( ); } completeSync( ); }, [ diff --git a/src/components/MyObservations/hooks/useUploadObservations.ts b/src/components/MyObservations/hooks/useUploadObservations.ts index b4cf14491..62467c88f 100644 --- a/src/components/MyObservations/hooks/useUploadObservations.ts +++ b/src/components/MyObservations/hooks/useUploadObservations.ts @@ -1,5 +1,4 @@ import { useNavigation } from "@react-navigation/native"; -import { deactivateKeepAwake } from "@sayem314/react-native-keep-awake"; import { RealmContext } from "providers/contexts"; import { useCallback, useEffect @@ -197,7 +196,6 @@ export default useUploadObservations = canUpload => { // fully stop uploads when cancel upload button is tapped if ( uploadStatus === UPLOAD_CANCELLED ) { abortController.abort( ); - deactivateKeepAwake( ); } }, [abortController, uploadStatus] ); diff --git a/src/stores/createSyncObservationsSlice.ts b/src/stores/createSyncObservationsSlice.ts index 55100b5e4..6cbbfaebe 100644 --- a/src/stores/createSyncObservationsSlice.ts +++ b/src/stores/createSyncObservationsSlice.ts @@ -1,4 +1,4 @@ -import { activateKeepAwake, deactivateKeepAwake } from "@sayem314/react-native-keep-awake"; +import { activateKeepAwake } from "@sayem314/react-native-keep-awake"; import { StateCreator } from "zustand"; export const SYNC_PENDING = "sync-pending"; @@ -76,21 +76,15 @@ const createSyncObservationsSlice: StateCreator = set => syncingStatus: BEGIN_MANUAL_SYNC } ); } ), - startAutomaticSync: ( ) => set( ( ) => { - activateKeepAwake( ); - return ( { - syncingStatus: BEGIN_AUTOMATIC_SYNC - } ); - } ), - completeSync: ( ) => set( ( ) => { - deactivateKeepAwake( ); - return ( { - currentDeleteCount: 1, - deleteError: null, - deleteQueue: [], - syncingStatus: SYNC_PENDING - } ); - } ) + startAutomaticSync: ( ) => set( ( ) => ( { + syncingStatus: BEGIN_AUTOMATIC_SYNC + } ) ), + completeSync: ( ) => set( ( ) => ( { + currentDeleteCount: 1, + deleteError: null, + deleteQueue: [], + syncingStatus: SYNC_PENDING + } ) ) } ); export default createSyncObservationsSlice; diff --git a/src/stores/createUploadObservationsSlice.ts b/src/stores/createUploadObservationsSlice.ts index 1ab622b8f..42aed3bac 100644 --- a/src/stores/createUploadObservationsSlice.ts +++ b/src/stores/createUploadObservationsSlice.ts @@ -1,3 +1,4 @@ +import { deactivateKeepAwake } from "@sayem314/react-native-keep-awake"; import _ from "lodash"; import { RealmObservation } from "realmModels/types.d.ts"; import { StateCreator } from "zustand"; @@ -106,10 +107,19 @@ const createUploadObservationsSlice: StateCreator = set }, multiError: error } ) ), - stopAllUploads: ( ) => set( { ...DEFAULT_STATE, uploadStatus: UPLOAD_CANCELLED } ), - completeUploads: ( ) => set( ( ) => ( { - uploadStatus: UPLOAD_COMPLETE - } ) ), + stopAllUploads: ( ) => set( ( ) => { + deactivateKeepAwake( ); + return ( { + ...DEFAULT_STATE, + uploadStatus: UPLOAD_CANCELLED + } ); + } ), + completeUploads: ( ) => set( ( ) => { + deactivateKeepAwake( ); + return ( { + uploadStatus: UPLOAD_COMPLETE + } ); + } ), updateTotalUploadProgress: ( uuid, increment ) => set( state => { const { currentUpload,