-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #414 from Orange-OpenSource/111-ods-component-menu
111 - ods component - menu
- Loading branch information
Showing
36 changed files
with
650 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
demo/src/main/java/com/orange/ods/demo/ui/components/menus/ComponentMenu.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* | ||
* Copyright 2021 Orange | ||
* | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT. | ||
* / | ||
*/ | ||
|
||
package com.orange.ods.demo.ui.components.menus | ||
|
||
import androidx.compose.runtime.Composable | ||
import com.orange.ods.demo.ui.components.Variant | ||
|
||
@Composable | ||
fun ComponentMenu(variant: Variant) { | ||
when (variant) { | ||
Variant.DropdownMenu -> MenuDropdown() | ||
Variant.ExposedDropdownMenu -> MenuExposedDropdown() | ||
else -> {} | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
demo/src/main/java/com/orange/ods/demo/ui/components/menus/MenuDropdown.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* | ||
* Copyright 2021 Orange | ||
* | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT. | ||
* / | ||
*/ | ||
|
||
package com.orange.ods.demo.ui.components.menus | ||
|
||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.verticalScroll | ||
import androidx.compose.material.ExperimentalMaterialApi | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.MoreVert | ||
import androidx.compose.material.rememberBottomSheetScaffoldState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.vector.rememberVectorPainter | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.dimensionResource | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.DpOffset | ||
import androidx.compose.ui.unit.dp | ||
import com.orange.ods.compose.component.divider.OdsDivider | ||
import com.orange.ods.compose.component.list.OdsIconTrailing | ||
import com.orange.ods.compose.component.list.OdsListItem | ||
import com.orange.ods.compose.component.list.OdsSwitchTrailing | ||
import com.orange.ods.compose.component.menu.OdsDropdownMenu | ||
import com.orange.ods.compose.component.menu.OdsDropdownMenuItem | ||
import com.orange.ods.compose.text.OdsTextBody1 | ||
import com.orange.ods.demo.R | ||
import com.orange.ods.demo.domain.recipes.LocalRecipes | ||
import com.orange.ods.demo.ui.components.utilities.ComponentCustomizationBottomSheetScaffold | ||
import com.orange.ods.demo.ui.components.utilities.clickOnElement | ||
|
||
@OptIn(ExperimentalMaterialApi::class) | ||
@Composable | ||
fun MenuDropdown() { | ||
val customizationState = rememberMenuDropdownCustomizationState() | ||
|
||
var menuExpanded by remember { mutableStateOf(false) } | ||
val context = LocalContext.current | ||
val recipes = LocalRecipes.current | ||
val recipe = rememberSaveable { recipes.filter { it.ingredients.isNotEmpty() }.random() } | ||
|
||
with(customizationState) { | ||
ComponentCustomizationBottomSheetScaffold( | ||
bottomSheetScaffoldState = rememberBottomSheetScaffoldState(), | ||
bottomSheetContent = { | ||
OdsListItem( | ||
text = stringResource(id = R.string.component_menu_icons), | ||
trailing = OdsSwitchTrailing(checked = icons) | ||
) | ||
OdsListItem( | ||
text = stringResource(id = R.string.component_menu_divider), | ||
trailing = OdsSwitchTrailing(checked = dividerExample) | ||
) | ||
}) { | ||
Column( | ||
modifier = Modifier | ||
.verticalScroll(rememberScrollState()) | ||
.padding(top = dimensionResource(id = R.dimen.screen_vertical_margin), bottom = dimensionResource(id = R.dimen.spacing_s)) | ||
) { | ||
OdsTextBody1( | ||
modifier = Modifier | ||
.padding(horizontal = dimensionResource(id = R.dimen.screen_horizontal_margin)), | ||
text = stringResource(id = R.string.component_menu_dropdown_description) | ||
) | ||
|
||
Box(modifier = Modifier.padding(top = dimensionResource(id = R.dimen.spacing_s))) { | ||
OdsListItem( | ||
modifier = Modifier.padding(top = dimensionResource(id = R.dimen.spacing_s)), | ||
text = recipe.title, | ||
secondaryText = recipe.subtitle, | ||
trailing = OdsIconTrailing( | ||
modifier = Modifier.clickable { menuExpanded = true }, | ||
painter = rememberVectorPainter(image = Icons.Filled.MoreVert), | ||
contentDescription = stringResource(id = R.string.component_menu_show_ingredients), | ||
) | ||
) | ||
OdsDropdownMenu(expanded = menuExpanded, onDismissRequest = { menuExpanded = false }, offset = DpOffset(x = (-100).dp, y = (-10).dp)) { | ||
recipes.take(MenuDropdownCustomizationState.MenuItemCount).forEachIndexed { index, recipe -> | ||
OdsDropdownMenuItem( | ||
text = recipe.title, | ||
icon = if (hasIcons && recipe.iconResId != null) painterResource(id = recipe.iconResId) else null, | ||
onClick = { clickOnElement(context, recipe.title) } | ||
) | ||
if (hasDividerExample && index == 2) { | ||
OdsDivider() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
demo/src/main/java/com/orange/ods/demo/ui/components/menus/MenuDropdownCustomizationState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* | ||
* Copyright 2021 Orange | ||
* | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT. | ||
* / | ||
*/ | ||
|
||
package com.orange.ods.demo.ui.components.menus | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
|
||
@Composable | ||
fun rememberMenuDropdownCustomizationState( | ||
icons: MutableState<Boolean> = rememberSaveable { mutableStateOf(false) }, | ||
dividerExample: MutableState<Boolean> = rememberSaveable { mutableStateOf(false) }, | ||
enabled: MutableState<Boolean> = rememberSaveable { mutableStateOf(true) } | ||
) = | ||
remember(icons, dividerExample, enabled) { | ||
MenuDropdownCustomizationState(icons, dividerExample, enabled) | ||
} | ||
|
||
class MenuDropdownCustomizationState( | ||
val icons: MutableState<Boolean>, | ||
val dividerExample: MutableState<Boolean>, | ||
val enabled: MutableState<Boolean> | ||
) { | ||
companion object { | ||
const val MenuItemCount = 5 | ||
} | ||
|
||
val hasIcons | ||
get() = icons.value | ||
|
||
val hasDividerExample | ||
get() = dividerExample.value | ||
|
||
val isEnabled | ||
get() = enabled.value | ||
} |
113 changes: 113 additions & 0 deletions
113
demo/src/main/java/com/orange/ods/demo/ui/components/menus/MenuExposedDropdown.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* | ||
* Copyright 2021 Orange | ||
* | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT. | ||
* / | ||
*/ | ||
|
||
package com.orange.ods.demo.ui.components.menus | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.verticalScroll | ||
import androidx.compose.material.ExperimentalMaterialApi | ||
import androidx.compose.material.rememberBottomSheetScaffoldState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.saveable.mapSaver | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.dimensionResource | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import com.orange.ods.compose.component.list.OdsListItem | ||
import com.orange.ods.compose.component.list.OdsSwitchTrailing | ||
import com.orange.ods.compose.component.menu.OdsExposedDropdownMenu | ||
import com.orange.ods.compose.component.menu.OdsExposedDropdownMenuItem | ||
import com.orange.ods.demo.R | ||
import com.orange.ods.demo.domain.recipes.LocalRecipes | ||
import com.orange.ods.demo.ui.components.utilities.ComponentCustomizationBottomSheetScaffold | ||
import com.orange.ods.demo.ui.components.utilities.clickOnElement | ||
|
||
@OptIn(ExperimentalMaterialApi::class) | ||
@Composable | ||
fun MenuExposedDropdown() { | ||
val customizationState = rememberMenuDropdownCustomizationState() | ||
|
||
val context = LocalContext.current | ||
val recipes = LocalRecipes.current.take(4) | ||
|
||
val dropdownItems = recipes.map { recipe -> | ||
OdsExposedDropdownMenuItem(label = recipe.title, icon = recipe.iconResId?.let { painterResource(id = it) }) | ||
} | ||
val textOnlyDropdownItems = recipes.map { recipe -> | ||
OdsExposedDropdownMenuItem(label = recipe.title) | ||
} | ||
|
||
var items by remember { mutableStateOf(dropdownItems) } | ||
val itemSaver = run { | ||
val itemKey = "item" | ||
mapSaver( | ||
save = { | ||
mapOf(itemKey to it.label) | ||
}, | ||
restore = { | ||
OdsExposedDropdownMenuItem(it[itemKey] as String) | ||
} | ||
) | ||
} | ||
|
||
with(customizationState) { | ||
val selectedItem: MutableState<OdsExposedDropdownMenuItem> = rememberSaveable(stateSaver = itemSaver) { mutableStateOf(dropdownItems.first()) } | ||
if (hasIcons) { | ||
items = dropdownItems | ||
selectedItem.value = dropdownItems.first { selectedItem.value.label == it.label } | ||
} else { | ||
items = textOnlyDropdownItems | ||
selectedItem.value = textOnlyDropdownItems.first { selectedItem.value.label == it.label } | ||
} | ||
|
||
ComponentCustomizationBottomSheetScaffold( | ||
bottomSheetScaffoldState = rememberBottomSheetScaffoldState(), | ||
bottomSheetContent = { | ||
OdsListItem( | ||
text = stringResource(id = R.string.component_state_enabled), | ||
trailing = OdsSwitchTrailing(checked = enabled) | ||
) | ||
OdsListItem( | ||
text = stringResource(id = R.string.component_menu_icons), | ||
trailing = OdsSwitchTrailing(checked = icons) | ||
) | ||
}) { | ||
Column( | ||
modifier = Modifier | ||
.verticalScroll(rememberScrollState()) | ||
.padding(top = dimensionResource(id = R.dimen.screen_vertical_margin), bottom = dimensionResource(id = R.dimen.spacing_s)) | ||
.padding(horizontal = dimensionResource(id = R.dimen.screen_horizontal_margin)), | ||
) { | ||
OdsExposedDropdownMenu( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(top = dimensionResource(id = R.dimen.spacing_s)), | ||
label = stringResource(id = R.string.data_recipe), | ||
items = items, | ||
selectedItem = selectedItem, | ||
onItemSelectionChange = { item -> | ||
clickOnElement(context, item.label) | ||
}, | ||
enabled = isEnabled | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.