Skip to content

Commit 372fb12

Browse files
committed
start / stop stream in lifecycle FoKE-Developers#9
1 parent 38c6fe8 commit 372fb12

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

presenter/src/main/java/com/foke/together/presenter/screen/CameraScreen.kt

+18-13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import androidx.compose.material3.Surface
1111
import androidx.compose.material3.Text
1212
import androidx.compose.runtime.Composable
1313
import androidx.compose.ui.Modifier
14+
import androidx.compose.ui.platform.LocalContext
1415
import androidx.compose.ui.text.font.FontWeight
1516
import androidx.compose.ui.tooling.preview.Preview
1617
import androidx.compose.ui.unit.dp
@@ -35,18 +36,7 @@ fun CameraScreen(
3536
viewModel: CameraViewModel = hiltViewModel()
3637
) {
3738
val TAG = "CameraScreen"
38-
LifecycleEventEffect(Lifecycle.Event.ON_START) {
39-
viewModel.setCaptureTimer { navigateToShare() }
40-
AppLog.d(TAG, "ON_START")
41-
}
42-
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
43-
AppLog.d(TAG, "ON_RESUME")
44-
viewModel.startCaptureTimer()
45-
}
46-
LifecycleEventEffect(Lifecycle.Event.ON_STOP) {
47-
viewModel.stopCaptureTimer()
48-
AppLog.d(TAG, "ON_STOP")
49-
}
39+
var mjpegView: MjpegView? = null
5040
ConstraintLayout(
5141
modifier = Modifier.fillMaxSize()
5242
) {
@@ -103,6 +93,7 @@ fun CameraScreen(
10393
.aspectRatio(1.5f),
10494
factory = { context ->
10595
MjpegView(context).apply {
96+
mjpegView = this
10697
mode = MjpegView.MODE_BEST_FIT
10798
isAdjustHeight = true
10899
supportPinchZoomAndPan = false
@@ -135,7 +126,6 @@ fun CameraScreen(
135126
// test url
136127
// TODO : change url in viewmodel
137128
setUrl("http://10.32.100.37:5000/preview")
138-
startStream()
139129
}
140130
},
141131
)
@@ -153,6 +143,21 @@ fun CameraScreen(
153143
fontSize = 24.sp,
154144
)
155145
}
146+
LifecycleEventEffect(Lifecycle.Event.ON_START) {
147+
viewModel.setCaptureTimer { navigateToShare() }
148+
AppLog.d(TAG, "ON_START")
149+
}
150+
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
151+
AppLog.d(TAG, "ON_RESUME")
152+
viewModel.startCaptureTimer()
153+
mjpegView?.startStream()
154+
}
155+
LifecycleEventEffect(Lifecycle.Event.ON_STOP) {
156+
viewModel.stopCaptureTimer()
157+
AppLog.d(TAG, "ON_STOP")
158+
mjpegView?.stopStream()
159+
}
160+
LocalContext
156161
}
157162

158163
@Preview(showBackground = true)

presenter/src/main/java/com/foke/together/presenter/viewmodel/CameraViewModel.kt

-3
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,16 @@ class CameraViewModel @Inject constructor(
2626
captureTimer = object : CountDownTimer(5000, 10) {
2727
override fun onTick(millisUntilFinished: Long) {
2828
_progressState.floatValue = 1f - (millisUntilFinished.toFloat() / 5000)
29-
AppLog.d("CameraViewModel", "onTick: ${_progressState.floatValue}")
3029
}
3130
override fun onFinish() {
3231
_progressState.floatValue = 1f
3332
if(_captureCount.intValue < 4){
3433
_captureCount.intValue += 1
3534
startCaptureTimer()
36-
AppLog.d("CameraViewModel", "onFinish: ${_captureCount.intValue}")
3735
}
3836
else {
3937
stopCaptureTimer()
4038
_captureCount.intValue = 1
41-
AppLog.d("CameraViewModel", "onFinish: ${_captureCount.intValue}")
4239
nextNavigate()
4340
}
4441
}

0 commit comments

Comments
 (0)