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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.SystemClock
import android.provider.Settings
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.animation.EnterTransition
Expand All @@ -21,6 +23,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
Expand Down Expand Up @@ -111,7 +114,19 @@ fun FoodDiaryNavHost(
val isLoginRoute =
currentDestination?.hierarchy?.any { it.hasRoute(LoginRoute::class) } == true
val shouldShowHomeInsightBottomBar = isHomeRoute || isInsightRoute
val shouldHandleAppExitBack = isHomeRoute || isInsightRoute
val selectedTab = if (isInsightRoute) HomeInsightTab.INSIGHT else HomeInsightTab.HOME
var lastExitBackPressedAt by remember { mutableLongStateOf(0L) }

val handleAppExitBackPress: () -> Unit = {
val now = SystemClock.elapsedRealtime()
if (now - lastExitBackPressedAt <= EXIT_CONFIRMATION_WINDOW_MILLIS) {
onFinish()
} else {
lastExitBackPressedAt = now
onShowToast(context.getString(R.string.exit_confirm_toast_message))
}
}

val navigateToPendingDetailIfNeeded: () -> Unit = {
pendingDetailDate?.let { detailDate ->
Expand Down Expand Up @@ -139,6 +154,12 @@ fun FoodDiaryNavHost(
}
}

LaunchedEffect(shouldHandleAppExitBack) {
if (!shouldHandleAppExitBack) {
lastExitBackPressedAt = 0L
}
}

LaunchedEffect(Unit) {
PushSyncEventBus.analysisCompleteEvents.collect { event ->
val currentEntry = navController.currentBackStackEntry
Expand Down Expand Up @@ -213,6 +234,10 @@ fun FoodDiaryNavHost(
}

Box(modifier = Modifier.fillMaxSize()) {
BackHandler(enabled = isHomeRoute) {
handleAppExitBackPress()
}

Scaffold(
contentWindowInsets = WindowInsets(0, 0, 0, 0),
bottomBar = {
Expand Down Expand Up @@ -310,7 +335,7 @@ fun FoodDiaryNavHost(

insightScreen(
onNavigateToMyPage = { navController.navigate(MyPageRoute) },
onBack = onFinish,
onBack = handleAppExitBackPress,
)

detailScreen(
Expand Down Expand Up @@ -453,3 +478,5 @@ private fun Uri?.getDetailDateOrNull(): String? {
return getQueryParameter(NavigationConstants.DEEP_LINK_QUERY_DATE)
?.takeIf { it.isNotBlank() }
}

private const val EXIT_CONFIRMATION_WINDOW_MILLIS = 2_000L
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
<string name="push_analysis_complete_body">%1$s사진을 AI가 기록을 완료했어요.</string>
<string name="push_analysis_complete_body_no_date">음식 사진 분석이 완료됐어요.</string>
<string name="push_analysis_complete_snackbar">AI가 기록을 완료했습니다.</string>
<string name="exit_confirm_toast_message">\'뒤로\'버튼 한번 더 누르시면 종료됩니다.</string>
</resources>