diff --git a/roborazzi-compose-preview-scanner-support/src/main/java/com/github/takahirom/roborazzi/RoborazziPreviewScannerSupport.kt b/roborazzi-compose-preview-scanner-support/src/main/java/com/github/takahirom/roborazzi/RoborazziPreviewScannerSupport.kt index 9251adc1..7c9c1510 100644 --- a/roborazzi-compose-preview-scanner-support/src/main/java/com/github/takahirom/roborazzi/RoborazziPreviewScannerSupport.kt +++ b/roborazzi-compose-preview-scanner-support/src/main/java/com/github/takahirom/roborazzi/RoborazziPreviewScannerSupport.kt @@ -13,7 +13,7 @@ import sergio.sastre.composable.preview.scanner.core.preview.ComposablePreview fun ComposablePreview.captureRoboImage( filePath: String, roborazziOptions: RoborazziOptions = provideRoborazziContext().options, - applierBuilder: RoborazziComposeApplierBuilder = RoborazziComposeApplierBuilder() + configBuilder: RoborazziComposeConfigBuilder = RoborazziComposeConfigBuilder() .sized( widthDp = previewInfo.widthDp, heightDp = previewInfo.heightDp @@ -29,17 +29,17 @@ fun ComposablePreview.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 { diff --git a/roborazzi-compose/src/main/java/com/github/takahirom/roborazzi/RoborazziCompose.kt b/roborazzi-compose/src/main/java/com/github/takahirom/roborazzi/RoborazziCompose.kt index 9699b95b..b3a1f4ff 100644 --- a/roborazzi-compose/src/main/java/com/github/takahirom/roborazzi/RoborazziCompose.kt +++ b/roborazzi-compose/src/main/java/com/github/takahirom/roborazzi/RoborazziCompose.kt @@ -30,7 +30,7 @@ fun captureRoboImage( captureRoboImage( file = file, roborazziOptions = roborazziOptions, - applierBuilder = RoborazziComposeApplierBuilder(), + configBuilder = RoborazziComposeConfigBuilder(), content = content ) } @@ -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 ) } @@ -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() } } } diff --git a/roborazzi-compose/src/main/java/com/github/takahirom/roborazzi/RoborazziComposeApplier.kt b/roborazzi-compose/src/main/java/com/github/takahirom/roborazzi/RoborazziComposeConfig.kt similarity index 53% rename from roborazzi-compose/src/main/java/com/github/takahirom/roborazzi/RoborazziComposeApplier.kt rename to roborazzi-compose/src/main/java/com/github/takahirom/roborazzi/RoborazziComposeConfig.kt index 50c7feb0..06899f2b 100644 --- a/roborazzi-compose/src/main/java/com/github/takahirom/roborazzi/RoborazziComposeApplier.kt +++ b/roborazzi-compose/src/main/java/com/github/takahirom/roborazzi/RoborazziComposeConfig.kt @@ -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) +interface RoborazziComposeActivityScenarioConfig : RoborazziComposeConfig { + fun configureToActivityScenario(scenario: ActivityScenario) } @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() - private val composableAppliers = mutableListOf() - private val setupAppliers = mutableListOf() - - fun with(applier: RoborazziComposeApplier): RoborazziComposeApplierBuilder { - if (applier is RoborazziComposeActivityScenarioApplier) { - activityScenarioAppliers.add(applier) +class RoborazziComposeConfigBuilder { + private val activityScenarioConfigs = + mutableListOf() + private val composableConfigs = mutableListOf() + private val setupConfigs = mutableListOf() + + 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, 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() @@ -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) { +data class RoborazziComposeSizeConfig(val widthDp: Int, val heightDp: Int) : + RoborazziComposeActivityScenarioConfig, + RoborazziComposeComposableConfig { + override fun configureToActivityScenario(scenario: ActivityScenario) { scenario.onActivity { activity -> activity.setDisplaySize(widthDp = widthDp, heightDp = heightDp) } @@ -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 @@ -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) { +) : RoborazziComposeActivityScenarioConfig { + override fun configureToActivityScenario(scenario: ActivityScenario) { when (showBackground) { false -> { scenario.onActivity { activity -> @@ -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" @@ -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) } } \ No newline at end of file