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
19 changes: 9 additions & 10 deletions src/hooks/useRunModals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,17 @@ export function useRunModals({
setModal('back');
}, [setModal]);

const handleConfirmBack = useCallback(() => {
// 공통 정리 및 뒤로가기 로직
const cleanupAndGoBack = useCallback(() => {
resetLocationTracking();
resetRunState();
Comment on lines 59 to 60

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

resetLocationTracking()resetRunState()를 연달아 호출하고 있습니다. run.store.ts를 보면 resetRunStateinitialLocationTrackingState를 포함한 전체 상태를 초기화하고, resetLocationTracking은 위치 추적 관련 상태를 초기화하고 구독을 해제합니다. 이로 인해 일부 상태(e.g., routeCoordinates, isTracking)가 중복으로 초기화되고 있습니다.

resetLocationTracking은 구독 해제만 담당하도록 하고, 상태 초기화는 resetRunState에서만 담당하도록 역할을 분리하면 코드가 더 명확해지고 중복을 제거할 수 있을 것 같습니다. 이는 run.store.ts 파일의 수정이 필요할 수 있는 제안입니다.

// courseId 파라미터 초기화
navigation.setParams({ courseId: undefined });
navigation.goBack();
}, [resetLocationTracking, resetRunState, navigation]);

const handleConfirmBack = cleanupAndGoBack;

const handleCancelBack = useCallback(() => {
setModal(null);
}, [setModal]);
Expand All @@ -71,9 +76,7 @@ export function useRunModals({

const handleConfirmExit = useCallback(async () => {
if (!startTime || routeCoordinates.length === 0) {
resetLocationTracking();
resetRunState();
navigation.goBack();
cleanupAndGoBack();
return;
}

Expand Down Expand Up @@ -135,9 +138,7 @@ export function useRunModals({
text2: '런닝 기록이 성공적으로 저장되었습니다.',
});

resetLocationTracking();
resetRunState();
navigation.goBack();
cleanupAndGoBack();
} catch (error: unknown) {
let errorMessage = '런닝 기록 저장에 실패했습니다.';

Expand Down Expand Up @@ -171,14 +172,12 @@ export function useRunModals({
stats,
setUI,
setError,
resetLocationTracking,
resetRunState,
saveRunningRecord,
navigation,
accessToken,
captureMap,
uploadImage,
courseId,
cleanupAndGoBack,
]);

const handleRetryExit = useCallback(() => {
Expand Down
47 changes: 28 additions & 19 deletions src/pages/Run/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useRef, useCallback } from 'react';
import { Text } from 'react-native';
import { Text, BackHandler } from 'react-native';
import styled from '@emotion/native';
import { ArrowLeft, LocateFixed } from 'lucide-react-native';
import { useFocusEffect } from '@react-navigation/native';
Expand Down Expand Up @@ -28,14 +28,6 @@ export default function Run({ route, navigation }: Props) {
useLocationTracking();
const { location, errorMsg, flyToCurrentUserLocation } = useLocationManager();
const cameraRef = useRef<Mapbox.Camera>(null!);

const locationPosition = location
? ([location.coords.longitude, location.coords.latitude] as [
number,
number,
])
: null;

const mapRef = useRef<Mapbox.MapView>(null);

const {
Expand All @@ -50,16 +42,6 @@ export default function Run({ route, navigation }: Props) {
useRunStats(routeCoordinates, isTracking);
const { loadCourseTopology } = useCourseTopologyApi(courseId);

useFocusEffect(
useCallback(() => {
resetLocationTracking();
resetRunState();
if (courseId) {
loadCourseTopology();
}
}, [resetLocationTracking, resetRunState, courseId, loadCourseTopology]),
);

const {
showExitModal,
showBackModal,
Expand All @@ -71,6 +53,33 @@ export default function Run({ route, navigation }: Props) {
handleConfirmExit,
} = useRunModals({ navigation, mapRef, cameraRef, courseId });

useFocusEffect(
useCallback(() => {
resetLocationTracking();
resetRunState();
Comment on lines +58 to +59

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

useRunModals.ts에서와 동일하게, 여기서도 resetLocationTracking()resetRunState()가 함께 호출되어 상태 초기화 로직이 중복 실행될 수 있습니다. run.store.ts의 관련 액션들을 리팩토링하여 역할을 명확히 분리하는 것을 고려해보시면 좋을 것 같습니다. 예를 들어, resetLocationTracking은 구독 해제와 같은 "정리(cleanup)" 작업만 수행하고, 모든 상태 초기화는 resetRunState가 담당하도록 하는 것입니다.

if (courseId) {
loadCourseTopology();
}
}, [resetLocationTracking, resetRunState, courseId, loadCourseTopology]),
);

// 하드웨어 뒤로가기 버튼 제어
useFocusEffect(
useCallback(() => {
const handleHardwareBackPress = () => {
handleBackPress();
return true; // 기본 뒤로가기 동작을 막음
};

const subscription = BackHandler.addEventListener(
'hardwareBackPress',
handleHardwareBackPress,
);

return () => subscription.remove();
}, [handleBackPress]),
);

const handleCurrentLocationPress = () => {
flyToCurrentUserLocation(cameraRef);
};
Expand Down