diff --git a/app/src/main/java/org/go/sopt/winey/presentation/main/mypage/MyPageFragment.kt b/app/src/main/java/org/go/sopt/winey/presentation/main/mypage/MyPageFragment.kt index 8dcc4fdb..db0ee8aa 100644 --- a/app/src/main/java/org/go/sopt/winey/presentation/main/mypage/MyPageFragment.kt +++ b/app/src/main/java/org/go/sopt/winey/presentation/main/mypage/MyPageFragment.kt @@ -1,18 +1,15 @@ package org.go.sopt.winey.presentation.main.mypage -import android.Manifest import android.content.Intent -import android.content.pm.PackageManager -import android.os.Build import android.os.Bundle import android.view.View import androidx.activity.OnBackPressedCallback -import androidx.core.content.ContextCompat import androidx.fragment.app.activityViewModels import androidx.lifecycle.flowWithLifecycle import androidx.lifecycle.lifecycleScope import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch @@ -49,17 +46,32 @@ class MyPageFragment : BindingFragment(R.layout.fragment_ super.onViewCreated(view, savedInstanceState) amplitudeUtils.logEvent("view_mypage") - initCheckNotificationPermission() initUserData() - initNavigation() addListener() addObserver() } + // 닉네임 액티비티 갔다가 다시 돌아왔을 때 유저 데이터 갱신하도록 + override fun onStart() { + super.onStart() + mainViewModel.getUser() + } + + private fun initUserData() { + viewLifeCycleScope.launch { + val userInfo = dataStoreRepository.getUserInfo().firstOrNull() ?: return@launch + updateUserInfo(userInfo) + } + } + + private fun updateUserInfo(data: UserV2) { + binding.data = data + } + private fun addListener() { initEditNicknameButtonClickListener() - initMyFeedButtonClickListener() initSettingButtonClickListener() + initMyFeedButtonClickListener() initGoalPathButtonClickListener() registerBackPressedCallback() } @@ -68,31 +80,18 @@ class MyPageFragment : BindingFragment(R.layout.fragment_ setupGetUserState() } - private fun initCheckNotificationPermission() { - isNotificationPermissionAllowed = - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - ContextCompat.checkSelfPermission( - requireContext(), - Manifest.permission.POST_NOTIFICATIONS - ) == PackageManager.PERMISSION_GRANTED - } else { - true - } - } - - // 닉네임 액티비티 갔다가 다시 돌아왔을 때 유저 데이터 갱신하도록 - override fun onStart() { - super.onStart() - mainViewModel.getUser() - initCheckNotificationPermission() - } - private fun initEditNicknameButtonClickListener() { binding.ivMypageEditNickname.setOnSingleClickListener { navigateToNicknameScreen() } } + private fun initSettingButtonClickListener() { + binding.ivMypageSetting.setOnClickListener { + navigateToSettingScreen() + } + } + private fun initMyFeedButtonClickListener() { binding.btnMypageMyfeed.setOnSingleClickListener { navigateToMyFeedScreen() @@ -101,19 +100,7 @@ class MyPageFragment : BindingFragment(R.layout.fragment_ private fun initGoalPathButtonClickListener() { binding.btnMypageTrip.setOnClickListener { - navigateToGoalPath() - } - } - - private fun navigateToGoalPath() { - Intent(requireContext(), GoalPathActivity::class.java).apply { - startActivity(this) - } - } - - private fun initSettingButtonClickListener() { - binding.ivMypageSetting.setOnClickListener { - navigateToSettingScreen() + navigateToGoalPathScreen() } } @@ -136,26 +123,6 @@ class MyPageFragment : BindingFragment(R.layout.fragment_ requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, callback) } - private fun initUserData() { - viewLifeCycleScope.launch { - val data = dataStoreRepository.getUserInfo().first() - if (data != null) { - updateUserInfo(data) - } - } - } - - private fun initNavigation() { - val receivedBundle = arguments - if (receivedBundle != null) { - val value = receivedBundle.getBoolean(KEY_TO_MYFEED) - if (value) { - navigateToMyFeedScreen() - arguments?.clear() - } - } - } - private fun navigateToNicknameScreen() { Intent(requireContext(), NicknameActivity::class.java).apply { putExtra(KEY_PREV_SCREEN_NAME, MY_PAGE_SCREEN) @@ -175,6 +142,12 @@ class MyPageFragment : BindingFragment(R.layout.fragment_ } } + private fun navigateToGoalPathScreen() { + Intent(requireContext(), GoalPathActivity::class.java).apply { + startActivity(this) + } + } + private fun setupGetUserState() { mainViewModel.getUserState.flowWithLifecycle(lifecycle).onEach { state -> when (state) { @@ -192,14 +165,9 @@ class MyPageFragment : BindingFragment(R.layout.fragment_ }.launchIn(lifecycleScope) } - private fun updateUserInfo(data: UserV2) { - binding.data = data - } - companion object { private const val KEY_PREV_SCREEN_NAME = "PREV_SCREEN_NAME" private const val MY_PAGE_SCREEN = "MyPageFragment" private const val KEY_FROM_NOTI = "fromNoti" - private const val KEY_TO_MYFEED = "toMyFeed" } }