Skip to content

Commit a5820af

Browse files
committed
♻️ Refactor: 카메라 캡처 안정성 및 리소스 해제 로직 개선
- `capture` 시 `CancellableContinuation`의 활성 상태(`isActive`)를 확인하여 불필요한 resume 방지 - `unbind` 호출 시 `surfaceRequests`를 초기화하도록 수정 - `bindToLifecycle` 시 기존에 생성된 `cameraProvider`가 있다면 재사용하도록 개선
1 parent 438f586 commit a5820af

File tree

1 file changed

+6
-3
lines changed
  • feature/photolog/capture/src/main/java/com/twix/photolog/capture/model/camera

1 file changed

+6
-3
lines changed

feature/photolog/capture/src/main/java/com/twix/photolog/capture/model/camera/CaptureCamera.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import androidx.camera.lifecycle.awaitInstance
1616
import androidx.core.content.ContextCompat
1717
import androidx.lifecycle.LifecycleOwner
1818
import com.twix.photolog.capture.model.TorchStatus
19+
import kotlinx.coroutines.CancellableContinuation
1920
import kotlinx.coroutines.flow.MutableStateFlow
2021
import kotlinx.coroutines.flow.StateFlow
2122
import kotlinx.coroutines.flow.asStateFlow
2223
import kotlinx.coroutines.suspendCancellableCoroutine
23-
import kotlin.coroutines.Continuation
2424
import kotlin.coroutines.resume
2525

2626
class CaptureCamera(
@@ -53,7 +53,7 @@ class CaptureCamera(
5353
lifecycleOwner: LifecycleOwner,
5454
lens: CameraSelector,
5555
) {
56-
val provider = ProcessCameraProvider.awaitInstance(context)
56+
val provider = cameraProvider ?: ProcessCameraProvider.awaitInstance(context)
5757
cameraProvider = provider
5858

5959
provider.unbindAll()
@@ -102,9 +102,10 @@ class CaptureCamera(
102102
contentValues,
103103
).build()
104104

105-
private fun capture(continuation: Continuation<Result<Uri>>): ImageCapture.OnImageSavedCallback =
105+
private fun capture(continuation: CancellableContinuation<Result<Uri>>): ImageCapture.OnImageSavedCallback =
106106
object : ImageCapture.OnImageSavedCallback {
107107
override fun onImageSaved(result: ImageCapture.OutputFileResults) {
108+
if (continuation.isActive.not()) return
108109
val uri = result.savedUri
109110
if (uri != null) {
110111
continuation.resume(Result.success(uri))
@@ -116,12 +117,14 @@ class CaptureCamera(
116117
}
117118

118119
override fun onError(exception: ImageCaptureException) {
120+
if (continuation.isActive.not()) return
119121
continuation.resume(Result.failure(exception))
120122
}
121123
}
122124

123125
override fun unbind() {
124126
cameraProvider?.unbindAll()
127+
_surfaceRequests.value = null
125128
}
126129

127130
override fun toggleTorch(torch: TorchStatus) {

0 commit comments

Comments
 (0)