Skip to content

Commit

Permalink
Use collectAsStateWithLifecycle for the safe unsubscriptions (#1286)
Browse files Browse the repository at this point in the history
Co-authored-by: Aleksandar Apostolov <[email protected]>
  • Loading branch information
skydoves and aleksandar-apostolov authored Jan 22, 2025
1 parent caf3e54 commit c12db8c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fun CallLobbyScreen(
onBack: () -> Unit,
) {
LockScreenOrientation(orientation = Configuration.ORIENTATION_PORTRAIT)
val isLoading by callLobbyViewModel.isLoading.collectAsState()
val isLoading by callLobbyViewModel.isLoading.collectAsStateWithLifecycle()
val isMicrophoneEnabled by callLobbyViewModel.microphoneEnabled.collectAsStateWithLifecycle()
val isCameraEnabled by callLobbyViewModel.cameraEnabled.collectAsStateWithLifecycle()
val call by remember {
Expand Down Expand Up @@ -271,7 +271,7 @@ private fun CallLobbyBody(
private fun LobbyDescription(
callLobbyViewModel: CallLobbyViewModel,
) {
val participantCounts by callLobbyViewModel.call.state.participantCounts.collectAsState()
val participantCounts by callLobbyViewModel.call.state.participantCounts.collectAsStateWithLifecycle()

LobbyDescriptionContent(participantCounts = participantCounts) {
callLobbyViewModel.handleUiEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -45,6 +44,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import io.getstream.video.android.compose.theme.VideoTheme
import io.getstream.video.android.compose.ui.components.call.controls.actions.ToggleSpeakerphoneAction
import io.getstream.video.android.core.Call
Expand All @@ -69,7 +69,7 @@ public fun BoxScope.LivestreamPlayerOverlay(call: Call) {

@Composable
private fun BoxScope.LiveBadge(call: Call) {
val totalParticipants by call.state.totalParticipants.collectAsState()
val totalParticipants by call.state.totalParticipants.collectAsStateWithLifecycle()

Row(
modifier = Modifier
Expand Down Expand Up @@ -114,7 +114,7 @@ private fun BoxScope.LiveBadge(call: Call) {

@Composable
private fun BoxScope.LiveDuration(call: Call) {
val duration by call.state.duration.collectAsState()
val duration by call.state.duration.collectAsStateWithLifecycle()

Row(
modifier = Modifier.align(Alignment.Center),
Expand Down Expand Up @@ -142,7 +142,7 @@ private fun BoxScope.LiveControls(call: Call) {
val speakerphoneEnabled by if (LocalInspectionMode.current) {
remember { mutableStateOf(true) }
} else {
call.speaker.isEnabled.collectAsState()
call.speaker.isEnabled.collectAsStateWithLifecycle()
}

ToggleSpeakerphoneAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -39,6 +38,7 @@ import androidx.compose.ui.draw.alpha
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import io.getstream.video.android.compose.theme.VideoTheme
import io.getstream.video.android.compose.ui.components.video.VideoRenderer
import io.getstream.video.android.core.Call
Expand All @@ -52,7 +52,7 @@ internal fun LivestreamRenderer(
enablePausing: Boolean,
onPausedPlayer: ((isPaused: Boolean) -> Unit)? = {},
) {
val livestream by call.state.livestream.collectAsState()
val livestream by call.state.livestream.collectAsStateWithLifecycle()
var videoTextureView: VideoTextureViewRenderer? by remember { mutableStateOf(null) }
var isPaused by rememberSaveable { mutableStateOf(false) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,11 @@ package io.getstream.video.android.compose.utils

import androidx.annotation.DimenRes
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.sp
import io.getstream.video.android.ui.common.util.getFloatResource

@Composable
internal fun textSizeResource(@DimenRes id: Int): TextUnit {
return dimensionResource(id = id).value.sp
}

@Composable
internal fun floatResource(@DimenRes id: Int): Float {
return LocalContext.current.getFloatResource(id)
}

0 comments on commit c12db8c

Please sign in to comment.