diff --git a/Sample/feature_b/src/main/java/com/veepee/feature/b/routes/FeatureBComposableNameMapper.kt b/Sample/feature_b/src/main/java/com/veepee/feature/b/routes/FeatureBComposableNameMapper.kt index b554061..dfaba3b 100644 --- a/Sample/feature_b/src/main/java/com/veepee/feature/b/routes/FeatureBComposableNameMapper.kt +++ b/Sample/feature_b/src/main/java/com/veepee/feature/b/routes/FeatureBComposableNameMapper.kt @@ -23,6 +23,7 @@ import androidx.compose.ui.Modifier import com.veepee.routes.feature_b.ComposableBNames import com.veepee.routes.feature_b.FeatureBComposableEvent import com.veepee.routes.feature_b.FeatureBComposableLink +import com.veepee.vpcore.route.link.compose.ComposableEvent import com.veepee.vpcore.route.link.compose.ComposableLink import com.veepee.vpcore.route.link.compose.ComposableNameMapper import com.veepee.vpcore.route.link.compose.events.LocalLinkRouterEventHandler @@ -33,15 +34,15 @@ object FeatureBComposableNameMapper : ComposableNameMapper { @Composable override fun Map( - composableLink: ComposableLink, + link: ComposableLink, modifier: Modifier ) { val handler = LocalLinkRouterEventHandler.current - when (composableLink) { + when (link) { is FeatureBComposableLink -> BasicText( - composableLink.parameter.message, + link.parameter.message, modifier.clickable { - handler.publish(FeatureBComposableEvent(composableLink.parameter.message.length)) + handler.publish(FeatureBComposableEvent(link.parameter.message.length)) }) } } diff --git a/Sample/routes/src/main/java/com/veepee/routes/feature_b/FeatureBComposableLink.kt b/Sample/routes/src/main/java/com/veepee/routes/feature_b/FeatureBComposableLink.kt index 6f9c717..81f3f25 100644 --- a/Sample/routes/src/main/java/com/veepee/routes/feature_b/FeatureBComposableLink.kt +++ b/Sample/routes/src/main/java/com/veepee/routes/feature_b/FeatureBComposableLink.kt @@ -16,7 +16,7 @@ package com.veepee.routes.feature_b import com.veepee.vpcore.route.link.compose.ComposableEvent -import com.veepee.vpcore.route.link.compose.ComposableLinkWithEvent +import com.veepee.vpcore.route.link.compose.ComposableLink import com.veepee.vpcore.route.link.compose.ComposableName import com.veepee.vpcore.route.link.compose.ComposableParameter @@ -26,7 +26,7 @@ enum class ComposableBNames : ComposableName { data class FeatureBComposableLink( override val parameter: FeatureBComposableParameter -) : ComposableLinkWithEvent { +) : ComposableLink { override val composableName: ComposableBNames = ComposableBNames.ComposableB constructor(message: String) : this(FeatureBComposableParameter(message)) diff --git a/changelog/next/CORE_improve-composable-handling.md b/changelog/next/CORE_improve-composable-handling.md new file mode 100644 index 0000000..0e658c5 --- /dev/null +++ b/changelog/next/CORE_improve-composable-handling.md @@ -0,0 +1,8 @@ +--- +title: ComposableFor methods now do a better job restricting implementations +url: https://jira.vptech.eu/browse/FCAN-1853 +author: Julio Cesar Bueno Cotta +--- +ComposableFor methods now do a better job restricting implementations that require a ComposableEvent to be used with the version that does not expose a lambda. + +A ComposableLink now needs to specify a `ComposableEvent` in it's declaration, if it does not emit any value, we should use `NoComposableEvent` as part of the declaration, this will make the signature match the `ComposableFor` implementation without the event lambda. diff --git a/library/src/main/java/com/veepee/vpcore/route/link/compose/ComposableLink.kt b/library/src/main/java/com/veepee/vpcore/route/link/compose/ComposableLink.kt index de5c2d6..82b1a8e 100644 --- a/library/src/main/java/com/veepee/vpcore/route/link/compose/ComposableLink.kt +++ b/library/src/main/java/com/veepee/vpcore/route/link/compose/ComposableLink.kt @@ -19,8 +19,7 @@ import com.veepee.vpcore.route.link.Event import com.veepee.vpcore.route.link.Link import com.veepee.vpcore.route.link.Parameter -interface ComposableLinkWithEvent : ComposableLink -interface ComposableLink : Link { +interface ComposableLink : Link { val composableName: T override val parameter: ComposableParameter? } @@ -29,4 +28,4 @@ interface ComposableParameter : Parameter interface ComposableEvent : Event -object NoEvent : ComposableEvent +object NoComposableEvent : ComposableEvent diff --git a/library/src/main/java/com/veepee/vpcore/route/link/compose/ComposableLinkRouter.kt b/library/src/main/java/com/veepee/vpcore/route/link/compose/ComposableLinkRouter.kt index 889a5ed..275fcb7 100644 --- a/library/src/main/java/com/veepee/vpcore/route/link/compose/ComposableLinkRouter.kt +++ b/library/src/main/java/com/veepee/vpcore/route/link/compose/ComposableLinkRouter.kt @@ -24,10 +24,10 @@ import com.veepee.vpcore.route.link.interceptor.ChainFactoryImpl interface ComposableLinkRouter { @Composable - fun ComposeFor(composableLink: ComposableLink, modifier: Modifier) + fun ComposeFor(link: ComposableLink, modifier: Modifier) @Composable - fun ComposeFor(composableLink: ComposableLink) + fun ComposeFor(link: ComposableLink) interface Builder { fun add(composableLinkInterceptor: ComposableLinkInterceptor): Builder @@ -70,7 +70,7 @@ class ComposeLinkRouterBuilder( @Suppress("UNCHECKED_CAST") internal class ComposableLinkRouterImpl( composableNameMappers: Set>, - private val chainFactory: ChainFactory, ComposableLink> + private val chainFactory: ChainFactory, ComposableLink> ) : ComposableLinkRouter { private val composableLinkMapper = @@ -81,19 +81,19 @@ internal class ComposableLinkRouterImpl( }.toMap() @Composable - override fun ComposeFor(composableLink: ComposableLink) { - ComposeFor(composableLink = composableLink, modifier = Modifier) + override fun ComposeFor(link: ComposableLink) { + ComposeFor(link = link, modifier = Modifier) } @Composable - override fun ComposeFor(composableLink: ComposableLink, modifier: Modifier) { + override fun ComposeFor(link: ComposableLink, modifier: Modifier) { val chain = chainFactory.create() - val mapper = composableLinkMapper[composableLink.composableName] - ?: throw NoComposableNameMapperException(composableLink) - val newComposableLink = chain.next(mapper, composableLink) - if (newComposableLink != composableLink) { + val mapper = composableLinkMapper[link.composableName] + ?: throw NoComposableNameMapperException(link) + val newComposableLink = chain.next(mapper, link) + if (newComposableLink != link) { return ComposeFor(newComposableLink, modifier) } - mapper.Map(composableLink, modifier) + mapper.Map(link, modifier) } } diff --git a/library/src/main/java/com/veepee/vpcore/route/link/compose/ComposableNameMapper.kt b/library/src/main/java/com/veepee/vpcore/route/link/compose/ComposableNameMapper.kt index 4a2f454..b474803 100644 --- a/library/src/main/java/com/veepee/vpcore/route/link/compose/ComposableNameMapper.kt +++ b/library/src/main/java/com/veepee/vpcore/route/link/compose/ComposableNameMapper.kt @@ -22,5 +22,5 @@ interface ComposableNameMapper { val supportedNames: Array @Composable - fun Map(composableLink: ComposableLink, modifier: Modifier) + fun Map(link: ComposableLink, modifier: Modifier) } diff --git a/library/src/main/java/com/veepee/vpcore/route/link/compose/LinkRouterContainer.kt b/library/src/main/java/com/veepee/vpcore/route/link/compose/LinkRouterContainer.kt index 98aa8d3..64ee737 100644 --- a/library/src/main/java/com/veepee/vpcore/route/link/compose/LinkRouterContainer.kt +++ b/library/src/main/java/com/veepee/vpcore/route/link/compose/LinkRouterContainer.kt @@ -56,17 +56,20 @@ fun ComposableLinkRouterContainer( } @Composable -fun ComposableFor(link: ComposableLink, modifier: Modifier = Modifier) { - LocalComposableLinkRouter.current.ComposeFor(composableLink = link, modifier) +fun ComposableFor(link: ComposableLink, modifier: Modifier = Modifier) { + ComposableFor(link = link, modifier) {} } @Composable inline fun ComposableFor( - link: ComposableLinkWithEvent, + link: ComposableLink, modifier: Modifier = Modifier, noinline onEvent: (Event) -> Unit ) { - LinkRouterEventHandlerContainer(onEvent = onEvent) { - ComposableFor(link = link, modifier = modifier) - } + LinkRouterEventHandlerContainer( + onEvent = onEvent, + content = { + LocalComposableLinkRouter.current.ComposeFor(link = link, modifier) + } + ) } diff --git a/library/src/main/java/com/veepee/vpcore/route/link/compose/NoComposableNameMapperException.kt b/library/src/main/java/com/veepee/vpcore/route/link/compose/NoComposableNameMapperException.kt index 0849d8a..0999232 100644 --- a/library/src/main/java/com/veepee/vpcore/route/link/compose/NoComposableNameMapperException.kt +++ b/library/src/main/java/com/veepee/vpcore/route/link/compose/NoComposableNameMapperException.kt @@ -15,5 +15,5 @@ */ package com.veepee.vpcore.route.link.compose -internal class NoComposableNameMapperException(composableLink: ComposableLink) : +internal class NoComposableNameMapperException(composableLink: ComposableLink) : IllegalArgumentException("$composableLink has no registered ComposableNameMapper registered for it.") diff --git a/library/src/main/java/com/veepee/vpcore/route/link/compose/chain/ComposableLinkInterceptor.kt b/library/src/main/java/com/veepee/vpcore/route/link/compose/chain/ComposableLinkInterceptor.kt index 5f00f72..0035cd1 100644 --- a/library/src/main/java/com/veepee/vpcore/route/link/compose/chain/ComposableLinkInterceptor.kt +++ b/library/src/main/java/com/veepee/vpcore/route/link/compose/chain/ComposableLinkInterceptor.kt @@ -15,10 +15,11 @@ */ package com.veepee.vpcore.route.link.compose.chain +import com.veepee.vpcore.route.link.compose.ComposableEvent import com.veepee.vpcore.route.link.compose.ComposableLink import com.veepee.vpcore.route.link.compose.ComposableName import com.veepee.vpcore.route.link.compose.ComposableNameMapper import com.veepee.vpcore.route.link.interceptor.LinkInterceptor interface ComposableLinkInterceptor : - LinkInterceptor, ComposableLink> + LinkInterceptor, ComposableLink> diff --git a/library/src/test/java/com/veepee/vpcore/route/composable/ComposableLinkRouterTest.kt b/library/src/test/java/com/veepee/vpcore/route/composable/ComposableLinkRouterTest.kt index ddcac1f..be86861 100644 --- a/library/src/test/java/com/veepee/vpcore/route/composable/ComposableLinkRouterTest.kt +++ b/library/src/test/java/com/veepee/vpcore/route/composable/ComposableLinkRouterTest.kt @@ -23,6 +23,7 @@ import com.veepee.vpcore.route.composable.feature.TestComposablesNameMapper import com.veepee.vpcore.route.composable.route.TestComposableALink import com.veepee.vpcore.route.composable.route.TestComposableBLink import com.veepee.vpcore.route.composable.route.TestComposableBParameter +import com.veepee.vpcore.route.link.compose.ComposableEvent import com.veepee.vpcore.route.link.compose.ComposableFor import com.veepee.vpcore.route.link.compose.ComposableLink import com.veepee.vpcore.route.link.compose.ComposableLinkRouterContainer @@ -113,10 +114,10 @@ class ComposableLinkRouterTest { mappers, ChainFactoryImpl(listOf(object : ComposableLinkInterceptor { override fun intercept( - chain: Chain, ComposableLink>, + chain: Chain, ComposableLink>, mapper: ComposableNameMapper, - link: ComposableLink - ): ComposableLink { + link: ComposableLink + ): ComposableLink { if (link is TestComposableBLink) { return TestComposableALink() } diff --git a/library/src/test/java/com/veepee/vpcore/route/composable/feature/TestComposablesNameMapper.kt b/library/src/test/java/com/veepee/vpcore/route/composable/feature/TestComposablesNameMapper.kt index b44ce34..0bd8b18 100644 --- a/library/src/test/java/com/veepee/vpcore/route/composable/feature/TestComposablesNameMapper.kt +++ b/library/src/test/java/com/veepee/vpcore/route/composable/feature/TestComposablesNameMapper.kt @@ -22,6 +22,7 @@ import com.veepee.vpcore.route.composable.route.TestComposableALink import com.veepee.vpcore.route.composable.route.TestComposableBLink import com.veepee.vpcore.route.composable.route.TestComposableBLinkEvent import com.veepee.vpcore.route.composable.route.TestComposableName +import com.veepee.vpcore.route.link.compose.ComposableEvent import com.veepee.vpcore.route.link.compose.ComposableLink import com.veepee.vpcore.route.link.compose.ComposableNameMapper import com.veepee.vpcore.route.link.compose.events.LocalLinkRouterEventHandler @@ -30,12 +31,12 @@ object TestComposablesNameMapper : ComposableNameMapper { override val supportedNames: Array = TestComposableName.values() @Composable - override fun Map(composableLink: ComposableLink, modifier: Modifier) { + override fun Map(link: ComposableLink, modifier: Modifier) { val handler = LocalLinkRouterEventHandler.current - when (composableLink) { + when (link) { is TestComposableALink -> TestComposableA(modifier = modifier) is TestComposableBLink -> TestComposableB( - composableLink.parameter.message, + link.parameter.message, modifier = modifier.clickable { handler.publish(TestComposableBLinkEvent("bar")) } diff --git a/library/src/test/java/com/veepee/vpcore/route/composable/route/TestComposableALink.kt b/library/src/test/java/com/veepee/vpcore/route/composable/route/TestComposableALink.kt index 15356cb..c8028ec 100644 --- a/library/src/test/java/com/veepee/vpcore/route/composable/route/TestComposableALink.kt +++ b/library/src/test/java/com/veepee/vpcore/route/composable/route/TestComposableALink.kt @@ -15,11 +15,11 @@ */ package com.veepee.vpcore.route.composable.route -import com.veepee.vpcore.route.link.compose.ComposableLinkWithEvent +import com.veepee.vpcore.route.link.compose.ComposableLink import com.veepee.vpcore.route.link.compose.ComposableParameter -import com.veepee.vpcore.route.link.compose.NoEvent +import com.veepee.vpcore.route.link.compose.NoComposableEvent -class TestComposableALink : ComposableLinkWithEvent { +class TestComposableALink : ComposableLink { override val composableName: TestComposableName = TestComposableName.TestComposableA override val parameter: ComposableParameter? = null } diff --git a/library/src/test/java/com/veepee/vpcore/route/composable/route/TestComposableBLink.kt b/library/src/test/java/com/veepee/vpcore/route/composable/route/TestComposableBLink.kt index 9bfdd89..c8ac4f7 100644 --- a/library/src/test/java/com/veepee/vpcore/route/composable/route/TestComposableBLink.kt +++ b/library/src/test/java/com/veepee/vpcore/route/composable/route/TestComposableBLink.kt @@ -16,12 +16,12 @@ package com.veepee.vpcore.route.composable.route import com.veepee.vpcore.route.link.compose.ComposableEvent -import com.veepee.vpcore.route.link.compose.ComposableLinkWithEvent +import com.veepee.vpcore.route.link.compose.ComposableLink import com.veepee.vpcore.route.link.compose.ComposableParameter class TestComposableBLink( override val parameter: TestComposableBParameter -) : ComposableLinkWithEvent { +) : ComposableLink { override val composableName: TestComposableName = TestComposableName.TestComposableB }