From 0eda9a490b1ad2dc630a1ef870094ebf14169f91 Mon Sep 17 00:00:00 2001 From: Monixc Date: Fri, 12 Sep 2025 14:38:36 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=AA=A8=EB=B0=94=EC=9D=BC=20=EC=9E=90?= =?UTF-8?q?=EC=B2=B4=20=EB=92=A4=EB=A1=9C=EA=B0=80=EA=B8=B0=20=EC=A0=9C?= =?UTF-8?q?=EC=96=B4=20=EB=A1=9C=EC=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - useRunModals 훅에서 뒤로가기 및 종료 시 공통으로 사용되는 로직을 cleanupAndGoBack 함수로 통합 - Run 페이지에서 하드웨어 뒤로가기 버튼 제어 추가 - useFocusEffect를 통해 위치 추적 및 상태 초기화 로직을 유지 --- src/hooks/useRunModals.ts | 21 ++++++----------- src/pages/Run/index.tsx | 47 +++++++++++++++++++++++---------------- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/hooks/useRunModals.ts b/src/hooks/useRunModals.ts index ef89c0b..f27b0b3 100644 --- a/src/hooks/useRunModals.ts +++ b/src/hooks/useRunModals.ts @@ -54,7 +54,8 @@ export function useRunModals({ setModal('back'); }, [setModal]); - const handleConfirmBack = useCallback(() => { + // 공통 정리 및 뒤로가기 로직 + const cleanupAndGoBack = useCallback(() => { resetLocationTracking(); resetRunState(); // courseId 파라미터 초기화 @@ -62,6 +63,8 @@ export function useRunModals({ navigation.goBack(); }, [resetLocationTracking, resetRunState, navigation]); + const handleConfirmBack = cleanupAndGoBack; + const handleCancelBack = useCallback(() => { setModal(null); }, [setModal]); @@ -73,11 +76,7 @@ export function useRunModals({ const handleConfirmExit = useCallback(async () => { if (!startTime || routeCoordinates.length === 0) { - resetLocationTracking(); - resetRunState(); - // courseId 파라미터 초기화 - navigation.setParams({ courseId: undefined }); - navigation.goBack(); + cleanupAndGoBack(); return; } @@ -139,11 +138,7 @@ export function useRunModals({ text2: '런닝 기록이 성공적으로 저장되었습니다.', }); - resetLocationTracking(); - resetRunState(); - // courseId 파라미터 초기화 - navigation.setParams({ courseId: undefined }); - navigation.goBack(); + cleanupAndGoBack(); } catch (error: unknown) { let errorMessage = '런닝 기록 저장에 실패했습니다.'; @@ -177,14 +172,12 @@ export function useRunModals({ stats, setUI, setError, - resetLocationTracking, - resetRunState, saveRunningRecord, - navigation, accessToken, captureMap, uploadImage, courseId, + cleanupAndGoBack, ]); const handleRetryExit = useCallback(() => { diff --git a/src/pages/Run/index.tsx b/src/pages/Run/index.tsx index f924197..28066d9 100644 --- a/src/pages/Run/index.tsx +++ b/src/pages/Run/index.tsx @@ -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'; @@ -28,14 +28,6 @@ export default function Run({ route, navigation }: Props) { useLocationTracking(); const { location, errorMsg, flyToCurrentUserLocation } = useLocationManager(); const cameraRef = useRef(null!); - - const locationPosition = location - ? ([location.coords.longitude, location.coords.latitude] as [ - number, - number, - ]) - : null; - const mapRef = useRef(null); const { @@ -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, @@ -71,6 +53,33 @@ export default function Run({ route, navigation }: Props) { handleConfirmExit, } = useRunModals({ navigation, mapRef, cameraRef, courseId }); + useFocusEffect( + useCallback(() => { + resetLocationTracking(); + 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); };