1+ /*
2+ * Software Name: OUDS Android
3+ * SPDX-FileCopyrightText: Copyright (c) Orange SA
4+ * SPDX-License-Identifier: MIT
5+ *
6+ * This software is distributed under the MIT license,
7+ * the text of which is available at https://opensource.org/license/MIT/
8+ * or see the "LICENSE" file for more details.
9+ *
10+ * Software description: Android library of reusable graphical components
11+ */
12+
13+ package com.orange.ouds.app.ui.components.navigationbar
14+
15+ import android.content.Context
16+ import androidx.annotation.DrawableRes
17+ import androidx.annotation.StringRes
18+ import androidx.compose.foundation.layout.PaddingValues
19+ import androidx.compose.foundation.layout.padding
20+ import androidx.compose.runtime.Composable
21+ import androidx.compose.runtime.remember
22+ import androidx.compose.ui.Modifier
23+ import androidx.compose.ui.platform.LocalContext
24+ import androidx.compose.ui.res.painterResource
25+ import androidx.compose.ui.res.stringResource
26+ import androidx.compose.ui.tooling.preview.PreviewLightDark
27+ import com.orange.ouds.app.R
28+ import com.orange.ouds.app.ui.components.Component
29+ import com.orange.ouds.app.ui.components.labelArgument
30+ import com.orange.ouds.app.ui.components.navigationbar.NavigationBarDemoState.Companion.MaxNavigationBarItemCount
31+ import com.orange.ouds.app.ui.components.navigationbar.NavigationBarDemoState.Companion.MinNavigationBarItemCount
32+ import com.orange.ouds.app.ui.components.painterArgument
33+ import com.orange.ouds.app.ui.utilities.Code
34+ import com.orange.ouds.app.ui.utilities.composable.CustomizationChoiceChips
35+ import com.orange.ouds.app.ui.utilities.composable.CustomizationSwitchItem
36+ import com.orange.ouds.app.ui.utilities.composable.DemoScreen
37+ import com.orange.ouds.core.component.OudsNavigationBar
38+ import com.orange.ouds.core.component.OudsNavigationBarItem
39+ import com.orange.ouds.core.theme.OudsTheme
40+ import com.orange.ouds.core.utilities.OudsPreview
41+
42+ @Composable
43+ fun NavigationBarDemoScreen () {
44+ val state = rememberNavigationBarDemoState()
45+ val context = LocalContext .current
46+ DemoScreen (
47+ description = stringResource(id = Component .NavigationBar .descriptionRes),
48+ bottomSheetContent = { NavigationBarDemoBottomSheetContent (state = state) },
49+ codeSnippet = { navigationBarDemoCodeSnippet(state = state, context = context) },
50+ demoContent = { NavigationBarDemoContent (state = state) },
51+ demoContentPaddingValues = PaddingValues (horizontal = OudsTheme .spaces.fixed.none)
52+ )
53+ }
54+
55+ @Composable
56+ private fun NavigationBarDemoBottomSheetContent (state : NavigationBarDemoState ) {
57+ with (state) {
58+ val itemCountOptions = remember { (MinNavigationBarItemCount .. MaxNavigationBarItemCount ).toList() }
59+ CustomizationChoiceChips (
60+ modifier = Modifier .padding(top = OudsTheme .spaces.fixed.medium),
61+ label = stringResource(R .string.app_components_navigationBar_itemCount_label),
62+ chipsLabels = itemCountOptions.map { it.toString() },
63+ selectedChipIndex = itemCountOptions.indexOf(itemCount),
64+ onSelectionChange = { id -> itemCount = itemCountOptions[id] }
65+ )
66+ CustomizationSwitchItem (
67+ label = stringResource(R .string.app_components_navigationBar_alwaysShowLabel_label),
68+ checked = alwaysShowLabel,
69+ onCheckedChange = { alwaysShowLabel = it },
70+ )
71+ CustomizationSwitchItem (
72+ label = stringResource(R .string.app_components_navigationBar_lastItemEnabled_label),
73+ checked = lastItemEnabled,
74+ onCheckedChange = { checked ->
75+ // If the last item is selected when the last item is disabled, select the first item
76+ if (! checked && selectedItemId == itemCount - 1 ) {
77+ selectedItemId = 0
78+ }
79+ lastItemEnabled = checked
80+ },
81+ )
82+ }
83+ }
84+
85+ @Composable
86+ private fun NavigationBarDemoContent (state : NavigationBarDemoState ) {
87+ val navigationBarItems = NavigationBarItem .entries
88+
89+ with (state) {
90+ OudsNavigationBar {
91+ navigationBarItems.take(itemCount).forEachIndexed { index, item ->
92+ val label = stringResource(id = item.labelRes)
93+ val isLastItem = index == itemCount - 1
94+ OudsNavigationBarItem (
95+ modifier = Modifier .weight(1f ),
96+ selected = selectedItemId == index,
97+ onClick = { selectedItemId = index },
98+ label = label,
99+ icon = OudsNavigationBarItem .Icon (
100+ painter = painterResource(id = item.iconRes),
101+ contentDescription = label
102+ ),
103+ alwaysShowLabel = alwaysShowLabel,
104+ enabled = ! (isLastItem && ! lastItemEnabled)
105+ )
106+ }
107+ }
108+ }
109+ }
110+
111+ private fun Code.Builder.navigationBarDemoCodeSnippet (state : NavigationBarDemoState , context : Context ) {
112+ functionCall(OudsNavigationBarItem ::class .simpleName.orEmpty()) {
113+ functionCallArgument(" items" , " listOf" ) {
114+ NavigationBarItem .entries.take(state.itemCount).forEach { item ->
115+ val label = context.resources.getString(item.labelRes)
116+ functionCallArgument(null , OudsNavigationBarItem ::class .simpleName.orEmpty()) {
117+ typedArgument(" selected" , item == NavigationBarItem .Home )
118+ lambdaArgument(" onClick" , {})
119+ labelArgument(label)
120+ functionCallArgument(null , OudsNavigationBarItem .Icon ::class .simpleName.orEmpty()) {
121+ painterArgument(id = item.iconRes)
122+ }
123+ }
124+ }
125+ }
126+ }
127+ }
128+
129+ enum class NavigationBarItem (@DrawableRes val iconRes : Int , @StringRes val labelRes : Int ) {
130+ Home (R .drawable.ic_home, R .string.app_components_navigationBar_homeItem_label),
131+ Shop (R .drawable.ic_shop_store, R .string.app_components_navigationBar_shopItem_label),
132+ Notification (R .drawable.ic_notification_alert, R .string.app_components_navigationBar_notificationItem_label),
133+ Account (R .drawable.ic_avatar, R .string.app_components_navigationBar_accountItem_label),
134+ Settings (R .drawable.ic_settings, R .string.app_components_navigationBar_settingsItem_label),
135+ }
136+
137+ @PreviewLightDark
138+ @Composable
139+ private fun PreviewNavigationBarDemoScreen () = OudsPreview {
140+ NavigationBarDemoScreen ()
141+ }
0 commit comments