Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
._*

node_modules/
.expo/
dist/
Expand Down Expand Up @@ -30,4 +32,4 @@ Gemfile
# The following patterns were generated by expo-cli

expo-env.d.ts
# @end expo-cli
# @end expo-cli
86 changes: 85 additions & 1 deletion src/app/(app)/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ jest.mock('expo-router', () => ({
replace: jest.fn(),
back: jest.fn(),
}),
useFocusEffect: (callback: () => void) => {
// Call the callback immediately for testing
callback();
},
}));
jest.mock('@react-navigation/native', () => ({
useIsFocused: () => true,
Expand Down Expand Up @@ -282,4 +286,84 @@ describe('Map Component - App Lifecycle', () => {
expect(mockLocationService.startLocationUpdates).toHaveBeenCalled();
});
});
});

it('should reset map state when navigating back to map page', async () => {
mockUseAppLifecycle.mockReturnValue({
isActive: true,
appState: 'active',
isBackground: false,
lastActiveTimestamp: Date.now(),
});

// Mock unlocked map with location
mockUseLocationStore.mockReturnValue({
latitude: 40.7128,
longitude: -74.0060,
heading: 0,
isMapLocked: false,
});

render(<Map />);

await waitFor(() => {
// Verify that location service was called
expect(mockLocationService.startLocationUpdates).toHaveBeenCalled();
});

// The useFocusEffect should trigger and reset the map state
// This is verified by the component rendering without errors
// and the camera being reset to default position
});

it('should reset camera to default position when navigating back with unlocked map', async () => {
mockUseAppLifecycle.mockReturnValue({
isActive: true,
appState: 'active',
isBackground: false,
lastActiveTimestamp: Date.now(),
});

// Mock unlocked map with location
mockUseLocationStore.mockReturnValue({
latitude: 40.7128,
longitude: -74.0060,
heading: 90, // With heading
isMapLocked: false, // Unlocked
});

render(<Map />);

await waitFor(() => {
expect(mockLocationService.startLocationUpdates).toHaveBeenCalled();
});

// When navigating back to map with unlocked state,
// the camera should be reset to default position (zoom: 12, heading: 0, pitch: 0)
});

it('should not reset camera when navigating back with locked map', async () => {
mockUseAppLifecycle.mockReturnValue({
isActive: true,
appState: 'active',
isBackground: false,
lastActiveTimestamp: Date.now(),
});

// Mock locked map with location
mockUseLocationStore.mockReturnValue({
latitude: 40.7128,
longitude: -74.0060,
heading: 90,
isMapLocked: true, // Locked
});

render(<Map />);

await waitFor(() => {
expect(mockLocationService.startLocationUpdates).toHaveBeenCalled();
});

// When map is locked, navigation focus should not reset camera position
// It should maintain navigation mode
});
});
65 changes: 58 additions & 7 deletions src/app/(app)/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Mapbox from '@rnmapbox/maps';
import { Stack } from 'expo-router';
import { Stack, useFocusEffect } from 'expo-router';
import { NavigationIcon } from 'lucide-react-native';
import React, { useEffect, useRef, useState } from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Animated, StyleSheet, TouchableOpacity, View } from 'react-native';

Expand Down Expand Up @@ -52,6 +52,37 @@ export default function Map() {
const pulseAnim = useRef(new Animated.Value(1)).current;
useMapSignalRUpdates(setMapPins);

// Handle navigation focus - reset map state when user navigates back to map page
useFocusEffect(
useCallback(() => {
// Reset hasUserMovedMap when navigating back to map
setHasUserMovedMap(false);

// If map is not locked and we have a location, reset camera to default position
if (!location.isMapLocked && location.latitude && location.longitude) {
cameraRef.current?.setCamera({
centerCoordinate: [location.longitude, location.latitude],
zoomLevel: 12,
heading: 0,
pitch: 0,
animationDuration: 1000,
});

logger.info({
message: 'Navigated back to map with unlocked map, resetting camera to default position',
context: {
latitude: location.latitude,
longitude: location.longitude,
},
});
} else {
logger.info({
message: 'Navigated back to map, resetting map user interaction state',
});
}
}, [location.isMapLocked, location.latitude, location.longitude])
);

useEffect(() => {
const startLocationTracking = async () => {
try {
Expand Down Expand Up @@ -131,15 +162,35 @@ export default function Map() {
}
}, [location.isMapLocked, location.latitude, location.longitude]);

// Reset hasUserMovedMap when app becomes active (startup/foreground)
// Reset hasUserMovedMap when app becomes active (startup/foreground) or when navigating back to map
useEffect(() => {
if (isActive) {
setHasUserMovedMap(false);
logger.info({
message: 'App became active, resetting map user interaction state',
});

// If map is not locked and we have a location, reset camera to default position
if (!location.isMapLocked && location.latitude && location.longitude) {
cameraRef.current?.setCamera({
centerCoordinate: [location.longitude, location.latitude],
zoomLevel: 12,
heading: 0,
pitch: 0,
animationDuration: 1000,
});

logger.info({
message: 'App became active with unlocked map, resetting camera to default position',
context: {
latitude: location.latitude,
longitude: location.longitude,
},
});
} else {
logger.info({
message: 'App became active, resetting map user interaction state',
});
}
}
}, [isActive]);
}, [isActive, location.isMapLocked, location.latitude, location.longitude]);

useEffect(() => {
const fetchMapDataAndMarkers = async () => {
Expand Down
Loading