Skip to content

Commit

Permalink
Merge pull request #38 from jeanbarrossilva/change/placeholder-conten…
Browse files Browse the repository at this point in the history
…t-with-box-scope

Provide `BoxScope` to `Placeholder` content
  • Loading branch information
jeanbarrossilva authored Jun 21, 2023
2 parents 1038377 + 004c750 commit 26416b7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class PlaceholderTests {
val composeRule = createComposeRule()

@Test
fun isLoadingWhenVisible() {
fun isLoadingWhenIsLoadingIsSetToTrue() {
composeRule.setContent { Placeholder(Modifier.tagAsPlaceholder()) }
composeRule.onPlaceholder().assertIsLoading()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.jeanbarrossilva.loadable.placeholder
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.assertHeightIsAtLeast
import androidx.compose.ui.test.assertWidthIsEqualTo
Expand Down Expand Up @@ -33,23 +34,27 @@ internal class TextualPlaceholderTests {
Modifier.tagAsPlaceholder(),
TextStyle(fontSize = 14.sp)
) {
Text(this)
Text(it)
}
}
composeRule.onPlaceholder().assertWidthIsEqualTo(screenWidth)
}

@Test
fun heightIsNotConstrainedIfItIsLoaded() {
val displayMetrics =
InstrumentationRegistry.getInstrumentation().context.resources.displayMetrics
val screenHeightInPx = InstrumentationRegistry
.getInstrumentation()
.context
.resources
.displayMetrics
.heightPixels
composeRule.setContent {
MediumTextualPlaceholder(
Loadable.Loaded("🙃".repeat(displayMetrics.heightPixels)),
Loadable.Loaded("🙃".repeat(screenHeightInPx)),
Modifier.tagAsPlaceholder(),
TextStyle(fontSize = 14.sp)
) {
Text(this)
Text(it)
}
}
composeRule.onPlaceholder().assertHeightIsAtLeast(24.dp)
Expand All @@ -62,7 +67,13 @@ internal class TextualPlaceholderTests {
Loadable.Loaded("🤌🏽"),
style = MaterialTheme.typography.headlineLarge
) {
assertEquals(MaterialTheme.typography.headlineLarge, LocalTextStyle.current)
val expected = MaterialTheme.typography.headlineLarge
val actual = LocalTextStyle.current

DisposableEffect(Unit) {
assertEquals(expected, actual)
onDispose { }
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jeanbarrossilva.loadable.placeholder

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.requiredHeight
import androidx.compose.foundation.layout.size
Expand Down Expand Up @@ -53,11 +54,11 @@ fun <T : Serializable?> Placeholder(
modifier: Modifier = Modifier,
shape: Shape = PlaceholderDefaults.shape,
color: Color = PlaceholderDefaults.color,
content: @Composable T.() -> Unit
content: @Composable BoxScope.(T) -> Unit
) {
Placeholder(modifier, isLoading = loadable is Loadable.Loading, shape, color) {
loadable.ifLoaded {
content()
content(this)
}
}
}
Expand All @@ -77,15 +78,14 @@ fun Placeholder(
isLoading: Boolean = true,
shape: Shape = PlaceholderDefaults.shape,
color: Color = PlaceholderDefaults.color,
content: @Composable () -> Unit = { }
content: @Composable BoxScope.() -> Unit = { }
) {
Box(
modifier
.placeholder(isLoading, color, shape, PlaceholderHighlight.shimmer())
.semantics { set(SemanticsProperties.Loading, isLoading) }
) {
content()
}
.semantics { set(SemanticsProperties.Loading, isLoading) },
content = content
)
}

/**
Expand Down Expand Up @@ -119,11 +119,11 @@ fun LargeTextualPlaceholder(
modifier: Modifier = Modifier,
style: TextStyle = LocalTextStyle.current,
color: Color = PlaceholderDefaults.color,
content: @Composable String.() -> Unit
content: @Composable (text: String) -> Unit
) {
LargeTextualPlaceholder(isLoading = loadable is Loadable.Loading, style, color, modifier) {
loadable.ifLoaded {
content()
content(this)
}
}
}
Expand All @@ -143,11 +143,11 @@ fun MediumTextualPlaceholder(
modifier: Modifier = Modifier,
style: TextStyle = LocalTextStyle.current,
color: Color = PlaceholderDefaults.color,
content: @Composable String.() -> Unit
content: @Composable (text: String) -> Unit
) {
MediumTextualPlaceholder(isLoading = loadable is Loadable.Loading, style, color, modifier) {
loadable.ifLoaded {
content()
content(this)
}
}
}
Expand Down Expand Up @@ -183,11 +183,11 @@ fun SmallTextualPlaceholder(
modifier: Modifier = Modifier,
style: TextStyle = LocalTextStyle.current,
color: Color = PlaceholderDefaults.color,
content: @Composable String.() -> Unit
content: @Composable (text: String) -> Unit
) {
SmallTextualPlaceholder(isLoading = loadable is Loadable.Loading, style, color, modifier) {
loadable.ifLoaded {
content()
content(this)
}
}
}
Expand Down

0 comments on commit 26416b7

Please sign in to comment.