Skip to content

Commit

Permalink
[#388] Replace content of lists component demo with recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
florentmaitre committed Dec 21, 2022
1 parent 533e27e commit 007e391
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ object Dependencies {
const val androidGradlePlugin = "com.android.tools.build:gradle:${Versions.androidGradlePlugin}"
const val appCompat = "androidx.appcompat:appcompat:${Versions.appCompat}"
const val browser = "androidx.browser:browser:${Versions.browser}"
const val coil = "io.coil-kt:coil:${Versions.coil}"
const val coilCompose = "io.coil-kt:coil-compose:${Versions.coil}"
const val composeMaterial = "androidx.compose.material:material:${Versions.compose}"
const val composeUi = "androidx.compose.ui:ui:${Versions.compose}"
const val composeUiTooling = "androidx.compose.ui:ui-tooling:${Versions.compose}"
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/com/orange/ods/gradle/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ object Versions {
const val appCompat = "1.5.1"
const val browser = "1.3.0"
const val compose = "1.2.0-rc02"
const val coil = "2.2.2"
const val core = "1.7.0"
const val customViewPoolingContainer = "1.0.0"
const val dataStorePreferences = "1.0.0"
Expand Down
4 changes: 3 additions & 1 deletion demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ android {

kotlinOptions {
jvmTarget = "11"
allWarningsAsErrors = true
allWarningsAsErrors = false
freeCompilerArgs = freeCompilerArgs + "-opt-in=kotlin.RequiresOptIn"
}

Expand Down Expand Up @@ -130,6 +130,8 @@ dependencies {
implementation(Dependencies.hiltAndroid)
kapt(Dependencies.hiltCompiler)
implementation(Dependencies.dataStorePreferences)
implementation(Dependencies.coil)
implementation(Dependencies.coilCompose)

debugImplementation(Dependencies.composeUiTooling)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package com.orange.ods.demo.data.recipes

import com.orange.ods.demo.R
import com.orange.ods.demo.domain.recipes.Ingredient
import com.orange.ods.demo.domain.recipes.Recipe
import org.json.JSONArray
Expand All @@ -28,6 +29,10 @@ class RecipesParser {
private const val Quantity = "quantity"
private const val Name = "name"
private const val Image = "image"
private const val Cafe = "Cafe"
private const val CookingPot = "CookingPot"
private const val IceCream = "IceCream"
private const val Restaurant = "Restaurant"
}

@Throws
Expand All @@ -43,10 +48,11 @@ class RecipesParser {
val description = jsonRecipe.getString(Description)
val url = jsonRecipe.getString(Url)
val iconName = jsonRecipe.getString(IconName)
val iconResId = getIconResId(iconName)
val jsonIngredients = jsonRecipe.getJSONArray(Ingredients)
val ingredients = List(jsonIngredients.length()) { parseIngredient(jsonIngredients.getJSONObject(it)) }

return Recipe(title, subtitle, ingredients, description, url, iconName)
return Recipe(title, subtitle, ingredients, description, url, iconResId)
}

private fun parseIngredient(jsonIngredient: JSONObject): Ingredient {
Expand All @@ -56,4 +62,12 @@ class RecipesParser {

return Ingredient(quantity, name, image)
}

private fun getIconResId(iconName: String) = when (iconName) {
Cafe -> R.drawable.ic_cafe
CookingPot -> R.drawable.ic_cooking_pot
IceCream -> R.drawable.ic_ice_cream
Restaurant -> R.drawable.ic_restaurant
else -> null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@

package com.orange.ods.demo.domain.recipes

import androidx.annotation.DrawableRes
import androidx.compose.runtime.staticCompositionLocalOf

val LocalRecipes = staticCompositionLocalOf<List<Recipe>> { error("CompositionLocal LocalRecipes not present") }

data class Recipe(
val title: String,
val subtitle: String,
val ingredients: List<Ingredient>,
val description: String,
val url: String,
val iconName: String
@DrawableRes val iconResId: Int?
)

data class Ingredient(
Expand Down
2 changes: 2 additions & 0 deletions demo/src/main/java/com/orange/ods/demo/ui/MainScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.orange.ods.compose.text.OdsTextH6
import com.orange.ods.compose.theme.OdsTheme
import com.orange.ods.demo.R
import com.orange.ods.demo.domain.recipes.LocalRecipes
import com.orange.ods.demo.ui.about.addAboutGraph
import com.orange.ods.demo.ui.components.addComponentsGraph
import com.orange.ods.demo.ui.components.tabs.FixedTabRow
Expand Down Expand Up @@ -84,6 +85,7 @@ fun MainScreen(themeConfigurations: Set<OdsThemeConfigurationContract>, mainView
LocalMainTabsManager provides mainState.tabsState,
LocalMainThemeManager provides mainState.themeState,
LocalOdsDemoGuideline provides mainState.themeState.currentThemeConfiguration.demoGuideline,
LocalRecipes provides mainViewModel.recipes
) {
var changeThemeDialogVisible by remember { mutableStateOf(false) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.painter.Painter
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.dp
import coil.compose.rememberAsyncImagePainter
import com.orange.ods.compose.component.chip.OdsChoiceChip
import com.orange.ods.compose.component.chip.OdsChoiceChipsFlowRow
import com.orange.ods.compose.component.control.OdsCheckbox
Expand All @@ -42,6 +44,8 @@ import com.orange.ods.compose.component.list.iconType
import com.orange.ods.compose.text.OdsTextCaption
import com.orange.ods.compose.theme.OdsTheme
import com.orange.ods.demo.R
import com.orange.ods.demo.domain.recipes.LocalRecipes
import com.orange.ods.demo.domain.recipes.Recipe
import com.orange.ods.demo.ui.components.utilities.ComponentCountRow
import com.orange.ods.demo.ui.components.utilities.ComponentCustomizationBottomSheetScaffold
import com.orange.ods.demo.ui.components.utilities.clickOnElement
Expand Down Expand Up @@ -101,25 +105,27 @@ private fun ComponentListsBottomSheetContent(listItemCustomizationState: ListIte

@Composable
private fun ComponentListsContent(listItemCustomizationState: ListItemCustomizationState) {
val recipes = LocalRecipes.current
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
if (!listItemCustomizationState.trailings.contains(listItemCustomizationState.selectedTrailing.value)) {
listItemCustomizationState.resetTrailing()
}

repeat(4) {
OdsListItem(
modifier = Modifier.clickable {}
.let { modifier ->
listItemCustomizationState.iconType?.let { modifier.iconType(it) }.orElse { modifier }
}
.let { if (listItemCustomizationState.dividerEnabled.value) it.divider() else it },
text = stringResource(id = R.string.component_element_title),
secondaryText = listItemCustomizationState.secondaryTextResId?.let { stringResource(id = it) },
singleLineSecondaryText = listItemCustomizationState.lineCount.value == 2,
icon = listItemCustomizationState.iconPainterResId?.let { { OdsListItemIcon(painter = painterResource(it)) } },
trailing = listItemCustomizationState.trailing
)
}
recipes.filter { it.description.isNotBlank() }
.forEach { recipe ->
OdsListItem(
modifier = Modifier.clickable {}
.let { modifier ->
listItemCustomizationState.iconType?.let { modifier.iconType(it) }.orElse { modifier }
}
.let { if (listItemCustomizationState.dividerEnabled.value) it.divider() else it },
text = recipe.title,
secondaryText = listItemCustomizationState.getSecondaryText(recipe),
singleLineSecondaryText = listItemCustomizationState.lineCount.value == 2,
icon = listItemCustomizationState.getIconPainter(recipe)?.let { { OdsListItemIcon(painter = it) } },
trailing = listItemCustomizationState.trailing
)
}
}
}

Expand All @@ -132,12 +138,13 @@ private val ListItemCustomizationState.Trailing.textResId: Int
ListItemCustomizationState.Trailing.Caption -> R.string.component_list_trailing_caption
}

private val ListItemCustomizationState.secondaryTextResId: Int?
get() = when (lineCount.value) {
2 -> R.string.component_element_subtitle
3 -> R.string.component_element_lorem_ipsum
private fun ListItemCustomizationState.getSecondaryText(recipe: Recipe): String? {
return when (lineCount.value) {
2 -> recipe.subtitle
3 -> recipe.description
else -> null
}
}

private val ListItemCustomizationState.iconType: OdsListItemIconType?
get() = when (selectedLeading.value) {
Expand All @@ -148,14 +155,20 @@ private val ListItemCustomizationState.iconType: OdsListItemIconType?
ListItemCustomizationState.Leading.WideImage -> OdsListItemIconType.WideImage
}

private val ListItemCustomizationState.iconPainterResId: Int?
get() = when (selectedLeading.value) {
@Composable
private fun ListItemCustomizationState.getIconPainter(recipe: Recipe): Painter? {
return when (selectedLeading.value) {
ListItemCustomizationState.Leading.None -> null
ListItemCustomizationState.Leading.Icon -> R.drawable.ic_address_book
ListItemCustomizationState.Leading.Icon -> recipe.iconResId?.let { painterResource(id = it) }
ListItemCustomizationState.Leading.CircularImage,
ListItemCustomizationState.Leading.SquareImage,
ListItemCustomizationState.Leading.WideImage -> R.drawable.placeholder
ListItemCustomizationState.Leading.WideImage -> rememberAsyncImagePainter(
model = recipe.url,
placeholder = painterResource(id = R.drawable.placeholder_small),
error = painterResource(id = R.drawable.placeholder_small)
)
}
}

private val ListItemCustomizationState.trailing: (@Composable () -> Unit)?
get() = when (selectedTrailing.value) {
Expand Down
10 changes: 10 additions & 0 deletions demo/src/main/res/drawable/ic_cafe.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="44dp"
android:height="44dp"
android:viewportWidth="44"
android:viewportHeight="44">
<path
android:pathData="M12.823,29.7h18.354a0.16,0.16 0,0 0,0.105 -0.039c1.962,-1.698 3.318,-3.93 3.76,-6.428a0.16,0.16 0,0 1,0.158 -0.133,5.5 5.5,0 0,0 0.153,-10.998 0.158,0.158 0,0 1,-0.153 -0.158v-0.871c0,-0.648 -0.525,-1.173 -1.173,-1.173L15.4,9.9v4.242c0,0.087 0.07,0.158 0.158,0.158h0.958a1.087,1.087 0,0 1,1.084 1.084v4.432a1.087,1.087 0,0 1,-1.084 1.084h-3.332a1.087,1.087 0,0 1,-1.084 -1.084v-4.432a1.087,1.087 0,0 1,1.084 -1.084h0.958c0.087,0 0.158,-0.07 0.158,-0.158L14.3,9.9L9.973,9.9c-0.648,0 -1.173,0.525 -1.173,1.173v9.865a0.998,0.998 0,0 0,0.008 0.137c-0.005,0.125 -0.008,0.25 -0.008,0.375 0,3.212 1.5,6.118 3.918,8.211a0.16,0.16 0,0 0,0.105 0.039zM38.5,17.6a3.3,3.3 0,0 1,-3.135 3.296,0.158 0.158,0 0,1 -0.165,-0.159v-6.274a0.158,0.158 0,0 1,0.165 -0.159A3.3,3.3 0,0 1,38.5 17.6zM14.3,16.5h1.1a1.1,1.1 0,0 1,1.1 1.1v1.1a1.1,1.1 0,0 1,-1.1 1.1h-1.1a1.1,1.1 0,0 1,-1.1 -1.1v-1.1a1.1,1.1 0,0 1,1.1 -1.1zM39.6,30.8L4.4,30.8a1.1,1.1 0,0 0,-1.1 1.1c0,0.26 3.005,1.939 9.902,2.797a0.552,0.552 0,0 0,0.548 0.503h16.5c0.303,0 0.55,-0.247 0.55,-0.55v-0.015c6.461,-0.875 9.9,-2.48 9.9,-2.735a1.1,1.1 0,0 0,-1.1 -1.1z"
android:fillColor="#000"
android:fillType="evenOdd"/>
</vector>
10 changes: 10 additions & 0 deletions demo/src/main/res/drawable/ic_cooking_pot.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="44dp"
android:height="44dp"
android:viewportWidth="44"
android:viewportHeight="44">
<path
android:pathData="M39.05,17.6L4.95,17.6c-0.91,0.003 -1.647,0.74 -1.65,1.65 0.003,0.91 0.74,1.647 1.65,1.65h1.402a0.55,0.55 0,0 1,0.548 0.507l0.725,9.188a0.51,0.51 0,0 1,0.002 0.037c0.01,3.132 2.382,5.668 5.307,5.668h18.132c2.925,0 5.298,-2.536 5.307,-5.668 0,-0.012 0,-0.025 0.002,-0.037l0.725,-9.188a0.55,0.55 0,0 1,0.548 -0.507h1.402a1.655,1.655 0,0 0,1.65 -1.65,1.655 1.655,0 0,0 -1.65,-1.65zM37.726,13.25a9.787,9.787 0,0 1,-1.05 -0.382c-4.016,-1.78 -8.64,-2.852 -13.576,-2.992v-0.542c0,-0.295 0.24,-0.534 0.534,-0.534a0.56,0.56 0,0 0,0.558 -0.455,0.551 0.551,0 0,0 -0.542,-0.645h-3.284a0.56,0.56 0,0 0,-0.558 0.455,0.551 0.551,0 0,0 0.542,0.645 0.55,0.55 0,0 1,0.55 0.55v0.526c-4.936,0.14 -9.56,1.212 -13.576,2.992a9.787,9.787 0,0 1,-1.05 0.382A1.1,1.1 0,0 0,6.6 15.4h30.8a1.1,1.1 0,0 0,0.326 -2.15z"
android:fillColor="#000"
android:fillType="evenOdd"/>
</vector>
10 changes: 10 additions & 0 deletions demo/src/main/res/drawable/ic_ice_cream.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="44dp"
android:height="44dp"
android:viewportWidth="44"
android:viewportHeight="44">
<path
android:pathData="m30.797,24.572 l-0.008,0.007c0.007,0.06 0.01,0.122 0.011,0.183v2.202a1.655,1.655 0,0 1,-1.65 1.65,1.655 1.655,0 0,1 -1.503,-0.971v-0.003,0.005l-4.453,12.217a1.259,1.259 0,0 1,-2.388 0l-5.065,-13.897a1.336,1.336 0,0 0,-0.958 -0.845,7.708 7.708,0 0,1 -0.928,-14.749 0.868,0.868 0,0 0,0.555 -0.67A7.703,7.703 0,0 1,22 3.3a7.704,7.704 0,0 1,7.577 6.324c0.067,0.358 0.317,0.654 0.659,0.78a7.708,7.708 0,0 1,0.56 14.168zM31.502,13.832a5.498,5.498 0,0 0,-1.948 -1.331c-0.322,-0.13 -0.855,-0.229 -1.31,-0.295a0.883,0.883 0,0 1,-0.751 -0.922,5.603 5.603,0 0,0 -0.102,-1.379 5.502,5.502 0,0 0,-10.886 1.341,0.942 0.942,0 0,1 -0.805,0.968c-0.443,0.066 -0.945,0.162 -1.254,0.287A5.503,5.503 0,0 0,16.5 23.11h0.003v0.642c0.002,0.91 0.74,1.649 1.65,1.651a1.655,1.655 0,0 0,1.65 -1.65L19.8,23.1a2.2,2.2 0,0 1,4.053 -1.185c0.152,0.123 0.297,0.255 0.435,0.393 0.624,0.624 2.005,0.803 3.212,0.803a5.504,5.504 0,0 0,4.002 -9.278z"
android:fillColor="#000"
android:fillType="evenOdd"/>
</vector>
10 changes: 10 additions & 0 deletions demo/src/main/res/drawable/ic_restaurant.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="44dp"
android:height="44dp"
android:viewportWidth="44"
android:viewportHeight="44">
<path
android:pathData="M30.8,3.3a5.5,5.5 0,0 0,-5.5 5.5v16.5h2.2v13.2a2.2,2.2 0,1 0,4.4 0L31.9,4.4a1.1,1.1 0,0 0,-1.1 -1.1zM19.8,3.3a1.1,1.1 0,0 0,-1.1 1.1v8.8h-1.1L17.6,4.4a1.1,1.1 0,0 0,-2.2 0v8.8h-1.1L14.3,4.4a1.1,1.1 0,0 0,-2.2 0v11a3.3,3.3 0,0 0,2.2 3.112L14.3,38.5a2.2,2.2 0,0 0,4.4 0L18.7,18.512a3.3,3.3 0,0 0,2.2 -3.112v-11a1.1,1.1 0,0 0,-1.1 -1.1z"
android:fillColor="#000"
android:fillType="evenOdd"/>
</vector>

0 comments on commit 007e391

Please sign in to comment.