-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskCertificationViewModel.kt
More file actions
54 lines (46 loc) · 1.57 KB
/
TaskCertificationViewModel.kt
File metadata and controls
54 lines (46 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.twix.task_certification
import android.net.Uri
import androidx.lifecycle.viewModelScope
import com.twix.task_certification.model.TaskCertificationIntent
import com.twix.task_certification.model.TaskCertificationSideEffect
import com.twix.task_certification.model.TaskCertificationUiState
import com.twix.task_certification.model.TorchStatus
import com.twix.ui.base.BaseViewModel
import kotlinx.coroutines.launch
class TaskCertificationViewModel :
BaseViewModel<TaskCertificationUiState, TaskCertificationIntent, TaskCertificationSideEffect>(
TaskCertificationUiState(),
) {
override suspend fun handleIntent(intent: TaskCertificationIntent) {
when (intent) {
is TaskCertificationIntent.TakePicture -> {
takePicture(intent.uri)
}
is TaskCertificationIntent.ToggleLens -> {
toggleLens()
}
is TaskCertificationIntent.ToggleFlash -> {
toggleTorch()
}
}
}
private fun takePicture(uri: Uri?) {
uri?.let {
reduce { updateCapturedImage(uri) }
if (uiState.value.torch == TorchStatus.On) {
reduce { toggleTorch() }
}
} ?: run { onFailureCapture() }
}
private fun onFailureCapture() {
viewModelScope.launch {
emitSideEffect(TaskCertificationSideEffect.ImageCaptureFailException)
}
}
private fun toggleLens() {
reduce { toggleLens() }
}
private fun toggleTorch() {
reduce { toggleTorch() }
}
}