|
1 | 1 | package com.runnect.runnect.presentation.mypage |
2 | 2 |
|
3 | 3 | import android.app.Activity |
4 | | -import android.app.Activity.RESULT_OK |
5 | 4 | import android.content.Intent |
6 | 5 | import android.os.Bundle |
| 6 | +import android.view.LayoutInflater |
| 7 | +import android.view.View |
| 8 | +import android.view.ViewGroup |
7 | 9 | import androidx.activity.result.ActivityResultLauncher |
8 | 10 | import androidx.activity.result.contract.ActivityResultContracts |
9 | | -import androidx.core.view.isVisible |
| 11 | +import androidx.compose.runtime.collectAsState |
| 12 | +import androidx.compose.runtime.getValue |
| 13 | +import androidx.compose.ui.platform.ComposeView |
| 14 | +import androidx.compose.ui.platform.ViewCompositionStrategy |
| 15 | +import androidx.fragment.app.Fragment |
10 | 16 | import androidx.fragment.app.activityViewModels |
11 | 17 | import androidx.fragment.app.commit |
12 | 18 | import androidx.fragment.app.replace |
13 | | -import coil3.load |
14 | 19 | import com.kakao.sdk.common.util.KakaoCustomTabsClient |
15 | 20 | import com.kakao.sdk.talk.TalkApiClient |
16 | 21 | import com.runnect.runnect.BuildConfig |
17 | 22 | import com.runnect.runnect.R |
18 | | -import com.runnect.runnect.binding.BaseVisitorFragment |
19 | | -import com.runnect.runnect.databinding.FragmentMyPageBinding |
| 23 | +import com.runnect.runnect.presentation.event.VisitorModeManager |
| 24 | +import com.runnect.runnect.presentation.login.LoginActivity |
20 | 25 | import com.runnect.runnect.presentation.mypage.editname.MyPageEditNameActivity |
21 | 26 | import com.runnect.runnect.presentation.mypage.history.MyHistoryActivity |
22 | 27 | import com.runnect.runnect.presentation.mypage.reward.MyRewardActivity |
23 | 28 | import com.runnect.runnect.presentation.mypage.setting.MySettingFragment |
24 | 29 | import com.runnect.runnect.presentation.mypage.upload.MyUploadActivity |
| 30 | +import com.runnect.runnect.presentation.ui.theme.RunnectTheme |
25 | 31 | import com.runnect.runnect.util.analytics.Analytics |
26 | 32 | import com.runnect.runnect.util.analytics.EventName.EVENT_CLICK_GOAL_REWARD |
27 | 33 | import com.runnect.runnect.util.analytics.EventName.EVENT_CLICK_RUNNING_RECORD |
28 | 34 | import com.runnect.runnect.util.analytics.EventName.EVENT_CLICK_UPLOADED_COURSE |
29 | 35 | import com.runnect.runnect.util.extension.getStampResId |
30 | | -import com.runnect.runnect.util.extension.repeatOnStarted |
31 | | -import com.runnect.runnect.util.extension.showSnackbar |
32 | 36 | import dagger.hilt.android.AndroidEntryPoint |
33 | | -import kotlinx.coroutines.flow.collectLatest |
| 37 | +import javax.inject.Inject |
34 | 38 |
|
35 | 39 | @AndroidEntryPoint |
36 | | -class MyPageFragment : BaseVisitorFragment<FragmentMyPageBinding>(R.layout.fragment_my_page) { |
| 40 | +class MyPageFragment : Fragment() { |
| 41 | + @Inject |
| 42 | + lateinit var visitorModeManager: VisitorModeManager |
| 43 | + |
37 | 44 | private val viewModel: MyPageViewModel by activityViewModels() |
38 | 45 | private lateinit var resultEditNameLauncher: ActivityResultLauncher<Intent> |
39 | 46 |
|
40 | | - override val visitorContainer by lazy { binding.clVisitorMode } |
41 | | - override val contentViews by lazy { listOf(binding.constraintInside) } |
42 | | - |
43 | | - override fun onContentModeInit() { |
44 | | - binding.lifecycleOwner = this@MyPageFragment.viewLifecycleOwner |
45 | | - viewModel.intent(MyPageIntent.LoadUserInfo) |
46 | | - addListener() |
47 | | - addObserver() |
| 47 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 48 | + super.onCreate(savedInstanceState) |
48 | 49 | setResultEditNameLauncher() |
49 | 50 | } |
50 | 51 |
|
| 52 | + override fun onCreateView( |
| 53 | + inflater: LayoutInflater, |
| 54 | + container: ViewGroup?, |
| 55 | + savedInstanceState: Bundle? |
| 56 | + ): View { |
| 57 | + return ComposeView(requireContext()).apply { |
| 58 | + setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) |
| 59 | + setContent { |
| 60 | + RunnectTheme { |
| 61 | + if (visitorModeManager.isVisitorMode) { |
| 62 | + VisitorModeScreen( |
| 63 | + onSignUpClick = { navigateToLogin() } |
| 64 | + ) |
| 65 | + } else { |
| 66 | + val state by viewModel.state.collectAsState() |
| 67 | + |
| 68 | + val stampResId = if (!state.isLoading) { |
| 69 | + getStampResourceId(state.stampId) |
| 70 | + } else { |
| 71 | + R.drawable.user_profile_basic |
| 72 | + } |
| 73 | + |
| 74 | + MyPageScreen( |
| 75 | + state = state.copy(profileImgResId = stampResId), |
| 76 | + onEditProfileClick = { navigateToEditName() }, |
| 77 | + onHistoryClick = { |
| 78 | + Analytics.logClickedItemEvent(EVENT_CLICK_RUNNING_RECORD) |
| 79 | + navigateTo<MyHistoryActivity>() |
| 80 | + }, |
| 81 | + onRewardClick = { |
| 82 | + Analytics.logClickedItemEvent(EVENT_CLICK_GOAL_REWARD) |
| 83 | + navigateTo<MyRewardActivity>() |
| 84 | + }, |
| 85 | + onUploadClick = { |
| 86 | + Analytics.logClickedItemEvent(EVENT_CLICK_UPLOADED_COURSE) |
| 87 | + navigateTo<MyUploadActivity>() |
| 88 | + }, |
| 89 | + onSettingClick = { moveToSettingFragment() }, |
| 90 | + onKakaoInquiryClick = { inquiryKakao() } |
| 91 | + ) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| 99 | + super.onViewCreated(view, savedInstanceState) |
| 100 | + if (!visitorModeManager.isVisitorMode) { |
| 101 | + viewModel.intent(MyPageIntent.LoadUserInfo) |
| 102 | + } |
| 103 | + } |
| 104 | + |
51 | 105 | private fun setResultEditNameLauncher() { |
52 | 106 | resultEditNameLauncher = |
53 | 107 | registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> |
54 | | - if (result.resultCode == RESULT_OK) { |
| 108 | + if (result.resultCode == Activity.RESULT_OK) { |
55 | 109 | val name = result.data?.getStringExtra(EXTRA_NICK_NAME) |
56 | 110 | ?: viewModel.currentState.nickname |
57 | 111 | viewModel.intent(MyPageIntent.UpdateNickname(name)) |
58 | 112 | } |
59 | 113 | } |
60 | 114 | } |
61 | 115 |
|
62 | | - private fun addListener() { |
63 | | - with(binding) { |
64 | | - ivMyPageEditFrame.setOnClickListener { |
65 | | - val intent = Intent(requireContext(), MyPageEditNameActivity::class.java) |
66 | | - intent.putExtra(EXTRA_NICK_NAME, viewModel.currentState.nickname) |
67 | | - val stampResId = getStampResourceId() |
68 | | - intent.putExtra(EXTRA_PROFILE, stampResId) |
69 | | - resultEditNameLauncher.launch(intent) |
70 | | - } |
71 | | - |
72 | | - viewMyPageMainRewardFrame.setOnClickListener { |
73 | | - Analytics.logClickedItemEvent(EVENT_CLICK_GOAL_REWARD) |
74 | | - navigateTo<MyRewardActivity>() |
75 | | - } |
76 | | - viewMyPageMainHistoryFrame.setOnClickListener { |
77 | | - Analytics.logClickedItemEvent(EVENT_CLICK_RUNNING_RECORD) |
78 | | - navigateTo<MyHistoryActivity>() |
79 | | - } |
80 | | - |
81 | | - viewMyPageMainUploadFrame.setOnClickListener { |
82 | | - Analytics.logClickedItemEvent(EVENT_CLICK_UPLOADED_COURSE) |
83 | | - navigateTo<MyUploadActivity>() |
84 | | - } |
85 | | - viewMyPageMainSettingFrame.setOnClickListener { |
86 | | - moveToSettingFragment() |
87 | | - } |
88 | | - viewMyPageMainKakaoChannelInquiryFrame.setOnClickListener { |
89 | | - inquiryKakao() |
90 | | - } |
91 | | - } |
| 116 | + private fun navigateToEditName() { |
| 117 | + val intent = Intent(requireContext(), MyPageEditNameActivity::class.java) |
| 118 | + intent.putExtra(EXTRA_NICK_NAME, viewModel.currentState.nickname) |
| 119 | + val stampResId = getStampResourceId(viewModel.currentState.stampId) |
| 120 | + intent.putExtra(EXTRA_PROFILE, stampResId) |
| 121 | + resultEditNameLauncher.launch(intent) |
92 | 122 | } |
93 | 123 |
|
94 | 124 | private fun moveToSettingFragment() { |
95 | 125 | val bundle = Bundle().apply { putString(ACCOUNT_INFO_TAG, viewModel.currentState.email) } |
96 | 126 | requireActivity().supportFragmentManager.commit { |
97 | | - this.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left) |
| 127 | + setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left) |
98 | 128 | replace<MySettingFragment>(R.id.fl_main, args = bundle) |
99 | 129 | } |
100 | 130 | } |
101 | 131 |
|
102 | | - private fun addObserver() { |
103 | | - repeatOnStarted { |
104 | | - viewModel.state.collectLatest { state -> |
105 | | - bindState(state) |
106 | | - } |
107 | | - } |
108 | | - } |
109 | | - |
110 | | - private fun bindState(state: MyPageUiState) { |
111 | | - setLoadingState(state.isLoading) |
112 | | - |
113 | | - if (!state.isLoading && state.error == null) { |
114 | | - with(binding) { |
115 | | - tvMyPageUserName.text = state.nickname |
116 | | - tvMyPageUserLv.text = state.level |
117 | | - pbMyPageProgress.progress = state.levelPercent |
118 | | - tvMyPageProgressCurrent.text = state.levelPercent.toString() |
119 | | - ivMyPageProfile.load(state.profileImgResId) |
120 | | - } |
121 | | - |
122 | | - val stampResId = getStampResourceId() |
123 | | - viewModel.intent(MyPageIntent.UpdateProfileImg(stampResId)) |
124 | | - } |
125 | | - |
126 | | - state.error?.let { |
127 | | - context?.showSnackbar(anchorView = binding.root, message = it) |
128 | | - } |
129 | | - } |
130 | | - |
131 | 132 | private fun inquiryKakao() { |
132 | 133 | val url = TalkApiClient.instance.channelChatUrl(BuildConfig.KAKAO_CHANNEL_ID) |
133 | 134 | KakaoCustomTabsClient.openWithDefault(requireActivity(), url) |
134 | 135 | } |
135 | 136 |
|
136 | | - private fun getStampResourceId(): Int { |
| 137 | + private fun navigateToLogin() { |
| 138 | + startActivity(Intent(requireContext(), LoginActivity::class.java)) |
| 139 | + requireActivity().finish() |
| 140 | + } |
| 141 | + |
| 142 | + private fun getStampResourceId(stampId: String): Int { |
137 | 143 | return requireContext().getStampResId( |
138 | | - stampId = viewModel.currentState.stampId, |
| 144 | + stampId = stampId, |
139 | 145 | resNameParam = RES_NAME, |
140 | 146 | resType = RES_STAMP_TYPE, |
141 | 147 | packageName = requireContext().packageName |
142 | 148 | ) |
143 | 149 | } |
144 | 150 |
|
145 | | - private fun setLoadingState(isLoading: Boolean) { |
146 | | - with(binding) { |
147 | | - indeterminateBar.isVisible = isLoading |
148 | | - ivMyPageEditFrame.isClickable = !isLoading |
149 | | - viewMyPageMainSettingFrame.isClickable = !isLoading |
150 | | - } |
151 | | - } |
152 | | - |
153 | 151 | private inline fun <reified T : Activity> navigateTo() { |
154 | 152 | startActivity(Intent(requireContext(), T::class.java)) |
155 | 153 | requireActivity().overridePendingTransition( |
|
0 commit comments