Skip to content

MediaRouteButton - remember dialog state #3172

Description

@mroczis

Use case description

I have integrated androidx.media3.cast.MediaRouteButton into my Compose UI. The problem is that my UI hides when user does not interact with it (traditional video controls). When user taps on a cast icon then cast dialog is shown. If user does not interact with it then video controls disappear including the dialog. Currently there is no way to tell if dialog is shown since showDialog it is hidden in lib function.

@MainThread
@UnstableApi
@Composable
fun MediaRouteButton(modifier: Modifier = Modifier) {
  CastUtils.verifyMainThread()
  MediaRouteButtonContainer() {
    var showDialog by remember { mutableStateOf(false) }
    IconButton(onClick = { showDialog = true }, modifier) { mediaRouteButtonIcon() }
    if (showDialog) {
      MediaRouteDialog { showDialog = false }
    }
  }
}

Proposed solution

I'd prefer to have an observable that tells me if dialog is shown so I can stop my local timer.

@MainThread
@UnstableApi
@Composable
fun MediaRouteButton(
    modifier: Modifier = Modifier,
    state: MediaRouteState = rememberMediaRouteState() 
) {
    CastUtils.verifyMainThread()
    
    MediaRouteButtonContainer {
        IconButton(
            onClick = { state.showDialog() }, 
            modifier = modifier
        ) { 
            mediaRouteButtonIcon() 
        }

        if (state.isShowingDialog) {
            MediaRouteDialog(onDismissRequest = { state.dismissDialog() })
        }
    }
}

@Stable
class MediaRouteState {
    var isShowingDialog by mutableStateOf(false)
        private set

    internal fun showDialog() {
        isShowingDialog = true
    }

    internal fun dismissDialog() {
        isShowingDialog = false
    }
}

@Composable
fun rememberMediaRouteState(): MediaRouteState {
    return remember { MediaRouteState() }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions