Skip to content

Commit

Permalink
feat: Added carousel view
Browse files Browse the repository at this point in the history
  • Loading branch information
Lastaapps committed Jul 14, 2024
1 parent 35e06da commit dbf13c4
Show file tree
Hide file tree
Showing 9 changed files with 385 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,13 @@ internal object TodayKocourkovDishRepoImpl : TodayDishRepo {

override suspend fun sync(params: TodayRepoParams, isForced: Boolean): SyncOutcome {
delay(Random.nextInt(100..1000).milliseconds)
return if (Random.nextInt(0..3) != 0) {
localNotifier.value += 1
SyncResult.Updated.right()
} else {
SyncResult.Skipped.right()
}
return SyncResult.Skipped.right()
// return if (Random.nextInt(0..3) != 0) {
// localNotifier.value += 1
// SyncResult.Updated.right()
// } else {
// SyncResult.Skipped.right()
// }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023, Petr Laštovička as Lasta apps, All rights reserved
* Copyright 2024, Petr Laštovička as Lasta apps, All rights reserved
*
* This file is part of Menza.
*
Expand All @@ -23,5 +23,6 @@ enum class DishListMode(val id: Int) {
COMPACT(0),
GRID(1),
HORIZONTAL(2),
CAROUSEL(3),
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import cz.lastaapps.api.core.domain.model.Dish
import cz.lastaapps.core.ui.vm.HandleAppear
import cz.lastaapps.menza.R
import cz.lastaapps.menza.features.settings.domain.model.DishListMode
import cz.lastaapps.menza.features.settings.domain.model.DishListMode.CAROUSEL
import cz.lastaapps.menza.features.settings.domain.model.DishListMode.COMPACT
import cz.lastaapps.menza.features.settings.domain.model.DishListMode.GRID
import cz.lastaapps.menza.features.settings.domain.model.DishListMode.HORIZONTAL
Expand All @@ -60,6 +61,7 @@ import cz.lastaapps.menza.features.today.ui.vm.DishListViewModel
import cz.lastaapps.menza.features.today.ui.widget.DishListViewModeSwitch
import cz.lastaapps.menza.features.today.ui.widget.Experimental
import cz.lastaapps.menza.features.today.ui.widget.ImageSizeSetting
import cz.lastaapps.menza.features.today.ui.widget.TodayDishCarousel
import cz.lastaapps.menza.features.today.ui.widget.TodayDishGrid
import cz.lastaapps.menza.features.today.ui.widget.TodayDishHorizontal
import cz.lastaapps.menza.features.today.ui.widget.TodayDishList
Expand Down Expand Up @@ -140,7 +142,7 @@ private fun DishListContent(
}
val footerFabPadding: @Composable () -> Unit = {
state.selectedMenza?.getOrNull()?.videoLinks?.firstOrNull()?.let {
Spacer(Modifier.height(96.dp))
Spacer(Modifier.height(96.dp + Padding.MidSmall))
}
}

Expand Down Expand Up @@ -233,6 +235,25 @@ private fun DishListContent(
scroll = scrollStates.horizontal,
)

CAROUSEL -> TodayDishCarousel(
isLoading = state.isLoading,
onRefresh = onRefresh,
data = state.items,
onNoItems = onNoItems,
onDishSelected = onDishSelected,
userSettings = userSettings,
isOnMetered = state.isOnMetered,
header = header,
footer = {
Column {
gridSwitch()
footerFabPadding()
}
},
modifier = modifier.fillMaxSize(),
scroll = scrollStates.carousel,
)

null -> {}
}
}
Expand Down Expand Up @@ -267,6 +288,7 @@ internal data class ScrollStates(
val list: LazyListState = LazyListState(),
val grid: LazyStaggeredGridState = LazyStaggeredGridState(),
val horizontal: LazyListState = LazyListState(),
val carousel: LazyListState = LazyListState(),
) {
companion object {
val Saver: Saver<ScrollStates, *> = listSaver(
Expand All @@ -275,6 +297,7 @@ internal data class ScrollStates(
with(LazyListState.Saver) { save(it.list) },
with(LazyStaggeredGridState.Saver) { save(it.grid) },
with(LazyListState.Saver) { save(it.horizontal) },
with(LazyListState.Saver) { save(it.carousel) },
)
},
restore = { list ->
Expand All @@ -284,6 +307,7 @@ internal data class ScrollStates(
list[0]?.let { llsSaver.restore(it) }!!,
list[1]?.let { LazyStaggeredGridState.Saver.restore(it) }!!,
list[2]?.let { llsSaver.restore(it) }!!,
list[3]?.let { llsSaver.restore(it) }!!,
)
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import android.content.res.Configuration
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
Expand Down Expand Up @@ -55,6 +55,7 @@ import coil3.Extras
import coil3.compose.SubcomposeAsyncImage
import coil3.request.CachePolicy
import coil3.request.ImageRequest.Builder
import cz.lastaapps.api.core.domain.model.Dish
import cz.lastaapps.menza.R.string
import cz.lastaapps.menza.ui.components.placeholders.PlaceholderHighlight
import cz.lastaapps.menza.ui.components.placeholders.fade
Expand All @@ -63,16 +64,42 @@ import cz.lastaapps.menza.ui.util.PreviewWrapper
import kotlin.math.absoluteValue
import kotlin.random.Random

internal fun loadImmediately(downloadOnMetered: Boolean, isOnMetered: Boolean) =
downloadOnMetered || !isOnMetered

@Composable
internal fun DishImageOrSupplement(
dish: Dish,
loadImmediately: Boolean,
modifier: Modifier = Modifier,
ratio: Float? = DishImageTokens.ASPECT_RATIO,
) {
val imageModifier = (ratio?.let { modifier.aspectRatio(it) } ?: modifier).fillMaxSize()
dish.photoLink?.let {
DishImage(
it,
loadImmediately = loadImmediately,
modifier = imageModifier,
)
} ?: run {
DishImageSupplement(
dish.name.hashCode(),
color = MaterialTheme.colorScheme.primaryContainer,
modifier = imageModifier,
)
}
}

@Composable
internal fun DishImageRatio(
photoLink: String,
loadImmediately: Boolean,
modifier: Modifier = Modifier,
ratio: Float = DishImageTokens.ASPECT_RATIO,
) {
val imageModifier = modifier
.fillMaxWidth()
.aspectRatio(DishImageTokens.ASPECT_RATIO)
.aspectRatio(ratio)
.fillMaxSize()

DishImage(
photoLink = photoLink,
Expand Down Expand Up @@ -155,7 +182,7 @@ internal fun DishImageSupplement(
) {
Surface(
shape = MaterialTheme.shapes.medium,
modifier = modifier.aspectRatio(DishImageTokens.ASPECT_RATIO),
modifier = modifier,
color = color,
) {
val icon =
Expand Down Expand Up @@ -194,7 +221,9 @@ private fun DishImageSupplementPreview() = PreviewWrapper() {
DishImageSupplement(
remember { Random.nextInt() },
color = it,
modifier = Modifier.padding(12.dp),
modifier = Modifier
.padding(12.dp)
.aspectRatio(DishImageTokens.ASPECT_RATIO),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal fun DishListViewModeSwitch(
DishListMode.COMPACT to R.string.today_list_mode_compact,
DishListMode.GRID to R.string.today_list_mode_grid,
DishListMode.HORIZONTAL to R.string.today_list_mode_horizontal,
DishListMode.CAROUSEL to R.string.today_list_mode_carousel,
)
}
SingleChoiceSegmentedButtonRow(
Expand Down
Loading

0 comments on commit dbf13c4

Please sign in to comment.