Skip to content

Commit

Permalink
Add new strings and refactor DropdownMenu component
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LeoColman committed Mar 2, 2024
1 parent 343d4fd commit ee14c50
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import androidx.compose.ui.Modifier

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun <T> ExposedDropdownMenuBox(
fun <T> DropdownMenu(
modifier: Modifier = Modifier,
value: String,
label: String,
options: List<T>,
optionToLabel: (T) -> String,
optionToLabel: @Composable (T) -> String,
onOptionSelected: (T) -> Unit
) {
var expanded by remember { mutableStateOf(false) }
Expand Down
44 changes: 30 additions & 14 deletions app/src/main/java/app/jopiter/restaurant/RestaurantPage.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
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
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<PreferredRestaurantRepository>()
Expand All @@ -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)
}
9 changes: 9 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@
<string name="jopiter_app">Jopiter App</string>
<string name="home_title">Home</string>
<string name="restaurant_title">Restaurant</string>
<string name="preferred_restaurant">Restaurante Preferido</string>
<string name="day_of_week">Dia da Semana</string>
<string name="monday">Segunda-feira</string>
<string name="tuesday">Terça-feira</string>
<string name="wednesday">Quarta-feira</string>
<string name="thursday">Quinta-feira</string>
<string name="friday">Sexta-feira</string>
<string name="saturday">Sábado</string>
<string name="sunday">Domingo</string>
</resources>

0 comments on commit ee14c50

Please sign in to comment.