From ee14c502a1f71bc4e9980e5e7f68909d7d5dcf27 Mon Sep 17 00:00:00 2001 From: Leonardo Colman Lopes Date: Sat, 2 Mar 2024 19:14:53 -0300 Subject: [PATCH] Add new strings and refactor DropdownMenu component This commit includes the addition of new string resources to the strings .xml file, primarily for days of the week and a preferred restaurant. It also refactors and renames ExposedDropdownMenuBox component to DropdownMenu, simplifying its parameters and its usage in RestaurantPage. The DayOfWeek enum has been updated to utilize these new strings. --- ...osedDropdownMenuBox.kt => DropdownMenu.kt} | 4 +- .../app/jopiter/restaurant/RestaurantPage.kt | 44 +++++++++++++------ app/src/main/res/values/strings.xml | 9 ++++ 3 files changed, 41 insertions(+), 16 deletions(-) rename app/src/main/java/app/jopiter/component/{ExposedDropdownMenuBox.kt => DropdownMenu.kt} (94%) diff --git a/app/src/main/java/app/jopiter/component/ExposedDropdownMenuBox.kt b/app/src/main/java/app/jopiter/component/DropdownMenu.kt similarity index 94% rename from app/src/main/java/app/jopiter/component/ExposedDropdownMenuBox.kt rename to app/src/main/java/app/jopiter/component/DropdownMenu.kt index 2efbf4c..ec6144f 100644 --- a/app/src/main/java/app/jopiter/component/ExposedDropdownMenuBox.kt +++ b/app/src/main/java/app/jopiter/component/DropdownMenu.kt @@ -15,12 +15,12 @@ import androidx.compose.ui.Modifier @OptIn(ExperimentalMaterialApi::class) @Composable -fun ExposedDropdownMenuBox( +fun DropdownMenu( modifier: Modifier = Modifier, value: String, label: String, options: List, - optionToLabel: (T) -> String, + optionToLabel: @Composable (T) -> String, onOptionSelected: (T) -> Unit ) { var expanded by remember { mutableStateOf(false) } diff --git a/app/src/main/java/app/jopiter/restaurant/RestaurantPage.kt b/app/src/main/java/app/jopiter/restaurant/RestaurantPage.kt index 64b4dba..a4a6ec2 100644 --- a/app/src/main/java/app/jopiter/restaurant/RestaurantPage.kt +++ b/app/src/main/java/app/jopiter/restaurant/RestaurantPage.kt @@ -1,8 +1,8 @@ package app.jopiter.restaurant +import androidx.annotation.StringRes import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.material.ExperimentalMaterialApi import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue @@ -10,12 +10,14 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier -import app.jopiter.component.ExposedDropdownMenuBox +import androidx.compose.ui.res.stringResource +import app.jopiter.R +import app.jopiter.R.string.preferred_restaurant +import app.jopiter.component.DropdownMenu import app.jopiter.restaurant.model.Restaurant import app.jopiter.restaurant.repository.PreferredRestaurantRepository import org.koin.compose.koinInject -@OptIn(ExperimentalMaterialApi::class) @Composable fun RestaurantPage() { val preferredRestaurantRepository = koinInject() @@ -27,18 +29,32 @@ fun RestaurantPage() { Column(Modifier.fillMaxWidth()) { - ExposedDropdownMenuBox(Modifier.fillMaxWidth(), preferredRestaurant.name,"Preferred Restaurant", Restaurant.AllRestaurants, { it.name }, { setPreferredRestaurant(it) } ) - - ExposedDropdownMenuBox(Modifier.fillMaxWidth(), selectedDay.displayName, "Day of Week", DayOfWeek.entries, { it.displayName }, { selectedDay = it }) + DropdownMenu( + modifier = Modifier.fillMaxWidth(), + value = preferredRestaurant.name, + label = stringResource(preferred_restaurant), + options = Restaurant.AllRestaurants, + optionToLabel = { it.name }, + onOptionSelected = { setPreferredRestaurant(it) } + ) + + DropdownMenu( + modifier = Modifier.fillMaxWidth(), + value = stringResource(selectedDay.displayNameRes), + label = stringResource(R.string.day_of_week), + options = DayOfWeek.entries, + optionToLabel = { stringResource(it.displayNameRes) }, + onOptionSelected = { selectedDay = it } + ) } } -enum class DayOfWeek(val displayName: String) { - MONDAY("Monday"), - TUESDAY("Tuesday"), - WEDNESDAY("Wednesday"), - THURSDAY("Thursday"), - FRIDAY("Friday"), - SATURDAY("Saturday"), - SUNDAY("Sunday") +enum class DayOfWeek(@StringRes val displayNameRes: Int) { + MONDAY(R.string.monday), + TUESDAY(R.string.tuesday), + WEDNESDAY(R.string.wednesday), + THURSDAY(R.string.thursday), + FRIDAY(R.string.friday), + SATURDAY(R.string.saturday), + SUNDAY(R.string.sunday) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 73d376e..9b06b02 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4,4 +4,13 @@ Jopiter App Home Restaurant + Restaurante Preferido + Dia da Semana + Segunda-feira + Terça-feira + Quarta-feira + Quinta-feira + Sexta-feira + Sábado + Domingo \ No newline at end of file