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.topappbar
14+
15+ import androidx.compose.foundation.layout.PaddingValues
16+ import androidx.compose.material3.ExperimentalMaterial3Api
17+ import androidx.compose.runtime.Composable
18+ import androidx.compose.ui.graphics.Color
19+ import androidx.compose.ui.res.painterResource
20+ import androidx.compose.ui.res.stringResource
21+ import androidx.compose.ui.tooling.preview.PreviewLightDark
22+ import com.orange.ouds.app.R
23+ import com.orange.ouds.app.ui.components.Component
24+ import com.orange.ouds.app.ui.components.colorArgument
25+ import com.orange.ouds.app.ui.components.contentDescriptionArgument
26+ import com.orange.ouds.app.ui.components.onClickArgument
27+ import com.orange.ouds.app.ui.components.painterArgument
28+ import com.orange.ouds.app.ui.utilities.Code
29+ import com.orange.ouds.app.ui.utilities.LocalThemeDrawableResources
30+ import com.orange.ouds.app.ui.utilities.ThemeDrawableResources
31+ import com.orange.ouds.app.ui.utilities.composable.AppPreview
32+ import com.orange.ouds.app.ui.utilities.composable.CustomizationFilterChips
33+ import com.orange.ouds.app.ui.utilities.composable.CustomizationSwitchItem
34+ import com.orange.ouds.app.ui.utilities.composable.CustomizationTextField
35+ import com.orange.ouds.app.ui.utilities.composable.DemoScreen
36+ import com.orange.ouds.core.component.OudsCenterAlignedTopAppBar
37+ import com.orange.ouds.core.component.OudsLargeTopAppBar
38+ import com.orange.ouds.core.component.OudsMediumTopAppBar
39+ import com.orange.ouds.core.component.OudsTopAppBar
40+ import com.orange.ouds.core.component.OudsTopAppBarAction
41+ import com.orange.ouds.core.component.OudsTopAppBarNavigationIcon
42+ import com.orange.ouds.core.theme.OudsTheme
43+ import com.orange.ouds.theme.OudsVersion
44+
45+ @Composable
46+ fun TopAppBarDemoScreen () {
47+ val state = rememberTopAppBarDemoState()
48+ val themeDrawableResources = LocalThemeDrawableResources .current
49+ DemoScreen (
50+ description = stringResource(id = Component .TopAppBar .descriptionRes),
51+ bottomSheetContent = { TopAppBarDemoBottomSheetContent (state = state) },
52+ codeSnippet = { topAppBarDemoCodeSnippet(state = state, themeDrawableResources = themeDrawableResources) },
53+ demoContent = { TopAppBarDemoContent (state = state) },
54+ demoContentPaddingValues = PaddingValues (horizontal = OudsTheme .spaces.fixed.none),
55+ version = OudsVersion .Component .NavigationBar
56+ )
57+ }
58+
59+ @Composable
60+ private fun TopAppBarDemoBottomSheetContent (state : TopAppBarDemoState ) {
61+ with (state) {
62+ CustomizationFilterChips (
63+ applyTopPadding = false ,
64+ label = stringResource(R .string.app_components_common_size_label),
65+ chipLabels = TopAppBarDemoState .Size .entries.map { stringResource(it.labelRes) },
66+ selectedChipIndex = TopAppBarDemoState .Size .entries.indexOf(size),
67+ onSelectionChange = { id -> size = TopAppBarDemoState .Size .entries[id] }
68+ )
69+ CustomizationSwitchItem (
70+ label = stringResource(R .string.app_components_topAppBar_centerAligned_label),
71+ checked = centerAligned,
72+ onCheckedChange = { centerAligned = it },
73+ enabled = centerAlignedSwitchEnabled
74+ )
75+ CustomizationSwitchItem (
76+ label = stringResource(R .string.app_components_topAppBar_navigationIcon_label),
77+ checked = navigationIcon,
78+ onCheckedChange = { navigationIcon = it }
79+ )
80+ CustomizationTextField (
81+ label = stringResource(R .string.app_components_topAppBar_title_label),
82+ value = title,
83+ onValueChange = { value -> title = value }
84+ )
85+ CustomizationFilterChips (
86+ applyTopPadding = true ,
87+ label = stringResource(R .string.app_components_topAppBar_avatar_label),
88+ chipLabels = TopAppBarDemoState .Avatar .entries.map { stringResource(it.labelRes) },
89+ selectedChipIndex = TopAppBarDemoState .Avatar .entries.indexOf(avatar),
90+ onSelectionChange = { id -> avatar = TopAppBarDemoState .Avatar .entries[id] }
91+ )
92+ CustomizationTextField (
93+ label = stringResource(R .string.app_components_topAppBar_avatarMonogram_label),
94+ value = avatarMonogram,
95+ onValueChange = { value -> avatarMonogram = value },
96+ enabled = avatarMonogramTextFieldEnabled
97+ )
98+ }
99+ }
100+
101+ @OptIn(ExperimentalMaterial3Api ::class )
102+ @Composable
103+ private fun TopAppBarDemoContent (state : TopAppBarDemoState ) {
104+ with (state) {
105+ val navigationIcon = if (navigationIcon) OudsTopAppBarNavigationIcon .Back {} else null
106+ val avatarContentDescription = stringResource(R .string.app_components_topAppBar_secondAction_a11y)
107+ val avatarAction = when (avatar) {
108+ TopAppBarDemoState .Avatar .Image -> OudsTopAppBarAction .Avatar (
109+ painter = painterResource(id = R .drawable.il_top_app_bar_avatar),
110+ contentDescription = avatarContentDescription,
111+ onClick = {}
112+ )
113+ TopAppBarDemoState .Avatar .Monogram -> OudsTopAppBarAction .Avatar (
114+ monogram = avatarMonogram,
115+ color = Color .White ,
116+ backgroundColor = Color (0xff138126 ),
117+ contentDescription = avatarContentDescription,
118+ onClick = {}
119+ )
120+ }
121+ val actions = listOf (
122+ OudsTopAppBarAction .Icon (
123+ painter = painterResource(id = LocalThemeDrawableResources .current.tipsAndTricks),
124+ contentDescription = stringResource(R .string.app_components_topAppBar_firstAction_a11y),
125+ onClick = {}
126+ ),
127+ avatarAction
128+ )
129+ when (size) {
130+ TopAppBarDemoState .Size .Small -> {
131+ if (centerAligned) {
132+ OudsCenterAlignedTopAppBar (
133+ title = title,
134+ navigationIcon = navigationIcon,
135+ actions = actions
136+ )
137+ } else {
138+ OudsTopAppBar (
139+ title = title,
140+ navigationIcon = navigationIcon,
141+ actions = actions
142+ )
143+ }
144+ }
145+ TopAppBarDemoState .Size .Medium -> {
146+ OudsMediumTopAppBar (
147+ title = title,
148+ navigationIcon = navigationIcon,
149+ actions = actions
150+ )
151+ }
152+ TopAppBarDemoState .Size .Large -> {
153+ OudsLargeTopAppBar (
154+ title = title,
155+ navigationIcon = navigationIcon,
156+ actions = actions
157+ )
158+ }
159+ }
160+ }
161+ }
162+
163+ private fun Code.Builder.topAppBarDemoCodeSnippet (state : TopAppBarDemoState , themeDrawableResources : ThemeDrawableResources ) {
164+ with (state) {
165+ val functionName = when (size) {
166+ TopAppBarDemoState .Size .Small -> if (centerAligned) " OudsCenterAlignedTopAppBar" else " OudsTopAppBar"
167+ TopAppBarDemoState .Size .Medium -> " OudsMediumTopAppBar"
168+ TopAppBarDemoState .Size .Large -> " OudsLargeTopAppBar"
169+ }
170+ functionCall(functionName) {
171+ typedArgument(" title" , title)
172+ if (navigationIcon) {
173+ constructorCallArgument<OudsTopAppBarNavigationIcon .Back >(" navigationIcon" ) {
174+ trailingLambda = true
175+ onClickArgument()
176+ }
177+ }
178+ functionCallArgument(" actions" , " listOf" ) {
179+ constructorCallArgument<OudsTopAppBarAction .Icon >(null ) {
180+ painterArgument(themeDrawableResources.tipsAndTricks)
181+ contentDescriptionArgument(R .string.app_components_topAppBar_firstAction_a11y)
182+ onClickArgument()
183+ }
184+ constructorCallArgument<OudsTopAppBarAction .Avatar >(null ) {
185+ when (avatar) {
186+ TopAppBarDemoState .Avatar .Image -> {
187+ painterArgument(R .drawable.il_top_app_bar_avatar)
188+ }
189+ TopAppBarDemoState .Avatar .Monogram -> {
190+ typedArgument(" monogram" , avatarMonogram)
191+ colorArgument(" color" , Color .White )
192+ colorArgument(" backgroundColor" , Color (0xff138126 ))
193+ }
194+ }
195+ contentDescriptionArgument(R .string.app_components_topAppBar_secondAction_a11y)
196+ onClickArgument()
197+ }
198+ }
199+ }
200+ }
201+ }
202+
203+ @PreviewLightDark
204+ @Composable
205+ private fun PreviewTopAppBarDemoScreen () = AppPreview {
206+ TopAppBarDemoScreen ()
207+ }
0 commit comments