Skip to content

Commit 4371b3a

Browse files
committed
✨ 인증글 작성하기 예외처리 및 사용자에게 알려주기와 등록 완료 시 커뮤니티 메인 페이지로 이동
1 parent d89d664 commit 4371b3a

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

presentation/src/main/java/com/whyranoid/presentation/community/runningpost/CreateRunningPostFragment.kt

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package com.whyranoid.presentation.community.runningpost
33
import android.os.Bundle
44
import android.view.View
55
import androidx.fragment.app.viewModels
6+
import androidx.navigation.fragment.findNavController
67
import com.google.android.material.snackbar.Snackbar
78
import com.whyranoid.presentation.R
89
import com.whyranoid.presentation.base.BaseFragment
910
import com.whyranoid.presentation.databinding.FragmentCreateRunningPostBinding
11+
import com.whyranoid.presentation.model.UiState
1012
import com.whyranoid.presentation.util.repeatWhenUiStarted
1113
import dagger.hilt.android.AndroidEntryPoint
1214

@@ -20,6 +22,7 @@ internal class CreateRunningPostFragment :
2022
super.onViewCreated(view, savedInstanceState)
2123

2224
initViews()
25+
observeState()
2326
}
2427

2528
private fun initViews() {
@@ -28,6 +31,19 @@ internal class CreateRunningPostFragment :
2831
setUpMenu()
2932
}
3033

34+
private fun observeState() {
35+
viewLifecycleOwner.repeatWhenUiStarted {
36+
viewModel.createPostState.collect { createPostState ->
37+
when (createPostState) {
38+
is UiState.UnInitialized -> {}
39+
is UiState.Loading -> {}
40+
is UiState.Success<Boolean> -> handleCreatePostStateSuccess(createPostState.value)
41+
is UiState.Failure -> {}
42+
}
43+
}
44+
}
45+
}
46+
3147
private fun setUpMenu() {
3248
with(binding.topAppBar) {
3349
inflateMenu(R.menu.community_create_running_post_menu)
@@ -51,7 +67,11 @@ internal class CreateRunningPostFragment :
5167
true
5268
}
5369
R.id.warning_about_create_running_post_button -> {
54-
Snackbar.make(binding.root, getString(R.string.community_warning_running_post), Snackbar.LENGTH_SHORT)
70+
Snackbar.make(
71+
binding.root,
72+
getString(R.string.community_warning_running_post),
73+
Snackbar.LENGTH_SHORT
74+
)
5575
.show()
5676
true
5777
}
@@ -62,4 +82,27 @@ internal class CreateRunningPostFragment :
6282
}
6383
}
6484
}
85+
86+
private fun handleCreatePostStateSuccess(result: Boolean) {
87+
if (result) {
88+
val action =
89+
CreateRunningPostFragmentDirections.actionCreateRunningPostFragmentToCommunityFragment()
90+
91+
Snackbar.make(
92+
requireView(),
93+
getString(R.string.community_success_create_running_post),
94+
Snackbar.LENGTH_SHORT
95+
)
96+
.show()
97+
98+
findNavController().navigate(action)
99+
} else {
100+
Snackbar.make(
101+
requireView(),
102+
getString(R.string.community_fail_create_running_post),
103+
Snackbar.LENGTH_SHORT
104+
)
105+
.show()
106+
}
107+
}
65108
}

presentation/src/main/java/com/whyranoid/presentation/community/runningpost/CreateRunningPostViewModel.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import androidx.lifecycle.ViewModel
55
import androidx.lifecycle.viewModelScope
66
import com.whyranoid.domain.usecase.CreateRunningPostUseCase
77
import com.whyranoid.presentation.model.RunningHistoryUiModel
8+
import com.whyranoid.presentation.model.UiState
89
import dagger.hilt.android.lifecycle.HiltViewModel
910
import kotlinx.coroutines.flow.MutableStateFlow
1011
import kotlinx.coroutines.flow.SharingStarted
1112
import kotlinx.coroutines.flow.StateFlow
13+
import kotlinx.coroutines.flow.asStateFlow
1214
import kotlinx.coroutines.flow.map
1315
import kotlinx.coroutines.flow.stateIn
1416
import kotlinx.coroutines.launch
@@ -33,13 +35,23 @@ class CreateRunningPostViewModel @Inject constructor(
3335
scope = viewModelScope
3436
)
3537

38+
private val _createPostState = MutableStateFlow<UiState<Boolean>>(UiState.UnInitialized)
39+
val createPostState: StateFlow<UiState<Boolean>>
40+
get() = _createPostState.asStateFlow()
41+
3642
fun createRunningPost() {
3743
viewModelScope.launch {
44+
_createPostState.value = UiState.Loading
45+
3846
selectedRunningHistory?.let { runningHistory ->
3947
createRunningPostUseCase(
4048
runningPostContent.value.toString(),
4149
runningHistory.historyId
42-
)
50+
).onSuccess { createPostResult ->
51+
_createPostState.value = UiState.Success(createPostResult)
52+
}.onFailure { throwable ->
53+
_createPostState.value = UiState.Failure(throwable)
54+
}
4355
}
4456
}
4557
}

presentation/src/main/res/navigation/navigation_graph.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@
8686
<argument
8787
android:name="selectedRunningHistory"
8888
app:argType="com.whyranoid.presentation.model.RunningHistoryUiModel" />
89+
<action
90+
android:id="@+id/action_createRunningPostFragment_to_communityFragment"
91+
app:destination="@id/communityFragment" />
8992
</fragment>
9093
<fragment
9194
android:id="@+id/runningFinishFragment"

presentation/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
<string name="community_select_running_history_snack_bar">운동을 하나 선택해주세요!</string>
3030
<string name="community_go_to_create_running_post">인증글 생성하러 가기</string>
3131
<string name="community_warning_running_post">내용을 1글자 이상 입력하세요</string>
32+
<string name="community_fail_create_running_post">인증글 작성에 실패했습니다</string>
33+
<string name="community_success_create_running_post">인증글이 등록되었습니다!</string>
3234

3335
<!-- 마이런 탭 화면 -->
3436
<string name="my_run_tool_bar_menu_setting">설정</string>

0 commit comments

Comments
 (0)