Skip to content

Commit

Permalink
#86 fix : 뒤로가기 커스텀
Browse files Browse the repository at this point in the history
Co-authored-by: plashdof <[email protected]>
Co-authored-by: BENDENG1 <[email protected]>
  • Loading branch information
3 people committed Dec 11, 2023
1 parent 17510bd commit bdfa7c8
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ abstract class BaseFragment<B : ViewDataBinding>(
}

fun showToastMessage(message: String) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show()
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
}

fun showTwoButtonTitleDialog(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.avengers.nibobnebob.presentation.ui

import android.app.Activity
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.FragmentActivity
import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import com.avengers.nibobnebob.NavGraphDirections


Expand Down Expand Up @@ -31,4 +35,12 @@ internal fun NavController.toMyPage() {
internal fun NavController.toUserDetail(nickName: String) {
val action = NavGraphDirections.globalToUserDetailFragment(nickName)
navigate(action)
}

internal fun customBack(activity : FragmentActivity, nav : NavController){
activity.onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
nav.navigateUp()
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.provider.MediaStore
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.core.app.ActivityCompat
Expand Down Expand Up @@ -52,6 +53,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::infl
viewModel.events.collect{
when(it){
is MainEvents.OpenGallery -> onCheckStoragePermissions()
is MainEvents.FinishApp -> finish()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import javax.inject.Inject

sealed class MainEvents{
data object OpenGallery: MainEvents()
data object FinishApp : MainEvents()
}

@HiltViewModel
Expand All @@ -40,6 +41,12 @@ class MainViewModel @Inject constructor(
}
}

fun finishApp() {
viewModelScope.launch {
_events.emit(MainEvents.FinishApp)
}
}

fun setUriString(uri : String){
_image.value = uri
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.avengers.nibobnebob.presentation.ui.main.follow

import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.NavController
Expand All @@ -24,6 +25,7 @@ class FollowFragment : BaseFragment<FragmentFollowBinding>(R.layout.fragment_fol
binding.rvFollowList.adapter = FollowAdapter()
binding.rvRecommendFriend.adapter = FollowAdapter()
setTabSelectedListener()
finishApp()
}

override fun initNetworkView() {
Expand Down Expand Up @@ -58,6 +60,20 @@ class FollowFragment : BaseFragment<FragmentFollowBinding>(R.layout.fragment_fol
})
}

private fun finishApp(){
var backPressTime = 0L
requireActivity().onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if(System.currentTimeMillis() - backPressTime <= 2000) {
parentViewModel.finishApp()
} else{
backPressTime = System.currentTimeMillis()
showToastMessage("뒤로가기 버튼을 한 번 더 누르면 종료됩니다.")
}
}
})
}

private fun NavController.toFollowSearch() {
val action = FollowFragmentDirections.actionFollowFragmentToFollowSearchFragment()
navigate(action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.avengers.nibobnebob.databinding.FragmentFollowSearchBinding
import com.avengers.nibobnebob.presentation.base.BaseFragment
import com.avengers.nibobnebob.presentation.customview.SelectRegionDialog
import com.avengers.nibobnebob.presentation.ui.adjustKeyboard
import com.avengers.nibobnebob.presentation.ui.customBack
import com.avengers.nibobnebob.presentation.ui.main.MainViewModel
import com.avengers.nibobnebob.presentation.ui.main.follow.adapter.FollowSearchAdapter
import com.avengers.nibobnebob.presentation.ui.toUserDetail
Expand All @@ -25,6 +26,7 @@ class FollowSearchFragment :
binding.rvFollowSearch.itemAnimator = null
setEditText()
viewModel.observeKeyword()
customBack(requireActivity(), findNavController())
}

override fun initNetworkView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.navigation.fragment.navArgs
import com.avengers.nibobnebob.R
import com.avengers.nibobnebob.databinding.FragmentAddMyRestaurantBinding
import com.avengers.nibobnebob.presentation.base.BaseFragment
import com.avengers.nibobnebob.presentation.ui.customBack
import com.avengers.nibobnebob.presentation.ui.main.MainViewModel
import com.avengers.nibobnebob.presentation.ui.toHome
import com.avengers.nibobnebob.presentation.ui.toMultiPart
Expand All @@ -29,6 +30,7 @@ class AddMyRestaurantFragment :
setSliderListener()
setVisitMethodRadioListener()
initImageObserver()
customBack(requireActivity(), findNavController())
}

override fun initNetworkView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.avengers.nibobnebob.R
import com.avengers.nibobnebob.databinding.FragmentRestaurantDetailBinding
import com.avengers.nibobnebob.presentation.base.BaseFragment
import com.avengers.nibobnebob.presentation.customview.TwoButtonTitleDialog
import com.avengers.nibobnebob.presentation.ui.customBack
import com.avengers.nibobnebob.presentation.ui.main.MainViewModel
import com.avengers.nibobnebob.presentation.ui.main.global.restaurantdetail.adapter.RestaurantReviewAdapter
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -27,6 +28,7 @@ class RestaurantDetailFragment :
binding.vm = viewModel
viewModel.setRestaurantId(restaurantId)
binding.rvReview.adapter = RestaurantReviewAdapter()
customBack(requireActivity(), findNavController())
}

override fun initNetworkView() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.avengers.nibobnebob.presentation.ui.main.global.restaurantreview

import android.util.Log
import androidx.appcompat.widget.PopupMenu
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
Expand All @@ -10,6 +9,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.avengers.nibobnebob.R
import com.avengers.nibobnebob.databinding.FragmentRestaurantReviewsBinding
import com.avengers.nibobnebob.presentation.base.BaseFragment
import com.avengers.nibobnebob.presentation.ui.customBack
import com.avengers.nibobnebob.presentation.ui.main.MainViewModel
import com.avengers.nibobnebob.presentation.ui.main.global.restaurantdetail.adapter.RestaurantReviewAdapter
import com.avengers.nibobnebob.presentation.util.Constants
Expand Down Expand Up @@ -43,11 +43,11 @@ class RestaurantReviewsFragment :
val isNotLoading = !viewModel.uiState.value.isLoading

if (scrollBottom && hasNextPage && isNotLoading) {
Log.d("TEST", "end")
viewModel.loadNextPage()
}
}
})
customBack(requireActivity(), findNavController())

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.avengers.nibobnebob.R
import com.avengers.nibobnebob.databinding.FragmentUserDetailBinding
import com.avengers.nibobnebob.presentation.base.BaseFragment
import com.avengers.nibobnebob.presentation.customview.ImageDialog
import com.avengers.nibobnebob.presentation.ui.customBack
import com.avengers.nibobnebob.presentation.ui.main.MainViewModel
import com.avengers.nibobnebob.presentation.ui.main.global.userdetail.adapter.UserDetailRestaurantAdapter
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -26,6 +27,7 @@ class UserDetailFragment : BaseFragment<FragmentUserDetailBinding>(R.layout.frag
binding.vm = viewModel
viewModel.setNick(nickName)
binding.rvRestaurant.adapter = UserDetailRestaurantAdapter()
customBack(requireActivity(), findNavController())
}

@RequiresApi(Build.VERSION_CODES.O)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.avengers.nibobnebob.presentation.ui.main.home

import android.Manifest
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.avengers.nibobnebob.R
import com.avengers.nibobnebob.app.App
import com.avengers.nibobnebob.databinding.FragmentHomeBinding
import com.avengers.nibobnebob.presentation.base.BaseFragment
import com.avengers.nibobnebob.presentation.customview.RecommendRestaurantDialog
Expand Down Expand Up @@ -61,6 +64,7 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home),
initMapView()
binding.rvHomeFilter.adapter = HomeFilterAdapter()
viewModel.setAddRestaurantId(restaurantId)
finishApp()
}

override fun initNetworkView() {
Expand Down Expand Up @@ -258,6 +262,20 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home),
markerList.clear()
}

private fun finishApp(){
var backPressTime = 0L
requireActivity().onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if(System.currentTimeMillis() - backPressTime <= 2000) {
parentViewModel.finishApp()
} else{
backPressTime = System.currentTimeMillis()
showToastMessage("뒤로가기 버튼을 한 번 더 누르면 종료됩니다.")
}
}
})
}


private suspend fun addWishTest(id: Int, curState: Boolean): Boolean {
return lifecycleScope.async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.avengers.nibobnebob.R
import com.avengers.nibobnebob.databinding.FragmentRestaurantSearchBinding
import com.avengers.nibobnebob.presentation.base.BaseFragment
import com.avengers.nibobnebob.presentation.ui.adjustKeyboard
import com.avengers.nibobnebob.presentation.ui.customBack
import com.avengers.nibobnebob.presentation.ui.main.MainActivity
import com.avengers.nibobnebob.presentation.ui.main.MainViewModel
import com.avengers.nibobnebob.presentation.ui.main.home.adapter.HomeSearchAdapter
Expand All @@ -43,6 +44,7 @@ class RestaurantSearchFragment :
fetchCurrentLocation()
view?.let { clearFocus(it) }
initStateObserver()
customBack(requireActivity(), findNavController())
}

override fun initNetworkView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.avengers.nibobnebob.R
import com.avengers.nibobnebob.databinding.FragmentRestaurantSearchMapBinding
import com.avengers.nibobnebob.presentation.base.BaseFragment
import com.avengers.nibobnebob.presentation.customview.RestaurantBottomSheet
import com.avengers.nibobnebob.presentation.ui.customBack
import com.avengers.nibobnebob.presentation.ui.main.MainViewModel
import com.avengers.nibobnebob.presentation.ui.main.home.model.UiRestaurantData
import com.avengers.nibobnebob.presentation.ui.toAddRestaurant
Expand Down Expand Up @@ -61,6 +62,7 @@ class RestaurantSearchMapFragment :
override fun initView() {
initMapView()
initClickEvent()
customBack(requireActivity(), findNavController())
}

override fun initNetworkView() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.avengers.nibobnebob.presentation.ui.main.mypage
import android.app.AlertDialog
import android.content.Intent
import android.os.Build
import androidx.activity.OnBackPressedCallback
import androidx.annotation.RequiresApi
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
Expand All @@ -27,7 +28,7 @@ class MyPageFragment : BaseFragment<FragmentMyPageBinding>(R.layout.fragment_my_
override fun initView() {
binding.svm = sharedViewModel
binding.vm = viewModel

finishApp()


binding.tvWithdraw.setOnClickListener {
Expand Down Expand Up @@ -91,6 +92,20 @@ class MyPageFragment : BaseFragment<FragmentMyPageBinding>(R.layout.fragment_my_

}

private fun finishApp(){
var backPressTime = 0L
requireActivity().onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if(System.currentTimeMillis() - backPressTime <= 2000) {
parentViewModel.finishApp()
} else{
backPressTime = System.currentTimeMillis()
showToastMessage("뒤로가기 버튼을 한 번 더 누르면 종료됩니다.")
}
}
})
}


private fun NavController.toEditProfile() {
val action = MyPageFragmentDirections.globalToEditProfileFragment()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.avengers.nibobnebob.presentation.ui.main.mypage.edit

import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.fragment.findNavController
import com.avengers.nibobnebob.R
import com.avengers.nibobnebob.databinding.FragmentEditProfileBinding
import com.avengers.nibobnebob.presentation.base.BaseFragment
import com.avengers.nibobnebob.presentation.customview.CalendarDatePicker
import com.avengers.nibobnebob.presentation.ui.customBack
import com.avengers.nibobnebob.presentation.ui.main.MainViewModel
import com.avengers.nibobnebob.presentation.ui.main.mypage.share.MyPageSharedUiEvent
import com.avengers.nibobnebob.presentation.ui.main.mypage.share.MyPageSharedViewModel
Expand All @@ -29,6 +32,7 @@ class EditProfileFragment :
setDateBtnListener()
initImageObserver()
setGenderRadioListener()
customBack(requireActivity(), findNavController())
}

override fun initNetworkView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.avengers.nibobnebob.R
import com.avengers.nibobnebob.databinding.FragmentMyRestaurantListBinding
import com.avengers.nibobnebob.presentation.base.BaseFragment
import com.avengers.nibobnebob.presentation.ui.customBack
import com.avengers.nibobnebob.presentation.ui.main.MainViewModel
import com.avengers.nibobnebob.presentation.ui.main.mypage.share.MyPageSharedUiEvent
import com.avengers.nibobnebob.presentation.ui.main.mypage.share.MyPageSharedViewModel
Expand Down Expand Up @@ -45,6 +46,7 @@ class MyRestaurantListFragment :
}
})
setFilterMenu()
customBack(requireActivity(), findNavController())
}

override fun initNetworkView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.avengers.nibobnebob.R
import com.avengers.nibobnebob.databinding.FragmentWishRestaurantListBinding
import com.avengers.nibobnebob.presentation.base.BaseFragment
import com.avengers.nibobnebob.presentation.ui.customBack
import com.avengers.nibobnebob.presentation.ui.main.MainViewModel
import com.avengers.nibobnebob.presentation.ui.main.mypage.share.MyPageSharedUiEvent
import com.avengers.nibobnebob.presentation.ui.main.mypage.share.MyPageSharedViewModel
Expand Down Expand Up @@ -47,6 +48,7 @@ class WishRestaurantListFragment :
}
})
setFilterMenu()
customBack(requireActivity(), findNavController())
}

override fun initNetworkView() {
Expand Down

0 comments on commit bdfa7c8

Please sign in to comment.