Skip to content

Commit

Permalink
add Paparazzi tests for the LanguageDownloadProgressIndicator
Browse files Browse the repository at this point in the history
  • Loading branch information
frett committed Mar 8, 2024
1 parent d82aa9e commit b305ccf
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package org.cru.godtools.ui.languages.downloadable

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
Expand All @@ -14,20 +17,23 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp

private val DEFAULT_ICON_SIZE = 24.dp

@Composable
internal fun LanguageDownloadProgressIndicator(
isPinned: Boolean,
downloaded: Int,
total: Int,
modifier: Modifier = Modifier,
iconSize: Dp = 24.dp,
) {
val total = total.coerceAtLeast(0)
val downloaded = downloaded.coerceIn(0, total)
val contentModifier = modifier.size(iconSize)
val contentModifier = modifier
.size(DEFAULT_ICON_SIZE)
.aspectRatio(1f)

when {
!isPinned -> Icon(
Expand All @@ -51,15 +57,19 @@ internal fun LanguageDownloadProgressIndicator(
},
)

val iconPadding = iconSize / 12
CircularProgressIndicator(
progress = { progress },
color = MaterialTheme.colorScheme.primary,
strokeWidth = (iconSize / 2) - iconPadding,
modifier = contentModifier
.padding(iconPadding)
.border(iconSize / 12, MaterialTheme.colorScheme.primary, CircleShape)
)
BoxWithConstraints(contentModifier) {
val size = with(LocalDensity.current) { constraints.maxWidth.toDp() }
val iconPadding = size / 12
CircularProgressIndicator(
progress = { progress },
color = MaterialTheme.colorScheme.primary,
strokeWidth = (size / 2) - iconPadding,
modifier = Modifier
.fillMaxSize()
.padding(iconPadding)
.border(size / 12, MaterialTheme.colorScheme.primary, CircleShape)
)
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.cru.godtools.ui.languages.downloadable

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import app.cash.paparazzi.Paparazzi
import kotlin.test.Test
import org.cru.godtools.base.ui.theme.GodToolsTheme
import org.junit.Rule

class LanguageDownloadProgressIndicatorPaparazziTest {
@get:Rule
val paparazzi = Paparazzi()

@Test
fun `LanguageDownloadProgressIndicator()`() {
paparazzi.snapshot {
GodToolsTheme(disableDagger = true) {
Box {
Row(Modifier.align(Alignment.Center)) {
for (it in 0..5) {
LanguageDownloadProgressIndicator(isPinned = true, downloaded = it, total = 5)
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import org.greenrobot.eventbus.EventBus
val LocalEventBus = staticCompositionLocalOf { EventBus() }

@Composable
internal fun CompositionLocals(content: @Composable () -> Unit) {
internal fun CompositionLocals(disableDagger: Boolean = false, content: @Composable () -> Unit) {
val context = LocalContext.current
val daggerComponents = when {
LocalInspectionMode.current -> object : ComposeEntryPoint {
override val eventBus = EventBus()
LocalInspectionMode.current || disableDagger -> remember {
object : ComposeEntryPoint {
override val eventBus = EventBus()
}
}
else -> remember { EntryPointAccessors.fromApplication<ComposeEntryPoint>(context) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ object GodToolsTheme {
}

@Composable
fun GodToolsTheme(content: @Composable () -> Unit) {
fun GodToolsTheme(disableDagger: Boolean = false, content: @Composable () -> Unit) {
val isDarkTheme = isSystemInDarkTheme() && BuildConfig.DEBUG
MaterialTheme(
colorScheme = when {
Expand All @@ -123,7 +123,7 @@ fun GodToolsTheme(content: @Composable () -> Unit) {
},
typography = GodToolsTheme.typography
) {
CompositionLocals {
CompositionLocals(disableDagger) {
CompositionLocalProvider(
LocalLightColorSchemeActive provides !isDarkTheme,
LocalContentColor provides contentColorFor(MaterialTheme.colorScheme.background),
Expand Down

0 comments on commit b305ccf

Please sign in to comment.