Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.tooling.preview.Preview
import com.lukeneedham.videodiary.R
import com.lukeneedham.videodiary.domain.model.Day
import java.time.DayOfWeek
import java.time.LocalDate
Expand All @@ -34,9 +40,27 @@ fun DiaryDatePicker(
placeholderDaysStart + week.map { Weekday.Value(it) } + placeholderDaysEnd
}

val today = LocalDate.now()
val rows = buildList<WeekRow> {
var previousWasCollapsed = false
weekDays.forEach { week ->
val isCurrentWeek = week.any { it is Weekday.Value && it.day.date == today }
val isEmpty = week.none { it is Weekday.Value && it.day.video != null }
if (!isCurrentWeek && isEmpty) {
if (!previousWasCollapsed) {
add(WeekRow.Collapsed)
previousWasCollapsed = true
}
} else {
add(WeekRow.Normal(week))
previousWasCollapsed = false
}
}
}

val firstVisibleWeekIndex = remember {
val res = weekDays.indexOfFirst { week ->
week.any { it is Weekday.Value && it.day.date == initialFocusedDate }
val res = rows.indexOfFirst { row ->
row is WeekRow.Normal && row.week.any { it is Weekday.Value && it.day.date == initialFocusedDate }
}
if (res == -1) 0 else res
}
Expand All @@ -53,30 +77,46 @@ fun DiaryDatePicker(
state = state,
modifier = modifier
) {
items(weekDays) { week ->
Row(modifier = Modifier.fillParentMaxWidth()) {
week.forEach { weekday ->
Box(
modifier = Modifier
.weight(1f)
) {
when (weekday) {
is Weekday.Empty -> {
// Nothing
}
items(rows) { row ->
when (row) {
is WeekRow.Normal -> {
Row(modifier = Modifier.fillParentMaxWidth()) {
row.week.forEach { weekday ->
Box(
modifier = Modifier
.weight(1f)
) {
when (weekday) {
is Weekday.Empty -> {
// Nothing
}

is Weekday.Value -> {
val day = weekday.day
DiaryDatePickerDay(
day = day,
onClick = {
onDateSelected(day.date)
is Weekday.Value -> {
val day = weekday.day
DiaryDatePickerDay(
day = day,
onClick = {
onDateSelected(day.date)
}
)
}
)
}
}
}
}
}

is WeekRow.Collapsed -> {
val collapsedDescription = stringResource(R.string.date_picker_collapsed_weeks_description)
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.fillParentMaxWidth()
.semantics { contentDescription = collapsedDescription }
) {
Text(text = stringResource(R.string.date_picker_collapsed_weeks))
}
}
}
}
}
Expand All @@ -87,6 +127,11 @@ private sealed interface Weekday {
data object Empty : Weekday
}

private sealed interface WeekRow {
data class Normal(val week: List<Weekday>) : WeekRow
data object Collapsed : WeekRow
}

@Preview
@Composable
private fun Preview() {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<resources>
<string name="app_name">Video Diary</string>
<string name="date_picker_collapsed_weeks">…</string>
<string name="date_picker_collapsed_weeks_description">Empty weeks collapsed</string>
</resources>
Loading