diff --git a/samples/containers/android/src/main/java/com/squareup/sample/container/overviewdetail/OverviewDetailConfig.kt b/samples/containers/android/src/main/java/com/squareup/sample/container/overviewdetail/OverviewDetailConfig.kt index 4b83a5ee2..141424aa8 100644 --- a/samples/containers/android/src/main/java/com/squareup/sample/container/overviewdetail/OverviewDetailConfig.kt +++ b/samples/containers/android/src/main/java/com/squareup/sample/container/overviewdetail/OverviewDetailConfig.kt @@ -31,7 +31,7 @@ enum class OverviewDetailConfig { Single; @OptIn(WorkflowUiExperimentalApi::class) - companion object : ViewEnvironmentKey(OverviewDetailConfig::class) { + companion object : ViewEnvironmentKey() { override val default = None } } diff --git a/workflow-ui/compose-tooling/src/androidTest/java/com/squareup/workflow1/ui/compose/tooling/LegacyPreviewViewFactoryTest.kt b/workflow-ui/compose-tooling/src/androidTest/java/com/squareup/workflow1/ui/compose/tooling/LegacyPreviewViewFactoryTest.kt index 6338b6c94..0cb79e794 100644 --- a/workflow-ui/compose-tooling/src/androidTest/java/com/squareup/workflow1/ui/compose/tooling/LegacyPreviewViewFactoryTest.kt +++ b/workflow-ui/compose-tooling/src/androidTest/java/com/squareup/workflow1/ui/compose/tooling/LegacyPreviewViewFactoryTest.kt @@ -164,7 +164,7 @@ internal class LegacyPreviewViewFactoryTest { ) } - object TestEnvironmentKey : ViewEnvironmentKey(String::class) { + object TestEnvironmentKey : ViewEnvironmentKey() { override val default: String get() = error("Not specified") } diff --git a/workflow-ui/compose-tooling/src/androidTest/java/com/squareup/workflow1/ui/compose/tooling/PreviewViewFactoryTest.kt b/workflow-ui/compose-tooling/src/androidTest/java/com/squareup/workflow1/ui/compose/tooling/PreviewViewFactoryTest.kt index bba768ca6..5a9012044 100644 --- a/workflow-ui/compose-tooling/src/androidTest/java/com/squareup/workflow1/ui/compose/tooling/PreviewViewFactoryTest.kt +++ b/workflow-ui/compose-tooling/src/androidTest/java/com/squareup/workflow1/ui/compose/tooling/PreviewViewFactoryTest.kt @@ -194,7 +194,7 @@ internal class PreviewViewFactoryTest { ) } - object TestEnvironmentKey : ViewEnvironmentKey(String::class) { + object TestEnvironmentKey : ViewEnvironmentKey() { override val default: String get() = error("Not specified") } diff --git a/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/ComposeViewFactoryTest.kt b/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/ComposeViewFactoryTest.kt index 6f1aae60a..eaf1e0ef3 100644 --- a/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/ComposeViewFactoryTest.kt +++ b/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/ComposeViewFactoryTest.kt @@ -78,7 +78,7 @@ internal class ComposeViewFactoryTest { } @Test fun getsViewEnvironmentUpdates() { - val testEnvironmentKey = object : ViewEnvironmentKey(String::class) { + val testEnvironmentKey = object : ViewEnvironmentKey() { override val default: String get() = error("No default") } diff --git a/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/LegacyComposeViewFactoryTest.kt b/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/LegacyComposeViewFactoryTest.kt index 8ee54914f..68f7c865d 100644 --- a/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/LegacyComposeViewFactoryTest.kt +++ b/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/LegacyComposeViewFactoryTest.kt @@ -79,7 +79,7 @@ internal class LegacyComposeViewFactoryTest { } @Test fun getsViewEnvironmentUpdates() { - val testEnvironmentKey = object : ViewEnvironmentKey(String::class) { + val testEnvironmentKey = object : ViewEnvironmentKey() { override val default: String get() = error("No default") } diff --git a/workflow-ui/core-android/api/core-android.api b/workflow-ui/core-android/api/core-android.api index 2b6917385..d403a64cb 100644 --- a/workflow-ui/core-android/api/core-android.api +++ b/workflow-ui/core-android/api/core-android.api @@ -334,24 +334,6 @@ public final class com/squareup/workflow1/ui/container/BackButtonScreen : com/sq public fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/container/BackButtonScreen; } -public final class com/squareup/workflow1/ui/container/BackStackConfig : java/lang/Enum { - public static final field Companion Lcom/squareup/workflow1/ui/container/BackStackConfig$Companion; - public static final field First Lcom/squareup/workflow1/ui/container/BackStackConfig; - public static final field None Lcom/squareup/workflow1/ui/container/BackStackConfig; - public static final field Other Lcom/squareup/workflow1/ui/container/BackStackConfig; - public static fun valueOf (Ljava/lang/String;)Lcom/squareup/workflow1/ui/container/BackStackConfig; - public static fun values ()[Lcom/squareup/workflow1/ui/container/BackStackConfig; -} - -public final class com/squareup/workflow1/ui/container/BackStackConfig$Companion : com/squareup/workflow1/ui/ViewEnvironmentKey { - public fun getDefault ()Lcom/squareup/workflow1/ui/container/BackStackConfig; - public synthetic fun getDefault ()Ljava/lang/Object; -} - -public final class com/squareup/workflow1/ui/container/BackStackConfigKt { - public static final fun plus (Lcom/squareup/workflow1/ui/ViewEnvironment;Lcom/squareup/workflow1/ui/container/BackStackConfig;)Lcom/squareup/workflow1/ui/ViewEnvironment; -} - public class com/squareup/workflow1/ui/container/BackStackContainer : android/widget/FrameLayout { public fun (Landroid/content/Context;)V public fun (Landroid/content/Context;Landroid/util/AttributeSet;)V diff --git a/workflow-ui/core-android/src/androidTest/java/com/squareup/workflow1/ui/DecorativeViewFactoryTest.kt b/workflow-ui/core-android/src/androidTest/java/com/squareup/workflow1/ui/DecorativeViewFactoryTest.kt index 841b80d0e..f1cfa27c0 100644 --- a/workflow-ui/core-android/src/androidTest/java/com/squareup/workflow1/ui/DecorativeViewFactoryTest.kt +++ b/workflow-ui/core-android/src/androidTest/java/com/squareup/workflow1/ui/DecorativeViewFactoryTest.kt @@ -30,7 +30,7 @@ internal class DecorativeViewFactoryTest { } } - val envString = object : ViewEnvironmentKey(String::class) { + val envString = object : ViewEnvironmentKey() { override val default: String get() = "Not set" } @@ -120,7 +120,7 @@ internal class DecorativeViewFactoryTest { } } - val envString = object : ViewEnvironmentKey(String::class) { + val envString = object : ViewEnvironmentKey() { override val default: String get() = "Not set" } diff --git a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewFactoryFinder.kt b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewFactoryFinder.kt index aa1e021b6..c55902c68 100644 --- a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewFactoryFinder.kt +++ b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewFactoryFinder.kt @@ -85,9 +85,7 @@ public interface ScreenViewFactoryFinder { ) } - public companion object : ViewEnvironmentKey( - ScreenViewFactoryFinder::class - ) { + public companion object : ViewEnvironmentKey() { override val default: ScreenViewFactoryFinder get() = object : ScreenViewFactoryFinder {} } diff --git a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/AlertDialogThemeResId.kt b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/AlertDialogThemeResId.kt index f450840e0..97b02ba0f 100644 --- a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/AlertDialogThemeResId.kt +++ b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/AlertDialogThemeResId.kt @@ -8,6 +8,6 @@ import com.squareup.workflow1.ui.WorkflowUiExperimentalApi * for the `@StyleRes themeResId: Int` argument of `AlertDialog.Builder()`. */ @WorkflowUiExperimentalApi -public object AlertDialogThemeResId : ViewEnvironmentKey(type = Int::class) { +public object AlertDialogThemeResId : ViewEnvironmentKey() { override val default: Int = 0 } diff --git a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/CoveredByModal.kt b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/CoveredByModal.kt index d805a44d1..e0b9e2093 100644 --- a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/CoveredByModal.kt +++ b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/CoveredByModal.kt @@ -12,6 +12,6 @@ import com.squareup.workflow1.ui.WorkflowUiExperimentalApi * https://stackoverflow.com/questions/2886407/dealing-with-rapid-tapping-on-buttons */ @WorkflowUiExperimentalApi -internal object CoveredByModal : ViewEnvironmentKey(type = Boolean::class) { +internal object CoveredByModal : ViewEnvironmentKey() { override val default: Boolean = false } diff --git a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/DialogCollator.kt b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/DialogCollator.kt index 4f70d9803..3350b4dac 100644 --- a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/DialogCollator.kt +++ b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/DialogCollator.kt @@ -254,7 +254,7 @@ internal class DialogCollator { ")" } - companion object : ViewEnvironmentKey(DialogCollator::class) { + companion object : ViewEnvironmentKey() { override val default: DialogCollator get() = error("Call ViewEnvironment.establishDialogCollator first.") } diff --git a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/OverlayArea.kt b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/OverlayArea.kt index cbc95a013..47feb3965 100644 --- a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/OverlayArea.kt +++ b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/OverlayArea.kt @@ -16,7 +16,7 @@ import kotlinx.coroutines.flow.StateFlow internal class OverlayArea( val bounds: StateFlow ) { - companion object : ViewEnvironmentKey(type = OverlayArea::class) { + companion object : ViewEnvironmentKey() { override val default: OverlayArea = OverlayArea(MutableStateFlow(Rect())) } } diff --git a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/OverlayDialogFactoryFinder.kt b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/OverlayDialogFactoryFinder.kt index 81e91a032..f2425bf4b 100644 --- a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/OverlayDialogFactoryFinder.kt +++ b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/OverlayDialogFactoryFinder.kt @@ -36,9 +36,7 @@ public interface OverlayDialogFactoryFinder { ) } - public companion object : ViewEnvironmentKey( - OverlayDialogFactoryFinder::class - ) { + public companion object : ViewEnvironmentKey() { override val default: OverlayDialogFactoryFinder = object : OverlayDialogFactoryFinder {} } } diff --git a/workflow-ui/core-android/src/test/java/com/squareup/workflow1/ui/container/ScreenViewFactoryTestUtil.kt b/workflow-ui/core-android/src/test/java/com/squareup/workflow1/ui/container/ScreenViewFactoryTestUtil.kt index 6ffb7718a..5039372ae 100644 --- a/workflow-ui/core-android/src/test/java/com/squareup/workflow1/ui/container/ScreenViewFactoryTestUtil.kt +++ b/workflow-ui/core-android/src/test/java/com/squareup/workflow1/ui/container/ScreenViewFactoryTestUtil.kt @@ -32,7 +32,7 @@ internal fun mockView(): View { } } -internal object SomeEnvValue : ViewEnvironmentKey(String::class) { +internal object SomeEnvValue : ViewEnvironmentKey() { override val default: String get() = error("Unset") } diff --git a/workflow-ui/core-common/api/core-common.api b/workflow-ui/core-common/api/core-common.api index 65670738d..b14bd88ec 100644 --- a/workflow-ui/core-common/api/core-common.api +++ b/workflow-ui/core-common/api/core-common.api @@ -99,12 +99,12 @@ public final class com/squareup/workflow1/ui/ViewEnvironment$Companion { } public abstract class com/squareup/workflow1/ui/ViewEnvironmentKey { + public fun ()V public fun (Lkotlin/reflect/KClass;)V public fun combine (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public final fun equals (Ljava/lang/Object;)Z public abstract fun getDefault ()Ljava/lang/Object; public final fun hashCode ()I - public final fun toString ()Ljava/lang/String; } public abstract interface class com/squareup/workflow1/ui/ViewRegistry { @@ -194,6 +194,24 @@ public final class com/squareup/workflow1/ui/container/AlertOverlay$Event$Cancel public static final field INSTANCE Lcom/squareup/workflow1/ui/container/AlertOverlay$Event$Canceled; } +public final class com/squareup/workflow1/ui/container/BackStackConfig : java/lang/Enum { + public static final field Companion Lcom/squareup/workflow1/ui/container/BackStackConfig$Companion; + public static final field First Lcom/squareup/workflow1/ui/container/BackStackConfig; + public static final field None Lcom/squareup/workflow1/ui/container/BackStackConfig; + public static final field Other Lcom/squareup/workflow1/ui/container/BackStackConfig; + public static fun valueOf (Ljava/lang/String;)Lcom/squareup/workflow1/ui/container/BackStackConfig; + public static fun values ()[Lcom/squareup/workflow1/ui/container/BackStackConfig; +} + +public final class com/squareup/workflow1/ui/container/BackStackConfig$Companion : com/squareup/workflow1/ui/ViewEnvironmentKey { + public fun getDefault ()Lcom/squareup/workflow1/ui/container/BackStackConfig; + public synthetic fun getDefault ()Ljava/lang/Object; +} + +public final class com/squareup/workflow1/ui/container/BackStackConfigKt { + public static final fun plus (Lcom/squareup/workflow1/ui/ViewEnvironment;Lcom/squareup/workflow1/ui/container/BackStackConfig;)Lcom/squareup/workflow1/ui/ViewEnvironment; +} + public final class com/squareup/workflow1/ui/container/BackStackScreen : com/squareup/workflow1/ui/Container, com/squareup/workflow1/ui/Screen { public fun (Lcom/squareup/workflow1/ui/Screen;Ljava/util/List;)V public fun (Lcom/squareup/workflow1/ui/Screen;[Lcom/squareup/workflow1/ui/Screen;)V diff --git a/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ViewEnvironment.kt b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ViewEnvironment.kt index bb7d08492..3508f820d 100644 --- a/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ViewEnvironment.kt +++ b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ViewEnvironment.kt @@ -62,13 +62,23 @@ constructor( } /** - * Defines a value that can be provided by a [ViewEnvironment] map, specifying its [type] - * and [default] value. + * Defines a value type [T] that can be provided by a [ViewEnvironment] map, + * and specifies its [default] value. + * + * It is hard to imagine a useful implementation of this that is not a Kotlin `object`. + * Preferred use is to have the `companion object` of [T] extend this class. See + * [BackStackConfig.Companion][com.squareup.workflow1.ui.container.BackStackConfig.Companion] + * for an example. */ @WorkflowUiExperimentalApi -public abstract class ViewEnvironmentKey( - private val type: KClass -) { +public abstract class ViewEnvironmentKey() { + @Deprecated("Use no args constructor", ReplaceWith("ViewEnvironmentKey()")) + public constructor(@Suppress("UNUSED_PARAMETER") type: KClass) : this() + + /** + * Defines the default value for this key. It is a grievous error for this value to be + * dynamic in any way. + */ public abstract val default: T /** @@ -80,15 +90,9 @@ public abstract class ViewEnvironmentKey( right: T ): T = right - final override fun equals(other: Any?): Boolean = when { - this === other -> true - other != null && this::class != other::class -> false - else -> type == (other as ViewEnvironmentKey<*>).type + final override fun equals(other: Any?): Boolean { + return this === other || (other != null && this::class == other::class) } - final override fun hashCode(): Int = type.hashCode() - - final override fun toString(): String { - return "${this::class.simpleName}(${type.simpleName})" - } + final override fun hashCode(): Int = this::class.hashCode() } diff --git a/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ViewRegistry.kt b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ViewRegistry.kt index d90d37ab0..d4566d084 100644 --- a/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ViewRegistry.kt +++ b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ViewRegistry.kt @@ -81,7 +81,7 @@ public interface ViewRegistry { renderingType: KClass ): Entry? - public companion object : ViewEnvironmentKey(ViewRegistry::class) { + public companion object : ViewEnvironmentKey() { override val default: ViewRegistry get() = ViewRegistry() override fun combine( left: ViewRegistry, diff --git a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/BackStackConfig.kt b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/container/BackStackConfig.kt similarity index 88% rename from workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/BackStackConfig.kt rename to workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/container/BackStackConfig.kt index 7fb5c2949..17c912aeb 100644 --- a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/BackStackConfig.kt +++ b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/container/BackStackConfig.kt @@ -7,13 +7,13 @@ import com.squareup.workflow1.ui.container.BackStackConfig.First import com.squareup.workflow1.ui.container.BackStackConfig.Other /** - * Informs views whether they're children of a [BackStackContainer], + * Informs views whether they're children of a [BackStackScreen], * and if so whether they're the [first frame][First] or [not][Other]. */ @WorkflowUiExperimentalApi public enum class BackStackConfig { /** - * There is no [BackStackContainer] above here. + * There is no [BackStackScreen] above here. */ None, @@ -29,7 +29,7 @@ public enum class BackStackConfig { */ Other; - public companion object : ViewEnvironmentKey(BackStackConfig::class) { + public companion object : ViewEnvironmentKey() { override val default: BackStackConfig = None } } diff --git a/workflow-ui/core-common/src/test/java/com/squareup/workflow1/ui/ViewEnvironmentTest.kt b/workflow-ui/core-common/src/test/java/com/squareup/workflow1/ui/ViewEnvironmentTest.kt index 063aee10f..6f8186445 100644 --- a/workflow-ui/core-common/src/test/java/com/squareup/workflow1/ui/ViewEnvironmentTest.kt +++ b/workflow-ui/core-common/src/test/java/com/squareup/workflow1/ui/ViewEnvironmentTest.kt @@ -6,11 +6,11 @@ import org.junit.Test @OptIn(WorkflowUiExperimentalApi::class) internal class ViewEnvironmentTest { - private object StringHint : ViewEnvironmentKey(String::class) { + private object StringHint : ViewEnvironmentKey() { override val default = "" } - private object OtherStringHint : ViewEnvironmentKey(String::class) { + private object OtherStringHint : ViewEnvironmentKey() { override val default = "" } @@ -18,7 +18,7 @@ internal class ViewEnvironmentTest { val int: Int = -1, val string: String = "" ) { - companion object : ViewEnvironmentKey(DataHint::class) { + companion object : ViewEnvironmentKey() { override val default = DataHint() } } @@ -101,7 +101,7 @@ internal class ViewEnvironmentTest { } @Test fun `honors combine`() { - val combiningHint = object : ViewEnvironmentKey(String::class) { + val combiningHint = object : ViewEnvironmentKey() { override val default: String get() = error("") diff --git a/workflow-ui/core-common/src/test/java/com/squareup/workflow1/ui/container/EnvironmentScreenTest.kt b/workflow-ui/core-common/src/test/java/com/squareup/workflow1/ui/container/EnvironmentScreenTest.kt index 406c3cd0f..f1fc9fdf6 100644 --- a/workflow-ui/core-common/src/test/java/com/squareup/workflow1/ui/container/EnvironmentScreenTest.kt +++ b/workflow-ui/core-common/src/test/java/com/squareup/workflow1/ui/container/EnvironmentScreenTest.kt @@ -19,7 +19,7 @@ internal class EnvironmentScreenTest { ) : ViewRegistry.Entry private data class TestValue(val value: String) { - companion object : ViewEnvironmentKey(TestValue::class) { + companion object : ViewEnvironmentKey() { override val default: TestValue get() = error("Set a default") } }