Skip to content

Commit

Permalink
feat: Rating overview in the info screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Lastaapps committed Sep 6, 2024
1 parent 1890c3f commit c4128e2
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package cz.lastaapps.api.core.domain.model

import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentMapOf

data class DishCategory(
val nameShort: String?,
Expand All @@ -46,7 +45,7 @@ data class Dish(
val ingredients: ImmutableList<String>,
val isActive: Boolean,
) {
val rating: Rating = Rating("", 4.321f, 420, persistentMapOf())
val rating: Rating = Rating.Mocked.valid
}

data class ServingPlace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,41 @@
package cz.lastaapps.api.core.domain.model

import kotlinx.collections.immutable.ImmutableMap
import kotlinx.collections.immutable.persistentMapOf

data class Rating(
val dishID: String,
val overallRating: Float,
val ratingCount: Int,
val ratingCategories: ImmutableMap<RatingCategory, Float>,
)
) {
object Mocked {
val valid =
Rating(
dishID = "",
overallRating = 4.321f,
ratingCount = 420,
ratingCategories =
persistentMapOf(
RatingCategory.TASTE to 3.4f,
RatingCategory.WORTHINESS to 1.2f,
RatingCategory.PORTION_SIZE to 5.0f,
),
)
val noRatings =
Rating(
dishID = "",
overallRating = 0.0f,
ratingCount = 0,
ratingCategories =
persistentMapOf(
RatingCategory.TASTE to 0.0f,
RatingCategory.WORTHINESS to 0.0f,
RatingCategory.PORTION_SIZE to 0.0f,
),
)
}
}

enum class RatingCategory {
TASTE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private fun TodayContent(
currentDish?.let {
TodayInfo(
dish = currentDish,
onRating = { }, // TODO
modifier = Modifier.fillMaxSize(),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ internal fun DishRatingBadge(
@Composable
private fun DishRatingBadgePreview() =
PreviewWrapper {
DishRatingBadge(Rating("", 4.321f, 42, persistentMapOf()), {})
DishRatingBadge(Rating("", 4.0f, 42, persistentMapOf()), {})
DishRatingBadge(Rating("", 0.0f, 0, persistentMapOf()), {})
listOf(Rating.Mocked.valid, Rating.Mocked.noRatings).forEach {
DishRatingBadge(it, {})
}
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,64 @@

package cz.lastaapps.menza.features.today.ui.widget

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.StarRate
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.PlaceholderVerticalAlign
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em
import cz.lastaapps.api.core.domain.model.Dish
import cz.lastaapps.api.core.domain.model.Rating
import cz.lastaapps.api.core.domain.model.RatingCategory
import cz.lastaapps.api.core.domain.model.RatingCategory.PORTION_SIZE
import cz.lastaapps.api.core.domain.model.RatingCategory.TASTE
import cz.lastaapps.api.core.domain.model.RatingCategory.WORTHINESS
import cz.lastaapps.api.core.domain.model.ServingPlace
import cz.lastaapps.menza.R
import cz.lastaapps.menza.features.today.ui.util.allergenForId
import cz.lastaapps.menza.features.today.ui.util.formatPrice
import cz.lastaapps.menza.ui.theme.MenzaColors
import cz.lastaapps.menza.ui.theme.Padding
import cz.lastaapps.menza.ui.util.PreviewWrapper
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.ImmutableMap
import kotlinx.collections.immutable.persistentMapOf
import kotlin.math.max

@Composable
fun TodayInfo(
dish: Dish,
onRating: () -> Unit,
modifier: Modifier = Modifier,
) {
Column(
Expand All @@ -66,6 +92,7 @@ fun TodayInfo(
IssueLocationList(
list = dish.servingPlaces,
)
RatingOverview(rating = dish.rating, onRating = onRating)
AllergenList(
allergens = dish.allergens,
)
Expand Down Expand Up @@ -137,6 +164,124 @@ private fun IssueLocationList(
}
}

@Composable
private fun RatingOverview(
rating: Rating,
onRating: () -> Unit,
modifier: Modifier = Modifier,
) {
Card(
modifier = modifier.clickable { onRating() },
) {
Column(
modifier = Modifier.padding(Padding.MidSmall),
verticalArrangement = Arrangement.spacedBy(Padding.Small),
) {
val icons =
remember {
persistentMapOf(
"star" to
InlineTextContent(
Placeholder(1.1.em, 1.1.em, PlaceholderVerticalAlign.TextTop),
) {
Icon(Icons.Default.StarRate, contentDescription = null)
},
"person" to
InlineTextContent(
Placeholder(1.1.em, 1.1.em, PlaceholderVerticalAlign.TextCenter),
) {
Icon(Icons.Default.Person, contentDescription = null)
},
"big_star" to
InlineTextContent(
Placeholder(1.5.em, 1.5.em, PlaceholderVerticalAlign.Center),
) {
Icon(
Icons.Default.StarRate,
contentDescription = null,
tint = MenzaColors.gold,
modifier = Modifier.fillMaxSize(),
)
},
)
}
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(Padding.Smaller),
) {
Text(
text = stringResource(R.string.today_info_rating_title),
style = MaterialTheme.typography.headlineSmall,
modifier = Modifier.weight(1f).align(Alignment.Top),
)
Column(
modifier = Modifier.align(Alignment.Bottom),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Bottom,
) {
if (rating.ratingCount != 0) {
Text(
text =
buildAnnotatedString {
append("%.1f".format(rating.overallRating))
appendInlineContent("big_star")
},
style = MaterialTheme.typography.titleLarge,
inlineContent = icons,
)
}
Text(
text =
buildAnnotatedString {
append(rating.ratingCount.toString())
appendInlineContent("person")
},
style = MaterialTheme.typography.bodySmall,
inlineContent = icons,
)
}
}

(rating.ratingCategories as ImmutableMap<RatingCategory, Float>).forEach { (key, value) ->
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
) {
Text(
text = key.toText(),
modifier = Modifier.weight(1f),
style = MaterialTheme.typography.titleMedium,
)
Text(
buildAnnotatedString {
append("%.1f".format(value))
appendInlineContent("star")
},
style = MaterialTheme.typography.bodyMedium,
inlineContent = icons,
)
}
}
}
}
}

@Composable
private fun RatingCategory.toText() =
when (this) {
TASTE -> R.string.rating_category_taste
PORTION_SIZE -> R.string.rating_category_portion_size
WORTHINESS -> R.string.rating_category_worthiness
}.let { stringResource(it) }

@Preview
@Composable
private fun RatingOverviewPreview() =
PreviewWrapper {
RatingOverview(Rating.Mocked.valid, onRating = {})
RatingOverview(Rating.Mocked.noRatings, onRating = {})
}

@Composable
private fun AllergenList(
allergens: ImmutableList<Int>?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import cz.lastaapps.api.core.domain.model.Menza
import cz.lastaapps.menza.ui.theme.colorForMenza
import cz.lastaapps.menza.ui.theme.MenzaColors

@Composable
fun MenzaLetter(
menza: Menza,
modifier: Modifier = Modifier,
) {
val colors = colorForMenza(menza)
val colors = MenzaColors.colorForMenza(menza)
val brush = Brush.horizontalGradient(colors)
val size = 32.dp

Expand Down
50 changes: 27 additions & 23 deletions app/src/main/kotlin/cz/lastaapps/menza/ui/theme/MenzaColors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,32 @@ import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlin.math.abs

private val menzaColors =
persistentListOf(
Color(0xfff44333) to Color(0xffff795e),
Color(0xffe91e63) to Color(0xffff6090),
Color(0xff9c27b0) to Color(0xffd05ce3),
Color(0xff673ab7) to Color(0xff9a67ea),
Color(0xff3f51b5) to Color(0xff757de8),
Color(0xff2196f3) to Color(0xff6ec6ff),
Color(0xff03a9f4) to Color(0xff67daff),
Color(0xff00bcd4) to Color(0xff62efff),
Color(0xff009688) to Color(0xff52c7b8),
Color(0xff4caf50) to Color(0xff80e27e),
Color(0xff8bc34a) to Color(0xffbef67a),
// Color(0xffcddc39) to Color(0xffffff6e),
// Color(0xffffeb3b) to Color(0xffffff72),
Color(0xffffc107) to Color(0xfffff350),
Color(0xffff9800) to Color(0xffffc947),
Color(0xffff5722) to Color(0xffff8a50),
)
object MenzaColors {
private val menzaColors =
persistentListOf(
Color(0xfff44333) to Color(0xffff795e),
Color(0xffe91e63) to Color(0xffff6090),
Color(0xff9c27b0) to Color(0xffd05ce3),
Color(0xff673ab7) to Color(0xff9a67ea),
Color(0xff3f51b5) to Color(0xff757de8),
Color(0xff2196f3) to Color(0xff6ec6ff),
Color(0xff03a9f4) to Color(0xff67daff),
Color(0xff00bcd4) to Color(0xff62efff),
Color(0xff009688) to Color(0xff52c7b8),
Color(0xff4caf50) to Color(0xff80e27e),
Color(0xff8bc34a) to Color(0xffbef67a),
// Color(0xffcddc39) to Color(0xffffff6e),
// Color(0xffffeb3b) to Color(0xffffff72),
Color(0xffffc107) to Color(0xfffff350),
Color(0xffff9800) to Color(0xffffc947),
Color(0xffff5722) to Color(0xffff8a50),
)

fun colorForMenza(menza: Menza): ImmutableList<Color> {
val hash = menza.name.hashCode()
return menzaColors[abs(hash % menzaColors.size)]
.let { persistentListOf(it.first, it.second) }
fun colorForMenza(menza: Menza): ImmutableList<Color> {
val hash = menza.name.hashCode()
return menzaColors[abs(hash % menzaColors.size)]
.let { persistentListOf(it.first, it.second) }
}

val gold = Color(255, 165, 0)
}
4 changes: 4 additions & 0 deletions app/src/main/res/values-cs/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
<string name="today_info_image_load_failed">Nepodařilo se načíst obrázek</string>
<string name="today_info_unknown_allergen_title">Neznámý alergen</string>
<string name="today_info_unknown_allergen_description">Server vrátil neplatná data</string>
<string name="today_info_rating_title">Hodnocení</string>
<string name="today_list_none">Data nejsou dostupná 👨‍🍳</string>
<string name="today_list_web">Otevřít web</string>
<string name="today_list_image_load_failed">Nepodařilo se načíst obrázek</string>
Expand All @@ -209,6 +210,9 @@
<string name="today_list_video_fab_content_description">Živý záznam z kamery</string>
<string name="today_list_video_title">Živý záznam z kamery</string>
<string name="today_list_video_error">Nepodařilo se načíst obrázek</string>
<string name="rating_category_taste">Chuť</string>
<string name="rating_category_portion_size">Porce</string>
<string name="rating_category_worthiness">Správná volba</string>
<string name="week_list_none">Data nejsou dostupná 👨‍🍳</string>
<string name="week_list_web">Otevřít web</string>
<string name="error_button_report">Nahlásit</string>
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
<string name="today_info_image_load_failed">Failed to load the image</string>
<string name="today_info_unknown_allergen_title">Unknown allergen</string>
<string name="today_info_unknown_allergen_description">Server returned invalid data</string>
<string name="today_info_rating_title">Rating</string>
<string name="today_list_none">No data available 👨‍🍳</string>
<string name="today_list_web">Check website</string>
<string name="today_list_image_load_failed">Failed to load the image</string>
Expand All @@ -226,6 +227,10 @@
<string name="today_list_video_title">Live Camera feed</string>
<string name="today_list_video_error">Failed to load the image</string>

<string name="rating_category_taste">Taste</string>
<string name="rating_category_portion_size">Portion size</string>
<string name="rating_category_worthiness">Good choice</string>

<string name="week_list_none">No data available 👨‍🍳</string>
<string name="week_list_web">Check website</string>

Expand Down Expand Up @@ -309,5 +314,4 @@
<string name="wallet_login_save">Save</string>
<string name="wallet_low_balance">Your account balance is low! 🙉</string>


</resources>

0 comments on commit c4128e2

Please sign in to comment.