Skip to content

Commit

Permalink
refactor(schedules): remove column count parameter in ScheduleGrid co…
Browse files Browse the repository at this point in the history
…mponent.
  • Loading branch information
GerardPaligot committed Feb 11, 2024
1 parent 9bd24fd commit b898a96
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,12 @@ fun MainNavigation(
composable(Screen.ScheduleList.route) {
val showFilterIcon = adaptiveInfo.windowSizeClass.widthSizeClass.isCompat
|| (adaptiveInfo.windowSizeClass.widthSizeClass.isMedium && config.isPortrait)
val columnCount = when {
adaptiveInfo.windowSizeClass.widthSizeClass.isExpanded
&& adaptiveInfo.windowSizeClass.heightSizeClass.isCompat -> 1

adaptiveInfo.windowSizeClass.widthSizeClass.isExpanded -> 2
else -> 1
}
val isSmallSize = adaptiveInfo.windowSizeClass.heightSizeClass.isCompat
ScheduleGridAdaptive(
onScheduleStarted = onScheduleStarted,
onFilterClicked = { navController.navigate(Screen.ScheduleFilters.route) },
onTalkClicked = { navController.navigate(Screen.Schedule.route(it)) },
showFilterIcon = showFilterIcon,
columnCount = columnCount,
isSmallSize = isSmallSize
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ fun ScheduleGridAdaptive(
onTalkClicked: (id: String) -> Unit,
showFilterIcon: Boolean,
modifier: Modifier = Modifier,
columnCount: Int = 1,
isSmallSize: Boolean = false,
) {
SupportingPaneScaffold(
Expand All @@ -31,7 +30,6 @@ fun ScheduleGridAdaptive(
onFilterClicked = onFilterClicked,
onTalkClicked = onTalkClicked,
showFilterIcon = showFilterIcon,
columnCount = columnCount,
isSmallSize = isSmallSize
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ fun ScheduleGridVM(
onTalkClicked: (id: String) -> Unit,
showFilterIcon: Boolean,
modifier: Modifier = Modifier,
columnCount: Int = 1,
isSmallSize: Boolean = false,
viewModel: ScheduleGridViewModel = koinViewModel()
) {
Expand All @@ -38,14 +37,13 @@ fun ScheduleGridVM(
LaunchedEffect(key1 = Unit) {
onScheduleStarted()
}
val uiState = viewModel.uiState.collectAsState()
when (uiState.value) {
when (val uiState = viewModel.uiState.collectAsState().value) {
is ScheduleGridUiState.Loading -> Scaffold(
title = title,
modifier = modifier
) {
ScheduleGridPager(
agendas = (uiState.value as ScheduleGridUiState.Loading).agenda,
agendas = uiState.agenda,
onTalkClicked = {},
onFavoriteClicked = {},
isLoading = true
Expand All @@ -54,14 +52,13 @@ fun ScheduleGridVM(

is ScheduleGridUiState.Failure -> Text(text = stringResource(id = R.string.text_error))
is ScheduleGridUiState.Success -> {
val modelUi = (uiState.value as ScheduleGridUiState.Success)
val count = modelUi.scheduleUi.tabActionsUi.actions.count()
val count = uiState.scheduleUi.tabActionsUi.actions.count()
val pagerState = rememberPagerState(pageCount = { count })
Scaffold(
title = title,
modifier = modifier,
topActions = if (!showFilterIcon) TopActionsUi() else modelUi.scheduleUi.topActionsUi,
tabActions = modelUi.scheduleUi.tabActionsUi,
topActions = if (!showFilterIcon) TopActionsUi() else uiState.scheduleUi.topActionsUi,
tabActions = uiState.scheduleUi.tabActionsUi,
hasScrollBehavior = false,
onActionClicked = {
when (it.id) {
Expand All @@ -72,16 +69,15 @@ fun ScheduleGridVM(
}
) {
ScheduleGridPager(
agendas = modelUi.scheduleUi.schedules,
pagerState = pagerState,
agendas = uiState.scheduleUi.schedules,
onTalkClicked = onTalkClicked,
onFavoriteClicked = { talkItem ->
viewModel.markAsFavorite(context, talkItem)
},
isLoading = false,
columnCount = columnCount,
modifier = Modifier.padding(top = it.calculateTopPadding()),
pagerState = pagerState,
isSmallSize = isSmallSize,
modifier = Modifier.padding(top = it.calculateTopPadding())
isLoading = false
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ fun ScheduleGridPager(
onFavoriteClicked: (TalkItemUi) -> Unit,
modifier: Modifier = Modifier,
pagerState: PagerState = rememberPagerState(pageCount = { 0 }),
columnCount: Int = 2,
isSmallSize: Boolean = false,
isLoading: Boolean = false,
) {
Expand All @@ -31,10 +30,9 @@ fun ScheduleGridPager(
) { page ->
ScheduleGridScreen(
agenda = agendas[page],
modifier = modifier,
onTalkClicked = onTalkClicked,
onFavoriteClicked = onFavoriteClicked,
columnCount = columnCount,
modifier = modifier,
isSmallSize = isSmallSize,
isLoading = isLoading
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.gdglille.devfest.android.theme.m3.schedules.screens

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
Expand All @@ -11,6 +12,8 @@ import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import kotlin.math.floor
import org.gdglille.devfest.android.theme.m3.schedules.ui.talks.MediumScheduleItem
import org.gdglille.devfest.android.theme.m3.schedules.ui.talks.NoFavoriteTalks
import org.gdglille.devfest.android.theme.m3.schedules.ui.talks.SmallScheduleItem
Expand All @@ -32,58 +35,61 @@ fun ScheduleGridScreen(
onTalkClicked: (id: String) -> Unit,
onFavoriteClicked: (TalkItemUi) -> Unit,
modifier: Modifier = Modifier,
columnCount: Int = 2,
isSmallSize: Boolean = false,
isLoading: Boolean = false,
) {
if (agenda.onlyFavorites && !isLoading && agenda.talks.keys.isEmpty()) {
NoFavoriteTalks()
} else {
LazyVerticalGrid(
columns = GridCells.Fixed(count = columnCount),
modifier = modifier,
contentPadding = PaddingValues(vertical = SpacingTokens.LargeSpacing.toDp()),
verticalArrangement = Arrangement.spacedBy(SpacingTokens.MediumSpacing.toDp()),
horizontalArrangement = Arrangement.spacedBy(SpacingTokens.MediumSpacing.toDp())
) {
agenda.talks.entries.forEach { slot ->
item(span = { GridItemSpan(columnCount) }) {
Time(time = slot.key, modifier = Modifier.placeholder(visible = isLoading))
}
items(slot.value.toList(), key = { it.id }) {
if (it.isPause) {
if (isSmallSize) {
SmallPauseItem(
title = it.title,
room = it.room,
time = it.time,
timeImageVector = it.timeInMinutes.findTimeImageVector(),
modifier = Modifier.placeholder(visible = isLoading)
)
} else {
MediumPauseItem(
title = it.title,
room = it.room,
time = it.time,
timeImageVector = it.timeInMinutes.findTimeImageVector(),
modifier = Modifier.placeholder(visible = isLoading)
)
}
} else {
if (isSmallSize) {
SmallScheduleItem(
talk = it,
modifier = Modifier.placeholder(visible = isLoading),
onFavoriteClicked = onFavoriteClicked,
onTalkClicked = onTalkClicked
)
BoxWithConstraints {
val minSize = 260.dp
val count = floor(this.maxWidth / minSize).toInt()
LazyVerticalGrid(
columns = GridCells.Adaptive(minSize = minSize),
modifier = modifier,
contentPadding = PaddingValues(vertical = SpacingTokens.LargeSpacing.toDp()),
verticalArrangement = Arrangement.spacedBy(SpacingTokens.MediumSpacing.toDp()),
horizontalArrangement = Arrangement.spacedBy(SpacingTokens.MediumSpacing.toDp())
) {
agenda.talks.entries.forEach { slot ->
item(span = { GridItemSpan(count) }) {
Time(time = slot.key, modifier = Modifier.placeholder(visible = isLoading))
}
items(slot.value.toList(), key = { it.id }) {
if (it.isPause) {
if (isSmallSize) {
SmallPauseItem(
title = it.title,
room = it.room,
time = it.time,
timeImageVector = it.timeInMinutes.findTimeImageVector(),
modifier = Modifier.placeholder(visible = isLoading)
)
} else {
MediumPauseItem(
title = it.title,
room = it.room,
time = it.time,
timeImageVector = it.timeInMinutes.findTimeImageVector(),
modifier = Modifier.placeholder(visible = isLoading)
)
}
} else {
MediumScheduleItem(
talk = it,
modifier = Modifier.placeholder(visible = isLoading),
onFavoriteClicked = onFavoriteClicked,
onTalkClicked = onTalkClicked
)
if (isSmallSize) {
SmallScheduleItem(
talk = it,
modifier = Modifier.placeholder(visible = isLoading),
onFavoriteClicked = onFavoriteClicked,
onTalkClicked = onTalkClicked
)
} else {
MediumScheduleItem(
talk = it,
modifier = Modifier.placeholder(visible = isLoading),
onFavoriteClicked = onFavoriteClicked,
onTalkClicked = onTalkClicked
)
}
}
}
}
Expand All @@ -93,7 +99,7 @@ fun ScheduleGridScreen(
}

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Preview(device = PHONE_LANDSCAPE)
@Preview
@Composable
private fun ScheduleListScreenPreview() {
Conferences4HallTheme {
Expand All @@ -106,3 +112,18 @@ private fun ScheduleListScreenPreview() {
}
}
}

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Preview(device = PHONE_LANDSCAPE)
@Composable
private fun ScheduleListScreenLandscapePreview() {
Conferences4HallTheme {
Scaffold {
ScheduleGridScreen(
agenda = AgendaUi.fake,
onTalkClicked = {},
onFavoriteClicked = { }
)
}
}
}

0 comments on commit b898a96

Please sign in to comment.