Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import com.tomsksoft.videoeffectsrecorder.ui.entity.SecondaryFiltersMode
import com.tomsksoft.videoeffectsrecorder.ui.viewmodel.CameraViewModel
import com.tomsksoft.videoeffectsrecorder.ui.viewmodel.CameraViewModelImpl
import com.tomsksoft.videoeffectsrecorder.ui.viewmodel.CameraViewModelStub
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

private const val REQUEST_PICK_PHOTO_BACKGROUND = 1
Expand Down Expand Up @@ -251,13 +252,14 @@ fun CameraUi(viewModel: CameraViewModel, onGalleryClick: () -> Unit) {
cameraUiState = cameraUiState,
onFlipCameraClick = viewModel::flipCamera,
onCaptureClick = {
scope.launch {
val durationMs = 200
alphaAnimation.animateTo(0f, snap())
alphaAnimation.animateTo(1f, tween(durationMs / 2))
alphaAnimation.animateTo(0f, tween(durationMs / 2))
val disposable = viewModel.captureImage().subscribe { _ ->
scope.launch(Dispatchers.Main) {
val durationMs = 200
alphaAnimation.animateTo(0f, snap())
alphaAnimation.animateTo(1f, tween(durationMs / 2))
alphaAnimation.animateTo(0f, tween(durationMs / 2))
}
}
viewModel.captureImage()
},
onLongPress = viewModel::startVideoRecording,
onRelease = viewModel::stopVideoRecording,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.tomsksoft.videoeffectsrecorder.domain.entity.ColorCorrection
import com.tomsksoft.videoeffectsrecorder.ui.entity.CameraUiState
import com.tomsksoft.videoeffectsrecorder.ui.entity.PrimaryFiltersMode
import com.tomsksoft.videoeffectsrecorder.ui.entity.SecondaryFiltersMode
import io.reactivex.rxjava3.core.Single
import kotlinx.coroutines.flow.StateFlow
import java.io.InputStream

Expand All @@ -20,7 +21,7 @@ interface CameraViewModel {
fun setBackground(bitmapStream: InputStream)
fun removeBackground()
fun flipCamera()
fun captureImage()
fun captureImage(): Single<*>
fun startVideoRecording()
fun stopVideoRecording()
fun setBlurPower(value: Float)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.tomsksoft.videoeffectsrecorder.ui.entity.PrimaryFiltersMode
import com.tomsksoft.videoeffectsrecorder.ui.entity.SecondaryFiltersMode
import com.tomsksoft.videoeffectsrecorder.ui.getNextFlashMode
import dagger.hilt.android.lifecycle.HiltViewModel
import io.reactivex.rxjava3.core.Single
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand Down Expand Up @@ -219,9 +220,9 @@ class CameraViewModelImpl @Inject constructor(
}
}

override fun captureImage() {
override fun captureImage(): Single<*> {
Log.d(TAG, "Capture image")
cameraRecordManager.takePhoto(camera!!.frame)
return cameraRecordManager.takePhoto(camera!!.frame)
}

override fun startVideoRecording() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.tomsksoft.videoeffectsrecorder.domain.boundary.PhotoPicker
import com.tomsksoft.videoeffectsrecorder.domain.boundary.VideoRecorder
import com.tomsksoft.videoeffectsrecorder.domain.entity.FlashMode
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.subjects.PublishSubject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
Expand All @@ -22,29 +24,31 @@ class CameraRecordManager(
}

private var record: AutoCloseable? = null
private val scope = CoroutineScope(Dispatchers.IO)

val isRecording: Boolean get() = record != null

fun takePhoto(frameSource: Observable<Any>) {
scope.launch {
val frame: Any
fun takePhoto(frameSource: Observable<Any>): Single<*> =
Observable.create { frameEmitter ->
fun emit() {
val frame = frameSource.blockingFirst()
frameEmitter.onNext(frame)
frameEmitter.onComplete()

photoPicker.takePhoto(
frame,
cameraManager.orientation.blockingFirst(),
PHOTO_BASE_NAME,
PHOTO_MIME_TYPE
)
}

if (cameraManager.flashMode.blockingFirst() == FlashMode.AUTO) {
cameraManager.setFlashEnabled(true)
delay(1000L)
frame = frameSource.blockingFirst()
Thread.sleep(1000)
emit()
cameraManager.setFlashEnabled(false)
} else {
frame = frameSource.blockingFirst()
}
photoPicker.takePhoto(
frame,
cameraManager.orientation.blockingFirst(),
PHOTO_BASE_NAME,
PHOTO_MIME_TYPE
)
}
}
} else emit()
}.singleOrError()

fun startRecord(frameSource: Observable<Any>) {
record = videoRecorder.startRecord(
Expand Down