Skip to content

Commit 0d93d5f

Browse files
committed
Feat: 루틴 토글 usecase 로직 추가
- RoutineToggleState 정의 - 기존 viewmodel에서 비즈니스 로직 domain으로 변경
1 parent cffd981 commit 0d93d5f

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.threegap.bitnagil.domain.routine.model
2+
3+
data class RoutineToggleState(
4+
val isCompleted: Boolean,
5+
val subRoutinesIsCompleted: List<Boolean>,
6+
)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.threegap.bitnagil.domain.routine.usecase
2+
3+
import com.threegap.bitnagil.domain.routine.model.RoutineToggleState
4+
import javax.inject.Inject
5+
6+
class ToggleRoutineUseCase @Inject constructor() {
7+
8+
/**
9+
* 메인 루틴을 토글합니다.
10+
* 메인 루틴이 토글되면 모든 서브 루틴도 같은 상태로 변경됩니다.
11+
*
12+
* @param isCompleted 현재 메인 루틴 완료 상태
13+
* @param subRoutineStates 현재 서브 루틴 완료 상태 리스트
14+
* @return 토글된 루틴 상태
15+
*/
16+
fun toggleMainRoutine(
17+
isCompleted: Boolean,
18+
subRoutineStates: List<Boolean>,
19+
): RoutineToggleState {
20+
val newIsCompleted = !isCompleted
21+
val newSubRoutineStates = subRoutineStates.map { newIsCompleted }
22+
23+
return RoutineToggleState(
24+
isCompleted = newIsCompleted,
25+
subRoutinesIsCompleted = newSubRoutineStates,
26+
)
27+
}
28+
29+
/**
30+
* 특정 서브 루틴을 토글합니다.
31+
* 모든 서브 루틴이 완료되면 메인 루틴도 자동으로 완료됩니다.
32+
*
33+
* @param index 토글할 서브 루틴의 인덱스
34+
* @param subRoutineStates 현재 서브 루틴 완료 상태 리스트
35+
* @return 토글된 루틴 상태, 잘못된 인덱스인 경우 null
36+
*/
37+
fun toggleSubRoutine(
38+
index: Int,
39+
subRoutineStates: List<Boolean>,
40+
): RoutineToggleState? {
41+
if (index !in subRoutineStates.indices) {
42+
return null
43+
}
44+
45+
val newState = !subRoutineStates[index]
46+
val newSubRoutineStates = subRoutineStates.toMutableList().apply {
47+
this[index] = newState
48+
}
49+
50+
val allCompleted = newSubRoutineStates.all { it }
51+
52+
return RoutineToggleState(
53+
isCompleted = allCompleted,
54+
subRoutinesIsCompleted = newSubRoutineStates,
55+
)
56+
}
57+
}

0 commit comments

Comments
 (0)