11package com.google.android.gms.example.jetpackcomposedemo
22
3+ import android.content.Context
4+ import android.content.ContextWrapper
5+ import androidx.activity.ComponentActivity
36import androidx.compose.foundation.layout.Column
47import androidx.compose.foundation.layout.Spacer
58import androidx.compose.foundation.layout.WindowInsets
@@ -13,6 +16,7 @@ import androidx.compose.material.icons.Icons
1316import androidx.compose.material.icons.automirrored.filled.ArrowBack
1417import androidx.compose.material.icons.filled.MoreVert
1518import androidx.compose.material3.DropdownMenu
19+ import androidx.compose.material3.DropdownMenuItem
1620import androidx.compose.material3.ExperimentalMaterial3Api
1721import androidx.compose.material3.Icon
1822import androidx.compose.material3.IconButton
@@ -22,25 +26,51 @@ import androidx.compose.material3.Surface
2226import androidx.compose.material3.Text
2327import androidx.compose.material3.TopAppBar
2428import androidx.compose.runtime.Composable
29+ import androidx.compose.runtime.LaunchedEffect
30+ import androidx.compose.runtime.collectAsState
2531import androidx.compose.runtime.getValue
2632import androidx.compose.runtime.mutableStateOf
2733import androidx.compose.runtime.remember
2834import androidx.compose.runtime.setValue
2935import androidx.compose.ui.Modifier
3036import androidx.compose.ui.platform.LocalContext
3137import androidx.compose.ui.tooling.preview.Preview
32- import androidx.navigation.NavHostController
3338import androidx.navigation.compose.NavHost
3439import androidx.navigation.compose.composable
3540import androidx.navigation.compose.rememberNavController
3641import com.example.jetpackcomposedemo.R
3742import com.google.android.gms.example.jetpackcomposedemo.ui.theme.JetpackComposeDemoTheme
3843
3944@Composable
40- fun MainScreen () {
45+ fun MainScreen (googleMobileAdsViewModel : MainViewModel , modifier : Modifier = Modifier ) {
46+ val context = LocalContext .current
47+ val activity = context.getActivity()
4148 val navController = rememberNavController()
49+ val uiState by googleMobileAdsViewModel.uiState.collectAsState()
50+ var showNavigationIcon by remember { mutableStateOf(false ) }
51+
52+ LaunchedEffect (navController) {
53+ navController.addOnDestinationChangedListener { _, destination, _ ->
54+ showNavigationIcon = destination.route != NavDestinations .Home .name
55+ }
56+ }
57+
4258 Scaffold (
43- topBar = { MainTopBar (navController = navController) },
59+ topBar = {
60+ MainTopBar (
61+ isMobileAdsInitialized = uiState.isMobileAdsInitialized,
62+ isPrivacyOptionsRequired = uiState.isPrivacyOptionsRequired,
63+ isNavigationEnabled = showNavigationIcon,
64+ navigateBack = { navController.popBackStack() },
65+ onOpenAdInspector = { googleMobileAdsViewModel.openAdInspector(context) {} },
66+ onShowPrivacyOptionsForm = {
67+ if (activity != null ) {
68+ googleMobileAdsViewModel.showPrivacyOptionsForm(activity) {}
69+ }
70+ },
71+ modifier,
72+ )
73+ },
4474 contentWindowInsets =
4575 WindowInsets .systemBars.only(WindowInsetsSides .Top + WindowInsetsSides .Horizontal ),
4676 ) { innerPadding ->
@@ -55,14 +85,24 @@ fun MainScreen() {
5585
5686@OptIn(ExperimentalMaterial3Api ::class )
5787@Composable
58- private fun MainTopBar (navController : NavHostController ) {
88+ private fun MainTopBar (
89+ isMobileAdsInitialized : Boolean ,
90+ isPrivacyOptionsRequired : Boolean ,
91+ isNavigationEnabled : Boolean ,
92+ navigateBack : () -> Unit ,
93+ onOpenAdInspector : () -> Unit ,
94+ onShowPrivacyOptionsForm : () -> Unit ,
95+ modifier : Modifier = Modifier ,
96+ ) {
5997 val context = LocalContext .current
6098 var menuExpanded by remember { mutableStateOf(false ) }
99+
61100 TopAppBar (
101+ modifier = modifier,
62102 title = { Text (context.getString(R .string.main_title)) },
63103 navigationIcon = {
64- if (navController.currentBackStackEntry != null ) {
65- IconButton (onClick = { navController.popBackStack() } ) {
104+ if (isNavigationEnabled ) {
105+ IconButton (onClick = navigateBack ) {
66106 Icon (
67107 imageVector = Icons .AutoMirrored .Filled .ArrowBack ,
68108 contentDescription = Icons .AutoMirrored .Filled .ArrowBack .name,
@@ -74,17 +114,36 @@ private fun MainTopBar(navController: NavHostController) {
74114 IconButton (onClick = { menuExpanded = true }) {
75115 Icon (imageVector = Icons .Filled .MoreVert , contentDescription = Icons .Filled .MoreVert .name)
76116 }
77- DropdownMenu (expanded = menuExpanded, onDismissRequest = { menuExpanded = false }) {}
117+ DropdownMenu (expanded = menuExpanded, onDismissRequest = { menuExpanded = false }) {
118+ DropdownMenuItem (
119+ text = { Text (context.getString(R .string.adinspector_open_button)) },
120+ enabled = isMobileAdsInitialized,
121+ onClick = onOpenAdInspector,
122+ )
123+ if (isPrivacyOptionsRequired) {
124+ DropdownMenuItem (
125+ text = { Text (context.getString(R .string.privacy_options_open_button)) },
126+ onClick = onShowPrivacyOptionsForm,
127+ )
128+ }
129+ }
78130 },
79131 )
80132}
81133
134+ private fun Context.getActivity (): ComponentActivity ? =
135+ when (this ) {
136+ is ComponentActivity -> this
137+ is ContextWrapper -> baseContext.getActivity()
138+ else -> null
139+ }
140+
82141@Preview
83142@Composable
84143private fun MainScreenPreview () {
85144 JetpackComposeDemoTheme {
86145 Surface (modifier = Modifier .fillMaxSize(), color = MaterialTheme .colorScheme.background) {
87- MainScreen ()
146+ MainScreen (MainViewModel .getInstance() )
88147 }
89148 }
90149}
0 commit comments