Skip to content

Commit

Permalink
Issues boostcampwm-2022#287 feat: startDestination 가 아닌 경우 backPress …
Browse files Browse the repository at this point in the history
…시 MainContainerFragment 에서 처리
  • Loading branch information
audxo112 committed Mar 1, 2023
1 parent f945404 commit 0625178
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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() {
Expand All @@ -58,4 +79,10 @@ class MainContainerFragment : Fragment(R.layout.fragment_main_container) {
}
}
}

override fun onDestroyView() {
navController.removeOnDestinationChangedListener(destinationChangedListener)

super.onDestroyView()
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 0625178

Please sign in to comment.