Skip to content

Instantly share code, notes, and snippets.

@Kyriakos-Georgiopoulos
Last active November 6, 2025 05:06
Show Gist options
  • Select an option

  • Save Kyriakos-Georgiopoulos/1b9648ad24c5a0656a3a35d69d0c9c2b to your computer and use it in GitHub Desktop.

Select an option

Save Kyriakos-Georgiopoulos/1b9648ad24c5a0656a3a35d69d0c9c2b to your computer and use it in GitHub Desktop.
/*
* Copyright 2025 Kyriakos Georgiopoulos
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.flow.collectLatest
@Composable
fun GymSessions() {
val sessionNames = listOf(
"Cardio", "Yoga", "Strength", "Cycling", "Pilates",
"Boxing", "HIIT", "Zumba", "CrossFit", "Stretching"
)
val sessionColors = listOf(
Color(0xFFEF5350), Color(0xFF26A69A), Color(0xFF7E57C2),
Color(0xFF42A5F5), Color(0xFFFFB74D), Color(0xFFD32F2F),
Color(0xFF66BB6A), Color(0xFFBA68C8), Color(0xFF5C6BC0), Color(0xFF90A4AE)
)
val hours = (0..23).map { String.format("%02d:00", it) }
val sessionLessons = remember {
List(sessionNames.size) { sessionIndex ->
val shuffledHours = hours.shuffled()
val lessonHours = shuffledHours.take((10..18).random())
hours.associateWith { hour ->
if (hour in lessonHours) {
Lesson(
title = "${sessionNames[sessionIndex]} - $hour",
instructor = "Coach ${'A' + sessionIndex}"
)
} else null
}
}
}
val hourHeaderState = remember { LazyListState() }
val sessionStates = remember { List(sessionNames.size) { LazyListState() } }
// Synchronize horizontal scroll of all session rows with the hour header
LaunchedEffect(Unit) {
snapshotFlow { hourHeaderState.firstVisibleItemIndex to hourHeaderState.firstVisibleItemScrollOffset }
.collectLatest { (index, offset) ->
sessionStates.forEach { state ->
if (state.firstVisibleItemIndex != index || state.firstVisibleItemScrollOffset != offset) {
state.scrollToItem(index, offset)
}
}
}
}
Column(
modifier = Modifier
.fillMaxSize()
.background(Color.Black)
) {
Spacer(modifier = Modifier.height(50.dp))
Text(
text = "Today's Gym Lessons",
style = MaterialTheme.typography.headlineMedium,
color = Color.White,
modifier = Modifier.padding(16.dp)
)
HourHeaderRow(hours = hours, state = hourHeaderState)
Spacer(Modifier.height(8.dp))
LazyColumn(
modifier = Modifier.fillMaxSize()
) {
items(sessionNames.size) { index ->
val sessionName = sessionNames[index]
val sessionColor = sessionColors[index]
val lessons = sessionLessons[index]
Column(
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 24.dp)
) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.padding(start = 16.dp, bottom = 4.dp)
) {
Box(
modifier = Modifier
.size(12.dp)
.background(sessionColor, CircleShape)
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = sessionName,
style = MaterialTheme.typography.titleMedium,
color = Color.White
)
}
LazyRow(
state = sessionStates[index],
contentPadding = PaddingValues(horizontal = 16.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp),
modifier = Modifier
.fillMaxWidth()
.height(160.dp)
) {
items(hours.size) { hourIndex ->
val hour = hours[hourIndex]
val lesson = lessons[hour]
if (lesson != null) {
GymLessonCard(
title = lesson.title,
time = hour,
instructor = lesson.instructor,
color = sessionColor
)
} else {
EmptyLessonCard(time = hour)
}
}
}
}
}
}
Spacer(Modifier.height(100.dp))
}
}
@Composable
fun HourHeaderRow(hours: List<String>, state: LazyListState) {
Column(
modifier = Modifier
.fillMaxWidth()
.height(80.dp)
.padding(bottom = 8.dp)
) {
Text(
text = "Hours",
style = MaterialTheme.typography.titleMedium,
color = Color.White,
modifier = Modifier.padding(start = 16.dp, bottom = 4.dp)
)
LazyRow(
state = state,
contentPadding = PaddingValues(horizontal = 16.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp),
modifier = Modifier.fillMaxSize()
) {
items(hours.size) { index ->
HourCard(hour = hours[index])
}
}
}
}
data class Lesson(val title: String, val instructor: String)
@Composable
fun GymLessonCard(
title: String,
time: String,
instructor: String,
color: Color
) {
Card(
shape = RoundedCornerShape(20.dp),
elevation = CardDefaults.cardElevation(8.dp),
colors = CardDefaults.cardColors(containerColor = color),
modifier = Modifier
.width(260.dp)
.fillMaxHeight()
) {
Column(
modifier = Modifier
.padding(16.dp)
.fillMaxSize(),
verticalArrangement = Arrangement.SpaceBetween
) {
Text(text = title, style = MaterialTheme.typography.titleLarge, color = Color.White)
Text(text = time, style = MaterialTheme.typography.bodyLarge, color = Color.White)
Text(
text = instructor,
style = MaterialTheme.typography.bodyMedium,
color = Color.White
)
}
}
}
@Composable
fun EmptyLessonCard(time: String) {
Card(
shape = RoundedCornerShape(20.dp),
elevation = CardDefaults.cardElevation(2.dp),
colors = CardDefaults.cardColors(containerColor = Color.DarkGray),
modifier = Modifier
.width(260.dp)
.fillMaxHeight()
) {
Box(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
contentAlignment = Alignment.Center
) {
Text(
text = time,
style = MaterialTheme.typography.bodySmall,
color = Color.LightGray,
textAlign = TextAlign.Center
)
}
}
}
@Composable
fun HourCard(hour: String) {
Card(
shape = RoundedCornerShape(12.dp),
elevation = CardDefaults.cardElevation(2.dp),
colors = CardDefaults.cardColors(containerColor = Color.Gray),
modifier = Modifier
.width(260.dp)
.fillMaxHeight()
) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text(
text = hour,
style = MaterialTheme.typography.titleMedium,
color = Color.White
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment