diff --git a/formulus/src/database/repositories/WatermelonDBRepo.ts b/formulus/src/database/repositories/WatermelonDBRepo.ts index 366bbb61b..3c42a487c 100644 --- a/formulus/src/database/repositories/WatermelonDBRepo.ts +++ b/formulus/src/database/repositories/WatermelonDBRepo.ts @@ -477,14 +477,16 @@ export class WatermelonDBRepo implements LocalRepoInterface { } /** - * Get pending changes from the local database - * @returns Promise resolving to an array of pending changes + * Get pending changes from the local database (unsynced or updated after last sync). + * Includes new, updated, and soft-deleted records so sync can push them to the server. + * synced_at can be null or 0 when never synced depending on storage. */ getPendingChanges(): Promise { return this.observationsCollection .query( Q.or( Q.where('synced_at', Q.eq(null)), + Q.where('synced_at', 0), Q.where('updated_at', Q.gt(Q.column('synced_at'))), ), ) diff --git a/formulus/src/screens/SyncScreen.tsx b/formulus/src/screens/SyncScreen.tsx index fe2ee9425..460e102a4 100644 --- a/formulus/src/screens/SyncScreen.tsx +++ b/formulus/src/screens/SyncScreen.tsx @@ -11,6 +11,7 @@ import { ActivityIndicator, } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; +import { useFocusEffect } from '@react-navigation/native'; import Icon from '@react-native-vector-icons/material-design-icons'; import AsyncStorage from '@react-native-async-storage/async-storage'; import { formatRelativeTime } from '../utils/dateUtils'; @@ -314,6 +315,14 @@ const SyncScreen = () => { updateProgress, ]); + // Refresh pending count whenever the Sync screen gains focus so it stays in sync after creating observations + useFocusEffect( + useCallback(() => { + updatePendingUploads(); + updatePendingObservations(); + }, [updatePendingUploads, updatePendingObservations]), + ); + useEffect(() => { if (!syncState.progress || !syncState.isActive) { Animated.timing(animatedProgress, {