Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Tidy up related Compose UI
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes committed Jun 1, 2020
1 parent 83a78e2 commit 8c563eb
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2020 Google LLC
*
* 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.
*/

package app.tivi.common.compose

import androidx.compose.Composable
import androidx.ui.core.Modifier
import androidx.ui.foundation.HorizontalScroller
import androidx.ui.layout.RowScope
import androidx.ui.layout.Spacer
import androidx.ui.layout.preferredWidth
import androidx.ui.unit.Dp
import androidx.ui.unit.dp

@Composable
fun <T> HorizontalCollectionScroller(
items: List<T>,
startEndSpacing: Dp = 16.dp,
childSpacing: Dp = 4.dp,
modifier: Modifier = Modifier,
children: @Composable RowScope.(item: T) -> Unit
) {
HorizontalScroller(modifier = modifier) {
items.forEachIndexed { index, item ->
if (index == 0) {
// First item, so lets add the starting margin
Spacer(modifier = Modifier.preferredWidth(startEndSpacing))
}

children(item)

if (index < items.lastIndex) {
// Add a spacer if there are still more items to add
Spacer(modifier = Modifier.preferredWidth(childSpacing))
} else {
// Last item, so lets add the end margin
Spacer(modifier = Modifier.preferredWidth(startEndSpacing))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import androidx.ui.core.onPositioned
import androidx.ui.core.positionInRoot
import androidx.ui.core.setContent
import androidx.ui.foundation.Box
import androidx.ui.foundation.HorizontalScroller
import androidx.ui.foundation.Icon
import androidx.ui.foundation.ScrollerPosition
import androidx.ui.foundation.Text
Expand Down Expand Up @@ -102,6 +101,7 @@ import androidx.ui.unit.IntPx
import androidx.ui.unit.dp
import app.tivi.common.compose.AutoSizedCircularProgressIndicator
import app.tivi.common.compose.ExpandingText
import app.tivi.common.compose.HorizontalCollectionScroller
import app.tivi.common.compose.InsetsAmbient
import app.tivi.common.compose.PopupMenu
import app.tivi.common.compose.PopupMenuItem
Expand Down Expand Up @@ -585,36 +585,32 @@ private fun RelatedShows(
) {
// TODO: ideally we would use AdapterList here, but it only works for vertical lists, not
// horizontal
HorizontalScroller(modifier = modifier.padding(vertical = 8.dp)) {
Row(modifier.padding(horizontal = 16.dp)) {
related.forEachIndexed { index, relatedEntry ->
Card(
Modifier.aspectRatio(2 / 3f)
.preferredWidth(64.dp)
.clickable { actioner(OpenShowDetails(relatedEntry.show.id)) }
) {
Stack {
ProvideEmphasis(EmphasisAmbient.current.medium) {
Text(
text = relatedEntry.show.title ?: "No title",
style = MaterialTheme.typography.caption,
modifier = Modifier.padding(4.dp)
.gravity(Alignment.CenterStart)
)
}
val poster = relatedEntry.images.findHighestRatedPoster()
if (poster != null) {
CoilImageWithCrossfade(
poster,
modifier = Modifier.fillMaxSize()
)
}
}
HorizontalCollectionScroller(
items = related,
modifier = modifier
) { item ->
Card(
modifier = Modifier.padding(vertical = 8.dp)
.preferredWidth(64.dp)
.aspectRatio(2 / 3f)
) {
Stack(
Modifier.clickable { actioner(OpenShowDetails(item.show.id)) }
) {
ProvideEmphasis(EmphasisAmbient.current.medium) {
Text(
text = item.show.title ?: "No title",
style = MaterialTheme.typography.caption,
modifier = Modifier.padding(4.dp)
.gravity(Alignment.CenterStart)
)
}

if (index + 1 < related.size) {
// Add a spacer if there are still more items to add
Spacer(modifier = Modifier.preferredWidth(4.dp))
val poster = item.images.findHighestRatedPoster()
if (poster != null) {
CoilImageWithCrossfade(
poster,
modifier = Modifier.fillMaxSize()
)
}
}
}
Expand Down

0 comments on commit 8c563eb

Please sign in to comment.