Skip to content

Commit

Permalink
Provide Theme override option to DisplayableAsset and rename
Browse files Browse the repository at this point in the history
  • Loading branch information
janseeger committed Mar 6, 2024
1 parent ab2ed71 commit 6c744a2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package de.sipgate.dachlatten.compose.asset

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import de.sipgate.dachlatten.asset.DisplayableAsset
import de.sipgate.dachlatten.asset.AssetUrl
import de.sipgate.dachlatten.asset.DisplayableAsset

@Composable
fun DisplayableAsset.getThemeDependingUri(): AssetUrl =
when {
isSystemInDarkTheme() -> dark ?: light
else -> light
}
fun DisplayableAsset.resolve(
useDarkMode: Boolean = isSystemInDarkTheme()
): AssetUrl = when {
useDarkMode -> dark ?: light
else -> light
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,19 @@ class DisplayableAssetTest {
expectResolvedComposeUrl(lightModeVariantUrl, image)
}

private fun expectResolvedComposeUrl(expected: String, image: DisplayableAsset) {
@Test
@Config(qualifiers = "notnight")
fun urlResolvesToDarkVariantInLightModeWhenExplicitlyRequested() {
expectResolvedComposeUrl(darkModeVariantUrl, image, true)
}

private fun expectResolvedComposeUrl(expected: String, image: DisplayableAsset, useDarkMode: Boolean? = null) {
composeTestRule.setContent {
val resolvedString = image.getThemeDependingUri()
val resolvedString = if (useDarkMode != null) {
image.resolve(useDarkMode)
} else {
image.resolve()
}
assertEquals(expected, resolvedString)
}
}
Expand Down

0 comments on commit 6c744a2

Please sign in to comment.