@@ -4,6 +4,7 @@ import androidx.lifecycle.SavedStateHandle
4
4
import androidx.lifecycle.ViewModel
5
5
import androidx.lifecycle.viewModelScope
6
6
import com.whyranoid.domain.model.toRule
7
+ import com.whyranoid.domain.usecase.CheckIsDuplicatedGroupNameUseCase
7
8
import com.whyranoid.domain.usecase.UpdateGroupInfoUseCase
8
9
import com.whyranoid.presentation.model.GroupInfoUiModel
9
10
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -20,10 +21,11 @@ import javax.inject.Inject
20
21
@HiltViewModel
21
22
class EditGroupViewModel @Inject constructor(
22
23
private val updateGroupInfoUseCase : UpdateGroupInfoUseCase ,
24
+ private val checkIsDuplicatedGroupNameUseCase : CheckIsDuplicatedGroupNameUseCase ,
23
25
stateHandle : SavedStateHandle
24
26
) : ViewModel() {
25
27
26
- private val initGroupInfo = stateHandle.get<GroupInfoUiModel >(" groupInfo" )!!
28
+ private val initGroupInfo = requireNotNull( stateHandle.get<GroupInfoUiModel >(" groupInfo" ))
27
29
28
30
val groupName = MutableStateFlow <String >(initGroupInfo.name)
29
31
val groupIntroduce = MutableStateFlow <String >(initGroupInfo.introduce)
@@ -32,7 +34,9 @@ class EditGroupViewModel @Inject constructor(
32
34
private val _eventFlow = MutableSharedFlow <Event >()
33
35
val eventFlow = _eventFlow .asSharedFlow()
34
36
35
- val isButtonEnable: StateFlow <Boolean >
37
+ private val isNotDuplicate = MutableStateFlow (false )
38
+
39
+ val isDoubleCheckButtonEnable: StateFlow <Boolean >
36
40
get() = groupName.combine(groupIntroduce) { name, introduce ->
37
41
name.trim().isNotEmpty() && introduce.trim().isNotEmpty()
38
42
}.stateIn(
@@ -41,37 +45,60 @@ class EditGroupViewModel @Inject constructor(
41
45
started = SharingStarted .WhileSubscribed (5000 )
42
46
)
43
47
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
+
45
60
fun onDuplicateCheckButtonClicked () {
61
+ viewModelScope.launch {
62
+ val isDuplicatedGroupName = checkIsDuplicatedGroupNameUseCase(groupName.value)
63
+ emitEvent(Event .DuplicateCheckButtonClick (isDuplicatedGroupName))
64
+ isNotDuplicate.value = isDuplicatedGroupName.not ()
65
+ }
46
66
}
47
67
48
68
fun onAddRuleButtonClicked () {
49
69
emitEvent(Event .AddRuleButtonClick )
50
70
}
51
71
52
72
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 -> {
57
77
_eventFlow .emit(event)
58
78
}
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 ))
73
82
} 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
+ }
75
102
}
76
103
}
77
104
}
0 commit comments