Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

545 - Add OdsExposedDropdownMenuItem saver in the library #547

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ 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.*
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,11 @@ 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.*
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.app.R
import com.orange.ods.app.domain.recipes.LocalRecipes
Expand All @@ -51,27 +44,16 @@ fun MenuExposedDropdown() {
val recipes = LocalRecipes.current.take(4)

val dropdownItems = recipes.map { recipe ->
OdsExposedDropdownMenuItem(label = recipe.title, icon = recipe.iconResId?.let { painterResource(id = it) })
OdsExposedDropdownMenuItem(label = recipe.title, iconResId = recipe.iconResId)
}
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()) }
val selectedItem: MutableState<OdsExposedDropdownMenuItem> = rememberSaveable { mutableStateOf(dropdownItems.first()) }
if (hasIcons) {
items = dropdownItems
selectedItem.value = dropdownItems.first { selectedItem.value.label == it.label }
Expand Down Expand Up @@ -122,7 +104,7 @@ fun MenuExposedDropdown() {
items.forEach { item ->
classInstance(OdsExposedDropdownMenuItem::class.java) {
string("label", item.label)
if (hasIcons) icon()
if (hasIcons) simple("iconResId", "<drawable id>")
}
}
}
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- \[App\] Add component image item ([#473](https://github.com/Orange-OpenSource/ods-android/issues/473))
- \[Lib\] Add `OdsImageItem` component ([#473](https://github.com/Orange-OpenSource/ods-android/issues/473))
- \[Lib\] Add `@Parcelize` annotation on `OdsExposedDropdownMenuItem` to allow to save and restore it ([#545](https://github.com/Orange-OpenSource/ods-android/issues/545))

## [0.13.0](https://github.com/Orange-OpenSource/ods-android/compare/0.12.0...0.13.0) - 2023-06-01

Expand Down
22 changes: 11 additions & 11 deletions docs/components/Menus.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ description: Menus appear from a button, action, or other control. It contains a
* [Specifications references](#specifications-references)
* [Accessibility](#accessibility)
* [Variants](#variants)
* [Dropdown menu](#dropdown-menu)
* [Exposed dropdown menu](#exposed-dropdown-menu)
* [Dropdown menu](#dropdown-menu)
* [Exposed dropdown menu](#exposed-dropdown-menu)
* [Component specific tokens](#component-specific-tokens)

---
Expand Down Expand Up @@ -43,22 +43,22 @@ The library offers an `OdsDropdownMenu` container composable in which you can ad
var menuExpanded by remember { mutableStateOf(false) }

OdsDropdownMenu(
expanded = menuExpanded,
onDismissRequest = { menuExpanded = false },
expanded = menuExpanded,
onDismissRequest = { menuExpanded = false },
offset = DpOffset(x = (-100).dp, y = (-10).dp)
) {
OdsDropdownMenuItem(
text = "Summer salad",
icon = painterResource(id = R.drawable.ic_salad),
onClick = {
onClick = {
// Do something
}
}
)
OdsDivider() // Allow to add a divider between the 2 items
OdsDropdownMenuItem(
text = "Brocoli soup",
icon = painterResource(id = R.drawable.ic_soup),
onClick = {
onClick = {
// Do something
}
)
Expand All @@ -81,11 +81,11 @@ To display an exposed dropdown menu, you can use the `OdsExposedDropdownMenu` co

```kotlin
val items = listOf(
OdsExposedDropdownMenuItem("Email", painterResource(id = android.R.drawable.ic_dialog_email)),
OdsExposedDropdownMenuItem("Map", painterResource(id = android.R.drawable.ic_dialog_map)),
OdsExposedDropdownMenuItem("Dialer", painterResource(id = android.R.drawable.ic_dialog_dialer)),
OdsExposedDropdownMenuItem("Email", android.R.drawable.ic_dialog_email),
OdsExposedDropdownMenuItem("Map", android.R.drawable.ic_dialog_map),
OdsExposedDropdownMenuItem("Dialer", android.R.drawable.ic_dialog_dialer),
)
val selectedItem = remember { mutableStateOf(items.first()) }
val selectedItem = rememberSaveable() { mutableStateOf(items.first()) }

OdsExposedDropdownMenu(
label = "Dropdown menu label",
Expand Down
1 change: 1 addition & 0 deletions lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ plugins {
id("kotlin-android")
id("github")
id("maven-central-publish")
id("kotlin-parcelize")
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@

package com.orange.ods.compose.component.menu

import android.os.Parcelable
import androidx.annotation.DrawableRes
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ExposedDropdownMenuBox
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.setValue
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.PreviewParameter
import com.orange.ods.compose.component.OdsComposable
Expand All @@ -29,6 +25,7 @@ import com.orange.ods.compose.component.textfield.OdsTextField
import com.orange.ods.compose.component.utilities.BasicPreviewParameterProvider
import com.orange.ods.compose.component.utilities.Preview
import com.orange.ods.compose.component.utilities.UiModePreviews
import kotlinx.parcelize.Parcelize

/**
* <a href="https://system.design.orange.com/0c1af118d/p/07a69b-menus/b/862cbb" class="external" target="_blank">ODS menus</a>.
Expand Down Expand Up @@ -67,7 +64,7 @@ fun OdsExposedDropdownMenu(
onValueChange = {},
readOnly = true,
label = label,
leadingIcon = selectedItem.value.icon,
leadingIcon = selectedItem.value.iconResId?.let { painterResource(id = it) },
trailing = OdsExposedDropdownMenuTrailing(expanded = if (enabled) expanded else false, enabled = enabled),
enabled = enabled
)
Expand All @@ -77,7 +74,7 @@ fun OdsExposedDropdownMenu(
onDismissRequest = { expanded = false },
content = {
items.forEach { item ->
OdsDropdownMenuItem(text = item.label, icon = item.icon, onClick = {
OdsDropdownMenuItem(text = item.label, icon = item.iconResId?.let { painterResource(id = it) }, onClick = {
selectedItem.value = item
expanded = false
onItemSelectionChange(item)
Expand All @@ -88,7 +85,8 @@ fun OdsExposedDropdownMenu(
}
}

data class OdsExposedDropdownMenuItem(val label: String, val icon: Painter? = null)
@Parcelize
data class OdsExposedDropdownMenuItem(val label: String, @DrawableRes val iconResId: Int? = null) : Parcelable

/**
* Note: Please use Android Studio preview interactive mode to see the OdsExposedDropdownMenu preview cause expanded is a target state.
Expand All @@ -97,10 +95,10 @@ data class OdsExposedDropdownMenuItem(val label: String, val icon: Painter? = nu
@Composable
private fun PreviewOdsDropdownMenu(@PreviewParameter(OdsDropdownMenuPreviewParameterProvider::class) enabled: Boolean) = Preview {
val items = listOf(
OdsExposedDropdownMenuItem("Email", painterResource(id = android.R.drawable.ic_dialog_email)),
OdsExposedDropdownMenuItem("Map", painterResource(id = android.R.drawable.ic_dialog_map)),
OdsExposedDropdownMenuItem("Dialer", painterResource(id = android.R.drawable.ic_dialog_dialer)),
OdsExposedDropdownMenuItem("Info", painterResource(id = android.R.drawable.ic_dialog_info))
OdsExposedDropdownMenuItem("Email", android.R.drawable.ic_dialog_email),
OdsExposedDropdownMenuItem("Map", android.R.drawable.ic_dialog_map),
OdsExposedDropdownMenuItem("Dialer", android.R.drawable.ic_dialog_dialer),
OdsExposedDropdownMenuItem("Info", android.R.drawable.ic_dialog_info)
)
val selectedItem = remember { mutableStateOf(items.first()) }
OdsExposedDropdownMenu(
Expand Down