Skip to content

Commit 74bd26e

Browse files
committed
✨ 그룹 수정 전 중복확인을 반드시 하도록 변경
1 parent 549cc05 commit 74bd26e

File tree

4 files changed

+68
-24
lines changed

4 files changed

+68
-24
lines changed

presentation/src/main/java/com/whyranoid/presentation/community/group/edit/EditGroupFragment.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ internal class EditGroupFragment :
5151
).show()
5252
}
5353
}
54+
is Event.DuplicateCheckButtonClick -> {
55+
if (event.isDuplicatedGroupName) {
56+
Snackbar.make(
57+
binding.root,
58+
getString(R.string.text_duplicated_group_name),
59+
Snackbar.LENGTH_SHORT
60+
).show()
61+
} else {
62+
Snackbar.make(
63+
binding.root,
64+
getString(R.string.text_un_duplicated_group_name),
65+
Snackbar.LENGTH_SHORT
66+
).show()
67+
binding.etGroupName.isEnabled = false
68+
}
69+
}
5470
is Event.WarningButtonClick -> {
5571
Snackbar.make(
5672
binding.root,
@@ -69,7 +85,7 @@ internal class EditGroupFragment :
6985
}
7086

7187
viewLifecycleOwner.repeatWhenUiStarted {
72-
viewModel.isButtonEnable.collect { isEnable ->
88+
viewModel.isGroupCreateButtonEnable.collect { isEnable ->
7389
if (isEnable) {
7490
binding.topAppBar.menu.setGroupVisible(R.id.ready_to_create, true)
7591
binding.topAppBar.menu.setGroupVisible(R.id.not_ready_to_create, false)

presentation/src/main/java/com/whyranoid/presentation/community/group/edit/EditGroupViewModel.kt

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.lifecycle.SavedStateHandle
44
import androidx.lifecycle.ViewModel
55
import androidx.lifecycle.viewModelScope
66
import com.whyranoid.domain.model.toRule
7+
import com.whyranoid.domain.usecase.CheckIsDuplicatedGroupNameUseCase
78
import com.whyranoid.domain.usecase.UpdateGroupInfoUseCase
89
import com.whyranoid.presentation.model.GroupInfoUiModel
910
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -20,10 +21,11 @@ import javax.inject.Inject
2021
@HiltViewModel
2122
class EditGroupViewModel @Inject constructor(
2223
private val updateGroupInfoUseCase: UpdateGroupInfoUseCase,
24+
private val checkIsDuplicatedGroupNameUseCase: CheckIsDuplicatedGroupNameUseCase,
2325
stateHandle: SavedStateHandle
2426
) : ViewModel() {
2527

26-
private val initGroupInfo = stateHandle.get<GroupInfoUiModel>("groupInfo")!!
28+
private val initGroupInfo = requireNotNull(stateHandle.get<GroupInfoUiModel>("groupInfo"))
2729

2830
val groupName = MutableStateFlow<String>(initGroupInfo.name)
2931
val groupIntroduce = MutableStateFlow<String>(initGroupInfo.introduce)
@@ -32,7 +34,9 @@ class EditGroupViewModel @Inject constructor(
3234
private val _eventFlow = MutableSharedFlow<Event>()
3335
val eventFlow = _eventFlow.asSharedFlow()
3436

35-
val isButtonEnable: StateFlow<Boolean>
37+
private val isNotDuplicate = MutableStateFlow(false)
38+
39+
val isDoubleCheckButtonEnable: StateFlow<Boolean>
3640
get() = groupName.combine(groupIntroduce) { name, introduce ->
3741
name.trim().isNotEmpty() && introduce.trim().isNotEmpty()
3842
}.stateIn(
@@ -41,37 +45,60 @@ class EditGroupViewModel @Inject constructor(
4145
started = SharingStarted.WhileSubscribed(5000)
4246
)
4347

44-
// TODO : 중복확인 로직
48+
val isGroupCreateButtonEnable: StateFlow<Boolean>
49+
get() = combine(
50+
isDoubleCheckButtonEnable,
51+
isNotDuplicate
52+
) { isDoubleCheckButtonEnable, isNotDuplicate ->
53+
isDoubleCheckButtonEnable && isNotDuplicate
54+
}.stateIn(
55+
scope = viewModelScope,
56+
initialValue = false,
57+
started = SharingStarted.WhileSubscribed(5000)
58+
)
59+
4560
fun onDuplicateCheckButtonClicked() {
61+
viewModelScope.launch {
62+
val isDuplicatedGroupName = checkIsDuplicatedGroupNameUseCase(groupName.value)
63+
emitEvent(Event.DuplicateCheckButtonClick(isDuplicatedGroupName))
64+
isNotDuplicate.value = isDuplicatedGroupName.not()
65+
}
4666
}
4767

4868
fun onAddRuleButtonClicked() {
4969
emitEvent(Event.AddRuleButtonClick)
5070
}
5171

5272
fun emitEvent(event: Event) {
53-
when (event) {
54-
is Event.AddRuleButtonClick,
55-
Event.WarningButtonClick -> {
56-
viewModelScope.launch {
73+
viewModelScope.launch {
74+
when (event) {
75+
is Event.AddRuleButtonClick,
76+
Event.WarningButtonClick -> {
5777
_eventFlow.emit(event)
5878
}
59-
}
60-
// TODO : 성공 여부에 따른 분기처리
61-
is Event.EditGroupButtonClick -> {
62-
viewModelScope.launch {
63-
val isSuccess = updateGroupInfoUseCase(
64-
groupId = initGroupInfo.groupId,
65-
groupName = groupName.value,
66-
groupIntroduce = groupIntroduce.value,
67-
rules = rules.value.map {
68-
it.toRule()
69-
}
70-
)
71-
if (isSuccess) {
72-
_eventFlow.emit(event)
79+
is Event.DuplicateCheckButtonClick -> {
80+
if (event.isDuplicatedGroupName) {
81+
_eventFlow.emit(event.copy(isDuplicatedGroupName = true))
7382
} else {
74-
_eventFlow.emit(Event.EditGroupButtonClick(false))
83+
_eventFlow.emit(event)
84+
}
85+
}
86+
// TODO : 성공 여부에 따른 분기처리
87+
is Event.EditGroupButtonClick -> {
88+
viewModelScope.launch {
89+
val isSuccess = updateGroupInfoUseCase(
90+
groupId = initGroupInfo.groupId,
91+
groupName = groupName.value,
92+
groupIntroduce = groupIntroduce.value,
93+
rules = rules.value.map {
94+
it.toRule()
95+
}
96+
)
97+
if (isSuccess) {
98+
_eventFlow.emit(event)
99+
} else {
100+
_eventFlow.emit(Event.EditGroupButtonClick(false))
101+
}
75102
}
76103
}
77104
}

presentation/src/main/java/com/whyranoid/presentation/community/group/edit/Event.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ sealed class Event {
55
data class EditGroupButtonClick(val isSuccess: Boolean = true) : Event()
66
object WarningButtonClick : Event()
77
object AddRuleButtonClick : Event()
8+
data class DuplicateCheckButtonClick(val isDuplicatedGroupName: Boolean = false) : Event()
89
}

presentation/src/main/res/layout/fragment_edit_group.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
android:layout_width="wrap_content"
6060
android:layout_height="wrap_content"
6161
android:layout_margin="16dp"
62-
android:enabled="@{viewModel.isButtonEnable()}"
62+
android:enabled="@{viewModel.isDoubleCheckButtonEnable()}"
6363
android:onClick="@{() -> viewModel.onDuplicateCheckButtonClicked()}"
6464
android:text="@string/text_duplicate_check"
6565
app:layout_constraintBottom_toTopOf="@id/time_and_date_picker"

0 commit comments

Comments
 (0)