Skip to content
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
11 changes: 11 additions & 0 deletions app/src/main/java/hu/toliver/vinotes/domain/WineYears.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package hu.toliver.vinotes.domain

import java.util.Calendar

object WineYears {
const val MIN_YEAR = 1900

fun maxYear(): Int = Calendar.getInstance().get(Calendar.YEAR) + 1

fun isValid(year: Int): Boolean = year in MIN_YEAR..maxYear()
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,5 @@ enum class StatPeriod {
ALL_TIME -> null
}
}

val label: String
get() = when (this) {
LAST_30_DAYS -> "30 days"
LAST_3_MONTHS -> "3 months"
THIS_YEAR -> "This year"
ALL_TIME -> "All time"
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import hu.toliver.vinotes.R
import hu.toliver.vinotes.ui.util.userFacingErrorMessage
import hu.toliver.vinotes.domain.model.Taste
import hu.toliver.vinotes.domain.usecases.location.GetCurrentLocationUseCase
import hu.toliver.vinotes.domain.usecases.location.ReverseGeocodeUseCase
Expand Down Expand Up @@ -131,7 +132,12 @@ class AddTastingViewModel @Inject constructor(
longitude = longitude,
)
}
emitLocationError(error.message ?: context.getString(R.string.reverse_geocoding_failed))
emitLocationError(
userFacingErrorMessage(
context,
error.message ?: context.getString(R.string.reverse_geocoding_failed),
),
)
}
}
.onFailure { error ->
Expand All @@ -147,8 +153,9 @@ class AddTastingViewModel @Inject constructor(
}

private suspend fun emitLocationError(message: String) {
_state.update { it.copy(locationError = message, isLoadingLocation = false) }
_effect.send(AddTastingEffect.ShowLocationError(message))
val friendly = userFacingErrorMessage(context, message)
_state.update { it.copy(locationError = friendly, isLoadingLocation = false) }
_effect.send(AddTastingEffect.ShowLocationError(friendly))
}

private fun loadForEdit(tasteId: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hu.toliver.vinotes.ui.screen.settings

import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
Expand Down Expand Up @@ -128,32 +129,47 @@ fun SettingsScreen(
Text(stringResource(R.string.cancel))
}
},
title = { Text(stringResource(R.string.settings)) },
title = { Text(stringResource(R.string.theme)) },
text = {
Column {
val current = state.themeMode
Row(
Modifier.fillMaxWidth(),
Modifier
.fillMaxWidth()
.clickable { viewModel.onEvent(SettingsEvent.ThemeSelected(ThemeMode.LIGHT)) },
horizontalArrangement = Arrangement.Start,
verticalAlignment = Alignment.CenterVertically
) {
RadioButton(selected = current == ThemeMode.LIGHT, onClick = { viewModel.onEvent(SettingsEvent.ThemeSelected(ThemeMode.LIGHT)) })
RadioButton(
selected = current == ThemeMode.LIGHT,
onClick = null,
)
Text(text = stringResource(R.string.light))
}
Row(
Modifier.fillMaxWidth(),
Modifier
.fillMaxWidth()
.clickable { viewModel.onEvent(SettingsEvent.ThemeSelected(ThemeMode.DARK)) },
horizontalArrangement = Arrangement.Start,
verticalAlignment = Alignment.CenterVertically
) {
RadioButton(selected = current == ThemeMode.DARK, onClick = { viewModel.onEvent(SettingsEvent.ThemeSelected(ThemeMode.DARK)) })
RadioButton(
selected = current == ThemeMode.DARK,
onClick = null,
)
Text(text = stringResource(R.string.dark))
}
Row(
Modifier.fillMaxWidth(),
Modifier
.fillMaxWidth()
.clickable { viewModel.onEvent(SettingsEvent.ThemeSelected(ThemeMode.SYSTEM)) },
horizontalArrangement = Arrangement.Start,
verticalAlignment = Alignment.CenterVertically
) {
RadioButton(selected = current == ThemeMode.SYSTEM, onClick = { viewModel.onEvent(SettingsEvent.ThemeSelected(ThemeMode.SYSTEM)) })
RadioButton(
selected = current == ThemeMode.SYSTEM,
onClick = null,
)
Text(text = stringResource(R.string.system_settings))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import hu.toliver.vinotes.R
import hu.toliver.vinotes.ui.util.userFacingErrorMessage
import hu.toliver.vinotes.domain.model.enums.AppLanguage
import hu.toliver.vinotes.domain.usecases.catalog.ImportFromFileUseCase
import hu.toliver.vinotes.domain.usecases.catalog.PerformDeltaSyncUseCase
Expand Down Expand Up @@ -171,11 +172,16 @@ class SettingsViewModel @Inject constructor(
}
.onFailure { error ->
_state.update { it.copy(isSyncingCatalog = false) }
val msg = error.message ?: context.getString(R.string.error_syncing_catalog)
val msg = userFacingErrorMessage(
context,
error.message ?: context.getString(R.string.error_syncing_catalog),
)
// Detect DNS resolution failures and show actionable hint
if (msg.contains(context.getString(R.string.dns_resolution_failed), ignoreCase = true)
|| msg.contains(context.getString(R.string.unable_to_resolve_host), ignoreCase = true)
|| msg.contains(context.getString(R.string.no_address_associated_with_hostname), ignoreCase = true)) {
|| msg.contains(context.getString(R.string.no_address_associated_with_hostname), ignoreCase = true)
|| msg == context.getString(R.string.cannot_resolve_catalog_host)
) {
_effect.send(
SettingsEffect.ShowSnackbar(
context.getString(R.string.cannot_resolve_catalog_host)
Expand All @@ -196,11 +202,16 @@ class SettingsViewModel @Inject constructor(
}
.onFailure { error ->
_state.update { it.copy(isSyncingCatalog = false) }
val msg = error.message ?: context.getString(R.string.error_syncing_catalog)
val msg = userFacingErrorMessage(
context,
error.message ?: context.getString(R.string.error_syncing_catalog),
)
// Detect DNS resolution failures and show actionable hint
if (msg.contains(context.getString(R.string.dns_resolution_failed), ignoreCase = true)
|| msg.contains(context.getString(R.string.unable_to_resolve_host), ignoreCase = true)
|| msg.contains(context.getString(R.string.no_address_associated_with_hostname), ignoreCase = true)) {
|| msg.contains(context.getString(R.string.no_address_associated_with_hostname), ignoreCase = true)
|| msg == context.getString(R.string.cannot_resolve_catalog_host)
) {
_effect.send(
SettingsEffect.ShowSnackbar(
context.getString(R.string.cannot_resolve_catalog_host)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package hu.toliver.vinotes.ui.screen.stats

import androidx.annotation.StringRes
import hu.toliver.vinotes.R
import hu.toliver.vinotes.domain.usecases.stats.StatPeriod

@StringRes
fun StatPeriod.labelRes(): Int = when (this) {
StatPeriod.LAST_30_DAYS -> R.string.stats_period_30_days
StatPeriod.LAST_3_MONTHS -> R.string.stats_period_3_months
StatPeriod.THIS_YEAR -> R.string.stats_period_this_year
StatPeriod.ALL_TIME -> R.string.stats_period_all_time
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.res.stringArrayResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import hu.toliver.vinotes.R
Expand Down Expand Up @@ -74,7 +76,7 @@ fun TastingHeatmap(
.format(getInstance().time)
}

val dayLabels = listOf("M", "Tu", "W", "Th", "F", "Sa", "Su")
val dayLabels = stringArrayResource(R.array.heatmap_weekday_labels).toList()

Column(modifier = modifier) {
Row(horizontalArrangement = Arrangement.spacedBy(3.dp)) {
Expand Down Expand Up @@ -192,9 +194,11 @@ fun HeatmapTooltip(day: String, count: Int, onDismiss: () -> Unit, modifier: Mod
) {
Text(formatted, style = typography.bodyMedium)
Text(
text = if (count == 0) stringResource(R.string.no_tastings_yet) else stringResource(
R.string.amount_tastings, count
),
text = if (count == 0) {
stringResource(R.string.no_tastings_yet)
} else {
pluralStringResource(R.plurals.tasting_count, count, count)
},
style = typography.bodyMedium,
color = if (count > 0) colorScheme.primary else colorScheme.onSurfaceVariant
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import hu.toliver.vinotes.R
import kotlin.math.round
import hu.toliver.vinotes.domain.model.FullStatsData

@Composable
Expand All @@ -32,7 +33,7 @@ fun HeaderCountersRow(
playAnimations = playAnimations,
)
AnimatedStatCounter(
targetValue = data.averageRating.toInt(),
targetValue = round(data.averageRating).toInt(),
label = stringResource(R.string.average),
modifier = Modifier.weight(1f),
playAnimations = playAnimations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import hu.toliver.vinotes.R
import kotlin.math.round

@Composable
fun PeriodStatsRow(
Expand All @@ -26,7 +27,7 @@ fun PeriodStatsRow(
onPlayed = onPlayed,
)
AnimatedStatCounter(
targetValue = avgRating.toInt(),
targetValue = round(avgRating).toInt(),
label = stringResource(R.string.average_score),
modifier = Modifier.weight(1f),
playAnimations = playAnimations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import androidx.compose.material3.MaterialTheme.typography
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import hu.toliver.vinotes.domain.usecases.stats.StatPeriod
import hu.toliver.vinotes.ui.screen.stats.labelRes

@Composable
fun StatPeriodSelector(
Expand All @@ -29,7 +31,7 @@ fun StatPeriodSelector(
onClick = { onSelect(period) },
label = {
Text(
period.label,
stringResource(period.labelRes()),
style = typography.labelSmall,
textAlign = TextAlign.Center,
fontWeight = if (period == selected) FontWeight.SemiBold else FontWeight.Normal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import hu.toliver.vinotes.R
Expand Down Expand Up @@ -100,7 +101,11 @@ fun StreakSection(data: FullStatsData, modifier: Modifier = Modifier) {
color = colorScheme.onSurfaceVariant
)
Text(
text = stringResource(R.string.amount_day, data.longestStreak),
text = pluralStringResource(
R.plurals.day_count,
data.longestStreak,
data.longestStreak,
),
style = typography.titleMedium,
color = colorScheme.primary
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import hu.toliver.vinotes.R
import hu.toliver.vinotes.domain.WineYears
import hu.toliver.vinotes.ui.screen.UIConverter.toFloat
import hu.toliver.vinotes.domain.model.Taste
import hu.toliver.vinotes.domain.usecases.wine.DeleteWineUseCase
Expand Down Expand Up @@ -57,9 +58,24 @@ class WineDetailViewModel @Inject constructor(
WineDetailEvent.EditSheetDismissed -> _state.value = _state.value.copy(isEditSheetOpen = false)

is WineDetailEvent.WineSaved -> viewModelScope.launch {
if (!WineYears.isValid(event.wine.year)) {
_effect.send(
WineDetailEffect.ShowSnackbar(
context.getString(
R.string.invalid_wine_year,
WineYears.MIN_YEAR,
WineYears.maxYear(),
),
),
)
return@launch
}
runCatching { updateWine(event.wine) }
.onSuccess {
_state.value = _state.value.copy(isEditSheetOpen = false)
_state.value = _state.value.copy(
isEditSheetOpen = false,
wine = event.wine,
)
_effect.send(WineDetailEffect.ShowSnackbar(context.getString(R.string.wine_updated)))
}
.onFailure { e ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import hu.toliver.vinotes.R
Expand Down Expand Up @@ -80,7 +81,7 @@ fun WineHeroSection(
Text("·", color = MaterialTheme.colorScheme.onSurfaceVariant)
}
Text(
text = stringResource(R.string.amount_tasting, tastingCount),
text = pluralStringResource(R.plurals.tasting_count, tastingCount, tastingCount),
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import hu.toliver.vinotes.R
import hu.toliver.vinotes.domain.WineYears
import hu.toliver.vinotes.domain.model.WineWithStats
import hu.toliver.vinotes.domain.usecases.wine.DeleteWineUseCase
import hu.toliver.vinotes.domain.usecases.wine.GetWinesWithStatsUseCase
Expand Down Expand Up @@ -97,6 +98,18 @@ class WineListViewModel @Inject constructor(
_state.update { it.copy(editingWine = null) }

is WineListEvent.WineSaved -> viewModelScope.launch {
if (!WineYears.isValid(event.wine.year)) {
_effect.send(
WineListEffect.ShowSnackbar(
context.getString(
R.string.invalid_wine_year,
WineYears.MIN_YEAR,
WineYears.maxYear(),
),
),
)
return@launch
}
runCatching {
if (event.isNew) addWine(event.wine) else updateWine(event.wine)
}.onSuccess {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fun RatingBadge(rating: Int, tastingCount: Int) {
modifier = Modifier.size(12.dp),
)
Text(
text = "★ $rating · ${tastingCount}×",
text = "$rating · ${tastingCount}×",
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onPrimaryContainer,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ fun WineCard(
}

Text(
text = "${item.wine.producer} · ${item.wine.year} · ${item.wine.country}",
text = buildList {
if (item.wine.producer.isNotBlank()) add(item.wine.producer)
add(item.wine.year.toString())
if (item.wine.country.isNotBlank()) add(item.wine.country)
}.joinToString(" · "),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
maxLines = 1,
Expand Down
Loading
Loading