Skip to content

Commit

Permalink
Add a menu to change environment in "Lists" on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 committed Dec 5, 2023
1 parent b1a871b commit 0a0775b
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ package ch.srgssr.pillarbox.demo.ui
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.MenuDefaults
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
Expand All @@ -18,10 +24,14 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.content.res.ResourcesCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
Expand All @@ -42,7 +52,9 @@ import androidx.navigation.navigation
import ch.srg.dataProvider.integrationlayer.request.image.ImageWidth
import ch.srg.dataProvider.integrationlayer.request.image.decorated
import ch.srgssr.pillarbox.analytics.SRGAnalytics
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlHost
import ch.srgssr.pillarbox.demo.DemoPageView
import ch.srgssr.pillarbox.demo.R
import ch.srgssr.pillarbox.demo.shared.data.DemoItem
import ch.srgssr.pillarbox.demo.shared.di.PlayerModule
import ch.srgssr.pillarbox.demo.shared.ui.HomeDestination
Expand All @@ -54,10 +66,12 @@ import ch.srgssr.pillarbox.demo.ui.integrationLayer.SearchView
import ch.srgssr.pillarbox.demo.ui.integrationLayer.listNavGraph
import ch.srgssr.pillarbox.demo.ui.player.SimplePlayerActivity
import ch.srgssr.pillarbox.demo.ui.showcases.showCasesNavGraph
import ch.srgssr.pillarbox.demo.ui.theme.PillarboxTheme
import java.net.URL

private val bottomNavItems = listOf(HomeDestination.Examples, HomeDestination.ShowCases, HomeDestination.Lists, HomeDestination.Search)
private val topLevelRoutes =
listOf(HomeDestination.Examples.route, NavigationRoutes.showcaseList, NavigationRoutes.contentLists, HomeDestination.Search)
listOf(HomeDestination.Examples.route, NavigationRoutes.showcaseList, NavigationRoutes.contentLists, HomeDestination.Search.route)

/**
* Main view with all the navigation
Expand All @@ -69,6 +83,9 @@ fun MainNavigation() {
val navController = rememberNavController()
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination

var ilHost by remember { mutableStateOf(IlHost.DEFAULT) }

Scaffold(
topBar = {
TopAppBar(
Expand All @@ -88,6 +105,14 @@ fun MainNavigation() {
}
}
}
},
actions = {
if (currentDestination?.route == NavigationRoutes.contentLists) {
ListsMenu(
currentServer = ilHost,
onServerSelected = { ilHost = it }
)
}
}
)
},
Expand All @@ -96,9 +121,7 @@ fun MainNavigation() {
}
) { innerPadding ->
val context = LocalContext.current
val ilRepository = remember {
PlayerModule.createIlRepository(context)
}

NavHost(navController = navController, startDestination = HomeDestination.Examples.route, modifier = Modifier.padding(innerPadding)) {
composable(HomeDestination.Examples.route, DemoPageView("home", listOf("app", "pillarbox", "examples"))) {
ExamplesHome()
Expand All @@ -109,6 +132,8 @@ fun MainNavigation() {
}

navigation(startDestination = NavigationRoutes.contentLists, route = HomeDestination.Lists.route) {
val ilRepository = PlayerModule.createIlRepository(context, ilHost)

listNavGraph(navController, ilRepository)
}

Expand All @@ -117,6 +142,7 @@ fun MainNavigation() {
}

composable(route = NavigationRoutes.searchHome, DemoPageView("home", listOf("app", "pillarbox", "search"))) {
val ilRepository = PlayerModule.createIlRepository(context)
val viewModel: SearchViewModel = viewModel(factory = SearchViewModel.Factory(ilRepository))
SearchView(searchViewModel = viewModel) {
val item = DemoItem(
Expand All @@ -133,6 +159,61 @@ fun MainNavigation() {
}
}

@Composable
private fun ListsMenu(
currentServer: URL,
onServerSelected: (server: URL) -> Unit
) {
var isMenuVisible by remember { mutableStateOf(false) }

IconButton(onClick = { isMenuVisible = true }) {
Icon(
imageVector = Icons.Default.Settings,
contentDescription = stringResource(R.string.server)
)
}

DropdownMenu(
expanded = isMenuVisible,
onDismissRequest = { isMenuVisible = false }
) {
val currentServerUrl = currentServer.toString()
val servers = mapOf(
stringResource(R.string.production) to IlHost.PROD.toString(),
stringResource(R.string.stage) to IlHost.STAGE.toString(),
stringResource(R.string.test) to IlHost.TEST.toString()
)

Text(
text = stringResource(R.string.server),
modifier = Modifier
.padding(MenuDefaults.DropdownMenuItemContentPadding)
.align(Alignment.CenterHorizontally),
style = MaterialTheme.typography.labelMedium
)

servers.forEach { (name, url) ->
DropdownMenuItem(
text = { Text(text = name) },
onClick = {
onServerSelected(URL(url))
isMenuVisible = false
},
trailingIcon = if (currentServerUrl == url) {
{
Icon(
imageVector = Icons.Default.Check,
contentDescription = null
)
}
} else {
null
}
)
}
}
}

@Composable
private fun DemoBottomNavigation(navController: NavController, currentDestination: NavDestination?) {
NavigationBar {
Expand Down Expand Up @@ -163,6 +244,28 @@ private fun DemoBottomNavigation(navController: NavController, currentDestinatio
}
}

@Composable
@Preview(showBackground = true)
private fun ListsMenuPreview() {
PillarboxTheme {
ListsMenu(
currentServer = IlHost.PROD,
onServerSelected = {}
)
}
}

@Composable
@Preview(showBackground = true)
private fun DemoBottomNavigationPreview() {
PillarboxTheme {
DemoBottomNavigation(
navController = rememberNavController(),
currentDestination = null
)
}
}

private fun NavDestination.getLabelResId(): Int {
val routes = hierarchy.map { it.route }
val navItem: HomeDestination? = bottomNavItems.firstOrNull { it.route in routes }
Expand Down
4 changes: 4 additions & 0 deletions pillarbox-demo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
<string name="clear">Clear</string>
<string name="licence_url">License URL</string>
<string name="play">Play</string>
<string name="server">Server</string>
<string name="production">Production</string>
<string name="stage">Stage</string>
<string name="test">Test</string>
</resources>

0 comments on commit 0a0775b

Please sign in to comment.