From 062517806165d98c9e8fbf1f53ce48076679e11d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AA=85=EC=84=9D?= Date: Thu, 2 Mar 2023 05:42:10 +0900 Subject: [PATCH] =?UTF-8?q?Issues=20#287=20feat:=20startDestination=20?= =?UTF-8?q?=EA=B0=80=20=EC=95=84=EB=8B=8C=20=EA=B2=BD=EC=9A=B0=20backPress?= =?UTF-8?q?=20=EC=8B=9C=20MainContainerFragment=20=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../features/main/ui/MainContainerFragment.kt | 27 +++++++++++++++++++ .../features/setting/ui/SettingFragment.kt | 27 ------------------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/features/ui-main/src/main/java/com/lighthouse/features/main/ui/MainContainerFragment.kt b/features/ui-main/src/main/java/com/lighthouse/features/main/ui/MainContainerFragment.kt index bf981f5eb..0ade2c6e4 100644 --- a/features/ui-main/src/main/java/com/lighthouse/features/main/ui/MainContainerFragment.kt +++ b/features/ui-main/src/main/java/com/lighthouse/features/main/ui/MainContainerFragment.kt @@ -1,11 +1,14 @@ package com.lighthouse.features.main.ui +import android.content.Context import android.os.Bundle import android.view.View +import androidx.activity.OnBackPressedCallback import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels import androidx.navigation.NavController +import androidx.navigation.NavGraph.Companion.findStartDestination import androidx.navigation.fragment.NavHostFragment import androidx.navigation.ui.setupWithNavController import com.lighthouse.features.common.binding.viewBindings @@ -29,6 +32,23 @@ class MainContainerFragment : Fragment(R.layout.fragment_main_container) { private val mainNavigationViewModel: MainNavigationViewModel by viewModels() private lateinit var navController: NavController + private val destinationChangedListener = + NavController.OnDestinationChangedListener { controller, destination, _ -> + backPressedCallback.isEnabled = + controller.graph.findStartDestination().id != destination.id + } + + private val backPressedCallback = object : OnBackPressedCallback(false) { + override fun handleOnBackPressed() { + navController.popBackStack() + } + } + + override fun onAttach(context: Context) { + super.onAttach(context) + + requireActivity().onBackPressedDispatcher.addCallback(this, backPressedCallback) + } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) @@ -42,6 +62,7 @@ class MainContainerFragment : Fragment(R.layout.fragment_main_container) { val navHostFragment = childFragmentManager.findFragmentById(R.id.fcv_main) as NavHostFragment navController = navHostFragment.navController + navController.addOnDestinationChangedListener(destinationChangedListener) } private fun setUpBottomNavigation() { @@ -58,4 +79,10 @@ class MainContainerFragment : Fragment(R.layout.fragment_main_container) { } } } + + override fun onDestroyView() { + navController.removeOnDestinationChangedListener(destinationChangedListener) + + super.onDestroyView() + } } diff --git a/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt b/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt index 5dd1a1e82..8542194f4 100644 --- a/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt +++ b/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt @@ -1,16 +1,13 @@ package com.lighthouse.features.setting.ui -import android.content.Context import android.os.Bundle import android.view.View -import androidx.activity.addCallback import androidx.activity.result.contract.ActivityResultContracts import androidx.annotation.StringRes import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels import androidx.lifecycle.lifecycleScope -import androidx.navigation.fragment.NavHostFragment import com.google.android.material.snackbar.Snackbar import com.lighthouse.auth.google.model.GoogleAuthEvent import com.lighthouse.auth.google.repository.GoogleClient @@ -27,8 +24,6 @@ import com.lighthouse.features.setting.databinding.FragmentSettingBinding import com.lighthouse.features.setting.model.SettingMenu import com.lighthouse.navs.app.model.AppNavigationItem import com.lighthouse.navs.app.navigator.AppNavigationViewModel -import com.lighthouse.navs.main.model.MainNavigationItem -import com.lighthouse.navs.main.navigator.MainNavigationViewModel import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.launch import javax.inject.Inject @@ -42,16 +37,6 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { private val appNavigationViewModel: AppNavigationViewModel by activityViewModels() - private val mainNavigationViewModel: MainNavigationViewModel by viewModels( - ownerProducer = { - var parent = requireParentFragment() - while (parent is NavHostFragment) { - parent = parent.requireParentFragment() - } - parent - } - ) - private val settingAdapter = SettingAdapter( onClick = { menu -> setUpMenuOnClick(menu) @@ -102,18 +87,6 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { LocationPermissionManager(requireActivity()) } - override fun onAttach(context: Context) { - super.onAttach(context) - - setUpOnBackPressed() - } - - private fun setUpOnBackPressed() { - requireActivity().onBackPressedDispatcher.addCallback(this) { - mainNavigationViewModel.navigate(MainNavigationItem.Popup) - } - } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState)