Skip to content

Commit

Permalink
Merge pull request #39 from bumble-tech/star-content-layout-adjustments
Browse files Browse the repository at this point in the history
Star field layout adjustments + autoscroll
  • Loading branch information
KovalevAndrey authored Oct 24, 2023
2 parents 45a2705 + 4b03ffa commit 221c882
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 26 deletions.
1 change: 1 addition & 0 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ android {
buildTypes {
release {
isMinifyEnabled = true
signingConfig = signingConfigs.getByName("debug")
}
}
compileOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumble.puzzyx.composable

import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -8,31 +9,45 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.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.draw.clip
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.bumble.puzzyx.imageloader.EmbeddableResourceImage
import com.bumble.appyx.navigation.integration.LocalScreenSize
import com.bumble.appyx.navigation.integration.ScreenSize.WindowSizeClass
import com.bumble.puzzyx.model.Entry
import com.bumble.puzzyx.ui.colors
import kotlinx.coroutines.isActive

@Composable
fun EntryCard(
entry: Entry,
modifier: Modifier = Modifier,
) {
val scaleFactor = scaleFactor()
val size = 24.dp * scaleFactor
Box(
modifier = modifier.clip(RoundedCornerShape(16.dp))
) {
when (entry) {
is Entry.Text -> TextEntry(entry)
is Entry.Text -> TextEntry(
entry = entry,
paddingTop = size
)

is Entry.Image -> EmbeddableResourceImage(
path = "participant/${entry.path}",
contentDescription = entry.contentDescription,
Expand All @@ -43,17 +58,20 @@ fun EntryCard(
is Entry.ComposableContent -> entry.content()
}
GitHubHeader(
size = size,
entry = entry,
modifier = Modifier.padding(8.dp)
modifier = Modifier.padding(8.dp * scaleFactor)
)
}
}

@Composable
fun GitHubHeader(
size: Dp,
entry: Entry,
modifier: Modifier = Modifier
) {
val scaleFactor = scaleFactor()
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = modifier
Expand All @@ -62,36 +80,59 @@ fun GitHubHeader(
path = "github.png",
contentScale = ContentScale.Inside,
modifier = Modifier
.size(32.dp)
.padding(2.dp)
.size(size)
)
Spacer(
modifier = Modifier.size(4.dp)
modifier = Modifier.size(4.dp * scaleFactor)
)
Text(
text = entry.githubUserName,
fontSize = 18.sp,
fontSize = 16.sp * scaleFactor,
fontWeight = FontWeight.Bold
)
}
}

@Composable
fun TextEntry(
paddingTop: Dp,
entry: Entry.Text,
modifier: Modifier = Modifier
) {
val colorIdx = remember { colors.indices.random() }
val state = rememberScrollState()

Text(
text = entry.message,
fontSize = 16.sp,
LaunchedEffect(Unit) {
while (isActive) {
state.animateScrollTo(state.maxValue, tween(10000))
state.scrollTo(0)
}
}

val scaleFactor = scaleFactor()

Column(
modifier = modifier
.fillMaxSize()
.background(colors[colorIdx])
.padding(12.dp)
.padding(top = 36.dp),
)
.padding(12.dp * scaleFactor)
.padding(top = paddingTop)
.clipToBounds()
.verticalScroll(state)
) {
Text(
text = entry.message,
fontSize = 16.sp * scaleFactor,
)
}
}

@Composable
fun scaleFactor(): Float = when (LocalScreenSize.current.windowSizeClass) {
WindowSizeClass.COMPACT -> 1f
WindowSizeClass.MEDIUM -> 1.5f
WindowSizeClass.EXPANDED -> 2f
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private data class StarFieldSpecs(
val zFadeInEnd: Float = 0.4f,
val zFadeOutStart: Float = 1.3f,
val zFadeOutEnd: Float = 1.4f,
val scaleFactor: Float,
) {
val zOffset = (zFadeOutEnd - zFadeInStart) / maxEntries
}
Expand All @@ -70,7 +71,7 @@ private sealed class StarType {

data class EntryType(val entry: Entry) : StarType() {
companion object {
val sizeDp: Dp = 260.dp
val sizeDp: Dp = 200.dp
const val aspectRatio: Float = 1.5f
}
}
Expand Down Expand Up @@ -115,7 +116,7 @@ private data class StarField(
entries.mapIndexed { index, entry ->
Star(
zCoord = starFieldSpecs.zFadeInStart - index * starFieldSpecs.zOffset,
sizeDp = StarType.EntryType.sizeDp,
sizeDp = StarType.EntryType.sizeDp * starFieldSpecs.scaleFactor,
aspectRatio = StarType.EntryType.aspectRatio,
type = StarType.EntryType(entry = entry),
)
Expand All @@ -125,9 +126,11 @@ private data class StarField(


private fun StarField.update(
timeInSecs: Float
timeInSecs: Float,
specs: StarFieldSpecs,
): StarField =
copy(
specs = specs,
stars = stars.map { star ->
val zUpdatedCoord = star.zCoord + specs.speed * timeInSecs
if (zUpdatedCoord < specs.zFadeOutEnd) {
Expand All @@ -147,7 +150,13 @@ fun StarFieldMessageBoard(
entries: ImmutableList<Entry>,
modifier: Modifier = Modifier,
) {
val starFieldSpecs = remember { StarFieldSpecs(maxEntries = entries.size) }
val scaleFactor = scaleFactor()
val starFieldSpecs = remember(scaleFactor, entries) {
StarFieldSpecs(
maxEntries = entries.size,
scaleFactor = scaleFactor
)
}
var starField by remember { mutableStateOf(generateStars(starFieldSpecs, entries)) }
var running by remember { mutableStateOf(true) }
LaunchedEffect(Unit) {
Expand All @@ -159,6 +168,7 @@ fun StarFieldMessageBoard(
}
starField = starField.update(
timeInSecs = (it - lastFrame) / 1_000f,
specs = starFieldSpecs,
)
lastFrame = it
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.bumble.puzzyx.imageloader

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale

@Composable
expect fun EmbeddableResourceImage(
path: String,
modifier: Modifier = Modifier,
contentDescription: String? = null,
contentScale: ContentScale = ContentScale.Fit,
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ import kotlinx.coroutines.withContext
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.resource

@Composable
expect fun EmbeddableResourceImage(
path: String,
modifier: Modifier = Modifier,
contentDescription: String? = null,
contentScale: ContentScale = ContentScale.Fit,
)

@OptIn(ExperimentalResourceApi::class)
@Composable
internal fun ResourceImage(
Expand Down
12 changes: 9 additions & 3 deletions shared/src/commonMain/kotlin/com/bumble/puzzyx/model/Entries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ val entries = listOf(
Entry.Text(
puzzle = Puzzle.PUZZLE1,
githubUserName = "dataGeek",
message = "Shoutout to the organizers for an amazing lineup!"
message = "Who's up for a post-conference karaoke session tonight? " +
"Mind blown by the innovative ideas shared today. " +
"Great to see old friends and make new ones!"
),
Entry.Text(
puzzle = Puzzle.PUZZLE1,
Expand All @@ -39,7 +41,9 @@ val entries = listOf(
Entry.Text(
puzzle = Puzzle.PUZZLE1,
githubUserName = "byteBender",
message = "Let's connect! Find me at the networking session!"
message = "Who's up for a post-conference karaoke session tonight? " +
"Mind blown by the innovative ideas shared today. " +
"Great to see old friends and make new ones!"
),
Entry.Text(
puzzle = Puzzle.PUZZLE1,
Expand All @@ -59,7 +63,9 @@ val entries = listOf(
Entry.Text(
puzzle = Puzzle.PUZZLE1,
githubUserName = "cyberPioneer",
message = "Who's up for a post-conference karaoke session tonight?"
message = "Who's up for a post-conference karaoke session tonight? " +
"Mind blown by the innovative ideas shared today. " +
"Great to see old friends and make new ones!"
),
Entry.Text(
puzzle = Puzzle.PUZZLE1,
Expand Down

0 comments on commit 221c882

Please sign in to comment.