Skip to content

Commit

Permalink
Rename Applier to Config
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Dec 3, 2024
1 parent e692a7b commit c897ef3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import sergio.sastre.composable.preview.scanner.core.preview.ComposablePreview
fun ComposablePreview<AndroidPreviewInfo>.captureRoboImage(
filePath: String,
roborazziOptions: RoborazziOptions = provideRoborazziContext().options,
applierBuilder: RoborazziComposeApplierBuilder = RoborazziComposeApplierBuilder()
configBuilder: RoborazziComposeConfigBuilder = RoborazziComposeConfigBuilder()
.sized(
widthDp = previewInfo.widthDp,
heightDp = previewInfo.heightDp
Expand All @@ -29,17 +29,17 @@ fun ComposablePreview<AndroidPreviewInfo>.captureRoboImage(
) {
if (!roborazziOptions.taskType.isEnabled()) return
val composablePreview = this
captureRoboImage(filePath, roborazziOptions, applierBuilder) {
captureRoboImage(filePath, roborazziOptions, configBuilder) {
composablePreview()
}
}

fun RoborazziComposeApplierBuilder.device(device: String) = with(DeviceApplier(device))
fun RoborazziComposeConfigBuilder.device(device: String) = with(DeviceConfig(device))

@ExperimentalRoborazziApi
data class DeviceApplier(val device: String) :
RoborazziComposeSetupApplier {
override fun apply() {
data class DeviceConfig(val device: String) :
RoborazziComposeSetupConfig {
override fun configure() {
if (device.isNotBlank()) {
// Requires `io.github.sergio-sastre.ComposablePreviewScanner:android:0.4.0` or later
RobolectricDeviceQualifierBuilder.build(device)?.run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fun captureRoboImage(
captureRoboImage(
file = file,
roborazziOptions = roborazziOptions,
applierBuilder = RoborazziComposeApplierBuilder(),
configBuilder = RoborazziComposeConfigBuilder(),
content = content
)
}
Expand All @@ -39,13 +39,13 @@ fun captureRoboImage(
fun captureRoboImage(
filePath: String = DefaultFileNameGenerator.generateFilePath(),
roborazziOptions: RoborazziOptions = provideRoborazziContext().options,
applierBuilder: RoborazziComposeApplierBuilder = RoborazziComposeApplierBuilder(),
configBuilder: RoborazziComposeConfigBuilder = RoborazziComposeConfigBuilder(),
content: @Composable () -> Unit,
) {
captureRoboImage(
file = fileWithRecordFilePathStrategy(filePath),
roborazziOptions = roborazziOptions,
applierBuilder = applierBuilder,
configBuilder = configBuilder,
content = content
)
}
Expand All @@ -54,17 +54,17 @@ fun captureRoboImage(
fun captureRoboImage(
file: File,
roborazziOptions: RoborazziOptions = provideRoborazziContext().options,
applierBuilder: RoborazziComposeApplierBuilder = RoborazziComposeApplierBuilder(),
configBuilder: RoborazziComposeConfigBuilder = RoborazziComposeConfigBuilder(),
content: @Composable () -> Unit,
) {
if (!roborazziOptions.taskType.isEnabled()) return
launchRoborazziTransparentActivity { activityScenario ->
val appliedContent = applierBuilder
.apply(activityScenario) {
val configuredContent = configBuilder
.configure(activityScenario) {
content()
}
activityScenario.captureRoboImage(file, roborazziOptions) {
appliedContent()
configuredContent()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,53 @@ import org.robolectric.shadows.ShadowDisplay.getDefaultDisplay
import kotlin.math.roundToInt

@ExperimentalRoborazziApi
interface RoborazziComposeApplier
interface RoborazziComposeConfig

interface RoborazziComposeSetupApplier : RoborazziComposeApplier {
fun apply()
interface RoborazziComposeSetupConfig : RoborazziComposeConfig {
fun configure()
}

@ExperimentalRoborazziApi
interface RoborazziComposeActivityScenarioApplier : RoborazziComposeApplier {
fun applyToActivityScenario(scenario: ActivityScenario<out Activity>)
interface RoborazziComposeActivityScenarioConfig : RoborazziComposeConfig {
fun configureToActivityScenario(scenario: ActivityScenario<out Activity>)
}

@ExperimentalRoborazziApi
interface RoborazziComposeComposableApplier : RoborazziComposeApplier {
fun applyToComposable(content: @Composable () -> Unit): @Composable () -> Unit
interface RoborazziComposeComposableConfig : RoborazziComposeConfig {
fun configureToComposable(content: @Composable () -> Unit): @Composable () -> Unit
}


@ExperimentalRoborazziApi
class RoborazziComposeApplierBuilder {
private val activityScenarioAppliers =
mutableListOf<RoborazziComposeActivityScenarioApplier>()
private val composableAppliers = mutableListOf<RoborazziComposeComposableApplier>()
private val setupAppliers = mutableListOf<RoborazziComposeSetupApplier>()

fun with(applier: RoborazziComposeApplier): RoborazziComposeApplierBuilder {
if (applier is RoborazziComposeActivityScenarioApplier) {
activityScenarioAppliers.add(applier)
class RoborazziComposeConfigBuilder {
private val activityScenarioConfigs =
mutableListOf<RoborazziComposeActivityScenarioConfig>()
private val composableConfigs = mutableListOf<RoborazziComposeComposableConfig>()
private val setupConfigs = mutableListOf<RoborazziComposeSetupConfig>()

fun with(config: RoborazziComposeConfig): RoborazziComposeConfigBuilder {
if (config is RoborazziComposeActivityScenarioConfig) {
activityScenarioConfigs.add(config)
}
if (applier is RoborazziComposeComposableApplier) {
composableAppliers.add(applier)
if (config is RoborazziComposeComposableConfig) {
composableConfigs.add(config)
}
if (applier is RoborazziComposeSetupApplier) {
setupAppliers.add(applier)
if (config is RoborazziComposeSetupConfig) {
setupConfigs.add(config)
}
return this
}

@InternalRoborazziApi
fun apply(
fun configure(
scenario: ActivityScenario<out Activity>,
content: @Composable () -> Unit
): @Composable () -> Unit {
setupAppliers.forEach { it.apply() }
activityScenarioAppliers.forEach { it.applyToActivityScenario(scenario) }
setupConfigs.forEach { it.configure() }
activityScenarioConfigs.forEach { it.configureToActivityScenario(scenario) }
var appliedContent = content
composableAppliers.forEach { applier ->
appliedContent = applier.applyToComposable(appliedContent)
composableConfigs.forEach { config ->
appliedContent = config.configureToComposable(appliedContent)
}
return {
appliedContent()
Expand All @@ -73,15 +73,15 @@ class RoborazziComposeApplierBuilder {
}

@ExperimentalRoborazziApi
fun RoborazziComposeApplierBuilder.sized(widthDp: Int = 0, heightDp: Int = 0): RoborazziComposeApplierBuilder {
return with(RoborazziComposeSizeApplier(widthDp, heightDp))
fun RoborazziComposeConfigBuilder.sized(widthDp: Int = 0, heightDp: Int = 0): RoborazziComposeConfigBuilder {
return with(RoborazziComposeSizeConfig(widthDp, heightDp))
}

@ExperimentalRoborazziApi
data class RoborazziComposeSizeApplier(val widthDp: Int, val heightDp: Int) :
RoborazziComposeActivityScenarioApplier,
RoborazziComposeComposableApplier {
override fun applyToActivityScenario(scenario: ActivityScenario<out Activity>) {
data class RoborazziComposeSizeConfig(val widthDp: Int, val heightDp: Int) :
RoborazziComposeActivityScenarioConfig,
RoborazziComposeComposableConfig {
override fun configureToActivityScenario(scenario: ActivityScenario<out Activity>) {
scenario.onActivity { activity ->
activity.setDisplaySize(widthDp = widthDp, heightDp = heightDp)
}
Expand All @@ -106,7 +106,7 @@ data class RoborazziComposeSizeApplier(val widthDp: Int, val heightDp: Int) :
recreate()
}

override fun applyToComposable(content: @Composable () -> Unit): @Composable () -> Unit {
override fun configureToComposable(content: @Composable () -> Unit): @Composable () -> Unit {
/**
* WARNING:
* For this to work, it requires that the Display is within the widthDp and heightDp dimensions
Expand All @@ -127,19 +127,19 @@ data class RoborazziComposeSizeApplier(val widthDp: Int, val heightDp: Int) :
}

@ExperimentalRoborazziApi
fun RoborazziComposeApplierBuilder.colored(
fun RoborazziComposeConfigBuilder.colored(
showBackground: Boolean,
backgroundColor: Long = 0L
): RoborazziComposeApplierBuilder {
return with(RoborazziComposeBackgroundApplier(showBackground, backgroundColor))
): RoborazziComposeConfigBuilder {
return with(RoborazziComposeBackgroundConfig(showBackground, backgroundColor))
}

@ExperimentalRoborazziApi
data class RoborazziComposeBackgroundApplier(
data class RoborazziComposeBackgroundConfig(
val showBackground: Boolean,
val backgroundColor: Long
) : RoborazziComposeActivityScenarioApplier {
override fun applyToActivityScenario(scenario: ActivityScenario<out Activity>) {
) : RoborazziComposeActivityScenarioConfig {
override fun configureToActivityScenario(scenario: ActivityScenario<out Activity>) {
when (showBackground) {
false -> {
scenario.onActivity { activity ->
Expand All @@ -161,14 +161,14 @@ data class RoborazziComposeBackgroundApplier(
}

@ExperimentalRoborazziApi
fun RoborazziComposeApplierBuilder.uiMode(configurationUiMode: Int): RoborazziComposeApplierBuilder {
return with(UiModeApplier(configurationUiMode))
fun RoborazziComposeConfigBuilder.uiMode(configurationUiMode: Int): RoborazziComposeConfigBuilder {
return with(UiModeConfig(configurationUiMode))
}

@ExperimentalRoborazziApi
data class UiModeApplier(val uiMode: Int) :
RoborazziComposeSetupApplier {
override fun apply() {
data class UiModeConfig(val uiMode: Int) :
RoborazziComposeSetupConfig {
override fun configure() {
val nightMode =
when (uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES) {
true -> "night"
Expand All @@ -179,28 +179,28 @@ data class UiModeApplier(val uiMode: Int) :
}

@ExperimentalRoborazziApi
fun RoborazziComposeApplierBuilder.locale(bcp47LanguageTag: String): RoborazziComposeApplierBuilder {
return with(LocaleApplier(bcp47LanguageTag))
fun RoborazziComposeConfigBuilder.locale(bcp47LanguageTag: String): RoborazziComposeConfigBuilder {
return with(LocaleConfig(bcp47LanguageTag))
}

@ExperimentalRoborazziApi
data class LocaleApplier(val locale: String) :
RoborazziComposeSetupApplier {
override fun apply() {
data class LocaleConfig(val locale: String) :
RoborazziComposeSetupConfig {
override fun configure() {
val localeWithFallback = locale.ifBlank { "en" }
setQualifiers("+$localeWithFallback")
}
}

@ExperimentalRoborazziApi
fun RoborazziComposeApplierBuilder.fontScale(fontScale: Float): RoborazziComposeApplierBuilder {
return with(FontScaleApplier(fontScale))
fun RoborazziComposeConfigBuilder.fontScale(fontScale: Float): RoborazziComposeConfigBuilder {
return with(FontScaleConfig(fontScale))
}

@ExperimentalRoborazziApi
data class FontScaleApplier(val fontScale: Float) :
RoborazziComposeSetupApplier {
override fun apply() {
data class FontScaleConfig(val fontScale: Float) :
RoborazziComposeSetupConfig {
override fun configure() {
setFontScale(fontScale)
}
}

0 comments on commit c897ef3

Please sign in to comment.