Skip to content

Commit

Permalink
Couple small star sample fixes (#670)
Browse files Browse the repository at this point in the history
Resolves #119
  • Loading branch information
ZacSweers authored Jun 7, 2023
1 parent 3ff2438 commit d4085f0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.movableContentOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -200,46 +201,52 @@ private fun UnknownAnimal(paddingValues: PaddingValues) {
private fun ShowAnimal(
state: PetDetailScreen.State.Success,
padding: PaddingValues,
) =
when (LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE) {
true -> ShowAnimalLandscape(state, padding)
false -> ShowAnimalPortrait(state, padding)
) {
val carouselContent = remember {
movableContentOf {
CircuitContent(
PetPhotoCarouselScreen(
name = state.name,
photoUrls = state.photoUrls,
photoUrlMemoryCacheKey = state.photoUrlMemoryCacheKey,
)
)
}
}
return when (LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE) {
true -> ShowAnimalLandscape(state, padding, carouselContent)
false -> ShowAnimalPortrait(state, padding, carouselContent)
}
}

@Composable
private fun ShowAnimalLandscape(state: PetDetailScreen.State.Success, padding: PaddingValues) {
private fun ShowAnimalLandscape(
state: PetDetailScreen.State.Success,
padding: PaddingValues,
carouselContent: @Composable () -> Unit,
) {
Row(
modifier = Modifier.padding(padding),
horizontalArrangement = spacedBy(16.dp),
) {
CircuitContent(
PetPhotoCarouselScreen(
name = state.name,
photoUrls = state.photoUrls,
photoUrlMemoryCacheKey = state.photoUrlMemoryCacheKey,
)
)
carouselContent()
LazyColumn(verticalArrangement = spacedBy(16.dp)) { petDetailDescriptions(state) }
}
}

@Composable
private fun ShowAnimalPortrait(state: PetDetailScreen.State.Success, padding: PaddingValues) {
private fun ShowAnimalPortrait(
state: PetDetailScreen.State.Success,
padding: PaddingValues,
carouselContent: @Composable () -> Unit,
) {
LazyColumn(
modifier = Modifier.padding(padding).testTag(ANIMAL_CONTAINER_TAG),
contentPadding = PaddingValues(16.dp),
verticalArrangement = spacedBy(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
item {
CircuitContent(
PetPhotoCarouselScreen(
name = state.name,
photoUrls = state.photoUrls,
photoUrlMemoryCacheKey = state.photoUrlMemoryCacheKey,
)
)
}
item { carouselContent() }
petDetailDescriptions(state)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
Expand Down Expand Up @@ -167,7 +168,7 @@ internal fun PetPhotoCarousel(state: PetPhotoCarouselScreen.State, modifier: Mod
pagerState = pagerState,
photoUrls = photoUrls,
name = name,
photoUrlMemoryCacheKey = photoUrlMemoryCacheKey
photoUrlMemoryCacheKey = photoUrlMemoryCacheKey,
)

HorizontalPagerIndicator(
Expand All @@ -187,24 +188,27 @@ private fun PagerState.calculateCurrentOffsetForPage(page: Int): Float {
return (currentPage - page) + currentPageOffsetFraction
}

@Suppress("LongParameterList")
@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun PhotoPager(
count: Int,
pagerState: PagerState,
photoUrls: ImmutableList<String>,
name: String,
modifier: Modifier = Modifier,
photoUrlMemoryCacheKey: String? = null,
) {
HorizontalPager(
pageCount = count,
state = pagerState,
key = photoUrls::get,
modifier = modifier,
contentPadding = PaddingValues(16.dp),
) { page ->
Card(
modifier =
Modifier.graphicsLayer {
Modifier.aspectRatio(1f).graphicsLayer {
// Calculate the absolute offset for the current page from the
// scroll position. We use the absolute value which allows us to mirror
// any effects for both directions
Expand Down Expand Up @@ -234,7 +238,7 @@ private fun PhotoPager(
}
.build(),
contentDescription = name,
contentScale = ContentScale.FillWidth,
contentScale = ContentScale.Crop,
)
}
}
Expand Down

0 comments on commit d4085f0

Please sign in to comment.