diff --git a/app/src/main/java/univ/earthbreaker/namu/feature/home/HomeRoute.kt b/app/src/main/java/univ/earthbreaker/namu/feature/home/HomeRoute.kt index f8d17a0..b2f891c 100644 --- a/app/src/main/java/univ/earthbreaker/namu/feature/home/HomeRoute.kt +++ b/app/src/main/java/univ/earthbreaker/namu/feature/home/HomeRoute.kt @@ -1,8 +1,10 @@ package univ.earthbreaker.namu.feature.home +import androidx.activity.compose.BackHandler import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.remember +import androidx.compose.ui.platform.LocalContext import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.viewmodel.compose.viewModel import univ.earthbreaker.namu.feature.home.model.HomeActions @@ -10,6 +12,8 @@ import univ.earthbreaker.namu.feature.home.model.HomeUiState import univ.earthbreaker.namu.feature.loading.LoadingActions import univ.earthbreaker.namu.feature.loading.LoadingScreen import univ.earthbreaker.namu.feature.loading.LoadingViewModel +import univ.earthbreaker.namu.util.extension.finishSafely +import univ.earthbreaker.namu.util.extension.getActivity @Composable fun HomeRoute( @@ -21,6 +25,10 @@ fun HomeRoute( characterNo: Int, ) -> Unit, ) { + // handle onBackPressed + HomeBackHandler() + + // declare ViewModels val homeViewModel: HomeViewModel = viewModel() val loadingViewModel: LoadingViewModel = viewModel() @@ -50,6 +58,14 @@ fun HomeRoute( } } +@Composable +fun HomeBackHandler() { + val context = LocalContext.current + BackHandler { + context.getActivity()?.finishSafely() + } +} + @Composable fun rememberHomeActions( viewModel: HomeViewModel,