Skip to content

Commit 662f4cf

Browse files
committed
♻️ Refactor: PhotologCaptureViewModel 상태 업데이트 로직 개선
1 parent a5820af commit 662f4cf

File tree

3 files changed

+23
-48
lines changed

3 files changed

+23
-48
lines changed

core/design-system/src/main/java/com/twix/designsystem/components/comment/model/CommentUiModel.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ data class CommentUiModel(
1616
value.isNotEmpty() &&
1717
hasMaxCommentLength
1818

19-
fun updateComment(newComment: String): CommentUiModel = copy(value = newComment)
20-
21-
fun updateFocus(isFocused: Boolean) = copy(isFocused = isFocused)
22-
2319
companion object {
2420
const val COMMENT_COUNT = 5
2521
}

feature/photolog/capture/src/main/java/com/twix/photolog/capture/PhotologCaptureViewModel.kt

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.twix.photolog.capture
22

33
import android.net.Uri
4+
import androidx.camera.core.CameraSelector
45
import androidx.lifecycle.SavedStateHandle
56
import androidx.lifecycle.viewModelScope
67
import com.twix.designsystem.R
@@ -15,6 +16,7 @@ import com.twix.photolog.capture.contract.PhotologCaptureIntent
1516
import com.twix.photolog.capture.contract.PhotologCaptureSideEffect
1617
import com.twix.photolog.capture.contract.PhotologCaptureUiState
1718
import com.twix.photolog.capture.model.CaptureStatus
19+
import com.twix.photolog.capture.model.TorchStatus
1820
import com.twix.ui.base.BaseViewModel
1921
import com.twix.ui.image.ImageGenerator
2022
import com.twix.util.bus.GoalRefreshBus
@@ -71,30 +73,42 @@ class PhotologCaptureViewModel(
7173
}
7274

7375
private fun reducePicture(uri: Uri) {
74-
reduce { updatePicture(uri) }
76+
reduce {
77+
copy(
78+
capture = CaptureStatus.Captured(uri),
79+
torch = TorchStatus.Off,
80+
)
81+
}
7582
if (uiState.value.hasMaxCommentLength.not()) {
7683
reduceCommentFocus(true)
7784
}
7885
}
7986

8087
private fun reduceLens() {
81-
reduce { toggleLens() }
88+
val newLens =
89+
if (currentState.lens == CameraSelector.DEFAULT_BACK_CAMERA) {
90+
CameraSelector.DEFAULT_FRONT_CAMERA
91+
} else {
92+
CameraSelector.DEFAULT_BACK_CAMERA
93+
}
94+
95+
reduce { copy(lens = newLens, torch = TorchStatus.Off) }
8296
}
8397

8498
private fun reduceTorch() {
85-
reduce { toggleTorch() }
99+
reduce { copy(torch = TorchStatus.toggle(torch)) }
86100
}
87101

88102
private fun setupRetake() {
89-
reduce { removePicture() }
103+
reduce { copy(capture = CaptureStatus.NotCaptured) }
90104
}
91105

92-
private fun reduceComment(comment: String) {
93-
reduce { updateComment(comment) }
106+
private fun reduceComment(newComment: String) {
107+
reduce { copy(comment = comment.copy(value = newComment)) }
94108
}
95109

96110
private fun reduceCommentFocus(isFocused: Boolean) {
97-
reduce { updateCommentFocus(isFocused) }
111+
reduce { copy(comment = comment.copy(isFocused = isFocused)) }
98112
}
99113

100114
private fun handleUploadIntent() {
@@ -120,9 +134,9 @@ class PhotologCaptureViewModel(
120134
private fun showValidationError() {
121135
viewModelScope.launch {
122136
if (!currentState.comment.canUpload) {
123-
reduce { showCommentError() }
137+
reduce { copy(showCommentError = true) }
124138
delay(ERROR_DISPLAY_DURATION_MS)
125-
reduce { hideCommentError() }
139+
reduce { copy(showCommentError = false) }
126140
}
127141
}
128142
}
Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.twix.photolog.capture.contract
22

3-
import android.net.Uri
43
import androidx.camera.core.CameraSelector
54
import androidx.compose.runtime.Immutable
65
import com.twix.designsystem.components.comment.model.CommentUiModel
@@ -24,38 +23,4 @@ data class PhotologCaptureUiState(
2423

2524
val showTorch: Boolean
2625
get() = capture is CaptureStatus.NotCaptured && lens == CameraSelector.DEFAULT_BACK_CAMERA
27-
28-
fun toggleLens(): PhotologCaptureUiState {
29-
val newLens =
30-
if (lens == CameraSelector.DEFAULT_BACK_CAMERA) {
31-
CameraSelector.DEFAULT_FRONT_CAMERA
32-
} else {
33-
CameraSelector.DEFAULT_BACK_CAMERA
34-
}
35-
return copy(
36-
lens = newLens,
37-
torch = TorchStatus.Off,
38-
)
39-
}
40-
41-
fun toggleTorch(): PhotologCaptureUiState {
42-
val newFlashMode = TorchStatus.Companion.toggle(torch)
43-
return copy(torch = newFlashMode)
44-
}
45-
46-
fun updatePicture(uri: Uri): PhotologCaptureUiState =
47-
copy(
48-
capture = CaptureStatus.Captured(uri),
49-
torch = TorchStatus.Off,
50-
)
51-
52-
fun removePicture(): PhotologCaptureUiState = copy(capture = CaptureStatus.NotCaptured)
53-
54-
fun updateComment(newComment: String) = copy(comment = comment.updateComment(newComment))
55-
56-
fun updateCommentFocus(isFocused: Boolean) = copy(comment = comment.updateFocus(isFocused))
57-
58-
fun showCommentError() = copy(showCommentError = true)
59-
60-
fun hideCommentError() = copy(showCommentError = false)
6126
}

0 commit comments

Comments
 (0)