Skip to content

Commit

Permalink
Update media title margin for large screens. (#2111)
Browse files Browse the repository at this point in the history
* Update media title margin for large screens.
* Update screenshot tests.

---------

Co-authored-by: kpaikena <[email protected]>
  • Loading branch information
kpaikena and kpaikena authored Mar 12, 2024
1 parent 3fe51a9 commit c25f407
Show file tree
Hide file tree
Showing 34 changed files with 197 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.PlaceholderVerticalAlign
import androidx.compose.ui.text.buildAnnotatedString
Expand All @@ -46,9 +44,9 @@ import androidx.wear.compose.material.MaterialTheme
import androidx.wear.compose.material.Text
import com.google.android.horologist.annotations.ExperimentalHorologistApi
import com.google.android.horologist.composables.MarqueeText
import com.google.android.horologist.compose.material.Icon
import com.google.android.horologist.images.base.paintable.Paintable
import com.google.android.horologist.images.base.paintable.PaintableIcon
import com.google.android.horologist.media.ui.components.controls.MediaTitleIcon
import com.google.android.horologist.media.ui.util.isLargeScreen
import kotlin.math.roundToInt

/**
Expand All @@ -65,6 +63,8 @@ public fun MarqueeTextMediaDisplay(
subtextTransitionDelay: Int = 30,
@FloatRange(from = 0.0, to = 1.0) transitionLength: Float = 0.125f,
) {
val isLargeScreen = LocalConfiguration.current.isLargeScreen

fun getTransitionAnimation(delay: Int = 0): ContentTransform {
return slideInHorizontally(animationSpec = tween(delayMillis = delay + enterTransitionDelay)) {
(it * transitionLength).roundToInt()
Expand Down Expand Up @@ -105,7 +105,10 @@ public fun MarqueeTextMediaDisplay(
inlineContent = inlineContent,
modifier = Modifier
.fillMaxWidth(0.7f)
.padding(top = 2.dp, bottom = .8.dp),
.padding(
top = if (isLargeScreen) 0.dp else 2.dp,
bottom = if (isLargeScreen) 3.dp else 1.dp,
),
color = MaterialTheme.colors.onBackground,
style = textStyle,
textAlign = TextAlign.Center,
Expand All @@ -121,7 +124,7 @@ public fun MarqueeTextMediaDisplay(
text = currentArtist.orEmpty(),
modifier = Modifier
.fillMaxWidth(0.8f)
.padding(top = 2.dp, bottom = .6.dp),
.padding(top = 1.dp, bottom = .6.dp),
color = MaterialTheme.colors.onBackground,
textAlign = TextAlign.Center,
overflow = TextOverflow.Ellipsis,
Expand All @@ -131,22 +134,3 @@ public fun MarqueeTextMediaDisplay(
}
}
}

@Composable
private fun MediaTitleIcon(icon: Paintable) {
if (icon is PaintableIcon) {
Icon(
modifier = Modifier.fillMaxSize(),
paintable = icon,
contentDescription = null,
tint = MaterialTheme.colors.onBackground,
)
} else {
Image(
modifier = Modifier.fillMaxSize(),
painter = icon.rememberPainter(),
contentDescription = null,
contentScale = ContentScale.FillHeight,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2024 The Android Open Source Project
*
* 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
*
* https://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 com.google.android.horologist.media.ui.components.controls

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.wear.compose.material.MaterialTheme
import com.google.android.horologist.compose.material.Icon
import com.google.android.horologist.images.base.paintable.Paintable
import com.google.android.horologist.images.base.paintable.PaintableIcon

/**
* An icon to be shown in the media display before media title.
*/
@Composable
internal fun MediaTitleIcon(icon: Paintable) {
if (icon is PaintableIcon) {
Icon(
modifier = Modifier.fillMaxSize(),
paintable = icon,
contentDescription = null,
tint = MaterialTheme.colors.onBackground,
)
} else {
Image(
modifier = Modifier.fillMaxSize(),
painter = icon.rememberPainter(),
contentDescription = null,
contentScale = ContentScale.FillHeight,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.unit.dp
import androidx.wear.compose.material.ExperimentalWearMaterialApi
import androidx.wear.compose.material.MaterialTheme
Expand All @@ -36,6 +37,7 @@ import androidx.wear.compose.material.placeholderShimmer
import androidx.wear.compose.material.rememberPlaceholderState
import com.google.android.horologist.annotations.ExperimentalHorologistApi
import com.google.android.horologist.media.ui.components.animated.MarqueeTextMediaDisplay
import com.google.android.horologist.media.ui.util.isLargeScreen

/**
* A loading state display. This style is matched to the Text of [TextMediaDisplay] as
Expand All @@ -49,9 +51,12 @@ public fun LoadingMediaDisplay(
) {
// Always shimmer on the placeholder pills.
val placeholderState = rememberPlaceholderState(isContentReady = { false })
val isLargeScreen = LocalConfiguration.current.isLargeScreen

Column(modifier = modifier, horizontalAlignment = Alignment.CenterHorizontally) {
Spacer(modifier = Modifier.height(1.dp))
if (!isLargeScreen) {
Spacer(modifier = Modifier.height(1.dp))
}
Box(
modifier = Modifier
.clip(RoundedCornerShape(6.dp))
Expand All @@ -61,7 +66,11 @@ public fun LoadingMediaDisplay(
.width(120.dp)
.height(12.dp),
)
Spacer(modifier = Modifier.height(8.dp))
Spacer(
modifier = Modifier.height(
if (isLargeScreen) 9.dp else 8.dp,
),
)
Box(
modifier = Modifier
.clip(RoundedCornerShape(6.dp))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,27 @@

package com.google.android.horologist.media.ui.components.display

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.PlaceholderVerticalAlign
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.wear.compose.material.MaterialTheme
import androidx.wear.compose.material.Text
import com.google.android.horologist.annotations.ExperimentalHorologistApi
import com.google.android.horologist.images.base.paintable.Paintable
import com.google.android.horologist.media.ui.components.controls.MediaTitleIcon
import com.google.android.horologist.media.ui.util.isLargeScreen

/**
* A simple text only display showing artist and title in two separated rows.
Expand All @@ -47,35 +49,47 @@ public fun TextMediaDisplay(
titleIcon: Paintable? = null,
modifier: Modifier = Modifier,
) {
val isLargeScreen = LocalConfiguration.current.isLargeScreen

Column(modifier = modifier, horizontalAlignment = Alignment.CenterHorizontally) {
Row(
modifier = Modifier
.fillMaxWidth(0.7f)
.padding(top = 2.dp, bottom = .8.dp)
.semantics(mergeDescendants = true) {},
horizontalArrangement = Arrangement.spacedBy(4.dp),
) {
val textStyle = MaterialTheme.typography.button
val text = buildAnnotatedString {
if (titleIcon != null) {
Image(
painter = titleIcon.rememberPainter(),
contentDescription = null,
colorFilter = ColorFilter.tint(MaterialTheme.colors.onBackground),
contentScale = ContentScale.FillHeight,
)
appendInlineContent(id = "iconSlot")
append(" ")
}
Text(
text = title,
color = MaterialTheme.colors.onBackground,
textAlign = TextAlign.Center,
maxLines = 1,
style = MaterialTheme.typography.button,
append(title)
}
val inlineContent = if (titleIcon != null) {
mapOf(
"iconSlot" to InlineTextContent(
Placeholder(textStyle.fontSize, textStyle.fontSize, PlaceholderVerticalAlign.TextCenter),
) {
MediaTitleIcon(titleIcon)
},
)
} else {
emptyMap()
}
Text(
text = text,
inlineContent = inlineContent,
modifier = Modifier
.fillMaxWidth(0.7f)
.padding(
top = if (isLargeScreen) 0.dp else 2.dp,
bottom = if (isLargeScreen) 3.dp else 1.dp,
),
color = MaterialTheme.colors.onBackground,
textAlign = TextAlign.Center,
maxLines = 1,
style = textStyle,
)
Text(
text = subtitle,
modifier = Modifier
.fillMaxWidth(0.8f)
.padding(top = 2.dp, bottom = .6.dp),
.padding(top = 1.dp, bottom = .6.dp),
color = MaterialTheme.colors.onBackground,
textAlign = TextAlign.Center,
overflow = TextOverflow.Ellipsis,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.google.android.horologist.media.ui.components.display.TextMediaDispla
import com.google.android.horologist.screenshots.ScreenshotBaseTest
import com.google.android.horologist.screenshots.ScreenshotTestRule
import org.junit.Test
import org.robolectric.annotation.Config

class LoadingMediaDisplayTest : ScreenshotBaseTest(
ScreenshotTestRule.screenshotTestRuleParams {
Expand All @@ -38,15 +39,37 @@ class LoadingMediaDisplayTest : ScreenshotBaseTest(
}

@Test
fun loadingMediaDisplay_textMediaDisplay_overlay() {
fun loadingMediaDisplay_textMediaDisplay_overlay_largeScreen() {
screenshotTestRule.setContent(isComponent = true, takeScreenshot = true) {
LoadingMediaDisplay(modifier = Modifier.alpha(0.5f))
TextMediaDisplay(title = "Sorrow", subtitle = "David Bowie")
}
}

@Test
fun loadingMediaDisplay_marqueeTextMediaDisplay_overlay() {
fun loadingMediaDisplay_marqueeTextMediaDisplay_overlay_largeScreen() {
screenshotTestRule.setContent(isComponent = true, takeScreenshot = true) {
LoadingMediaDisplay(modifier = Modifier.alpha(0.5f))
MarqueeTextMediaDisplay(title = "Sorrow", artist = "David Bowie")
}
}

@Config(
qualifiers = "+w192dp-h192dp",
)
@Test
fun loadingMediaDisplay_textMediaDisplay_overlay_smallScreen() {
screenshotTestRule.setContent(isComponent = true, takeScreenshot = true) {
LoadingMediaDisplay(modifier = Modifier.alpha(0.5f))
TextMediaDisplay(title = "Sorrow", subtitle = "David Bowie")
}
}

@Config(
qualifiers = "+w192dp-h192dp",
)
@Test
fun loadingMediaDisplay_marqueeTextMediaDisplay_overlay_smallScreen() {
screenshotTestRule.setContent(isComponent = true, takeScreenshot = true) {
LoadingMediaDisplay(modifier = Modifier.alpha(0.5f))
MarqueeTextMediaDisplay(title = "Sorrow", artist = "David Bowie")
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c25f407

Please sign in to comment.