-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'CORE/feat/FCAN-1841_enable-compose-communication' into …
…'main' FCAN-1841 | Feat (CORE) : Enable composables communication See merge request veepee/offerdiscovery/products/front-mobile/android/link-router!24
- Loading branch information
Showing
12 changed files
with
285 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
changelog/next/CORE_FCAN-1841_enable-compose-communication.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
--- | ||
title: Enable composables communication | ||
url: https://jira.vptech.eu/browse/FCAN-1841 | ||
author: Julio Cesar Bueno Cotta | ||
--- | ||
|
||
|
||
Android components have ways of implicit communication, for instance | ||
- Activities -> Activities -> OnActivityResult/Contracts | ||
- Fragments -> Fragments -> Fragment Result API | ||
|
||
The started activity don't need to know which Activity called it, the Fragments sending results are not required to know who is listening for it's results. | ||
|
||
|
||
But how about Composables -> ??? | ||
|
||
|
||
Compose does not provide an out of the box solution to send implicit events to the callers. | ||
|
||
|
||
### What did I do? | ||
|
||
This MR tries to enable that communication in a **somewhat** type safe way. | ||
|
||
Our approach avoids "lambda forwarding" by using a `LinkRouterEventHandler` that is instantiated when accessing a `LinkRouterEventHandlerContainer`. | ||
|
||
Example : | ||
``` kotlin | ||
sealed interface MyData : ComposableEvent { | ||
data class MyData1(val foo: String) : MyData | ||
data class MyData2(val foo: String) : MyData | ||
} | ||
|
||
@Composable | ||
fun MyScreen() { | ||
LinkRouterEventHandlerContainer<MyData>(onEvent = { | ||
println(it) | ||
}, | ||
content = { | ||
MyDataComposable() | ||
} | ||
) | ||
} | ||
|
||
@Composable | ||
private fun MyDataComposable() { | ||
MySecondLayerDataComposable() | ||
} | ||
|
||
@Composable | ||
private fun MySecondLayerDataComposable() { | ||
val handler = LocalLinkRouterEventHandler.current | ||
BasicText( | ||
"hello world", | ||
modifier = Modifier.clickable { handler.publish(MyData.MyData1("bar")) }) | ||
} | ||
``` | ||
|
||
Event though `MySecondLayerDataComposable` is a leaf composable and does not contain a "onClick : () -> Unit" lambda, the onClick event arrives to `onEvent` lambda in `MyScreen` composable. | ||
|
||
We say that this API is **somewhat** type safe because there is no guarantees that a `LinkRouterEventHandlerContainer` will be listening for the published type. | ||
|
||
For instance: | ||
``` kotlin | ||
@Composable | ||
private fun MySecondLayerDataComposable() { | ||
val handler = LocalLinkRouterEventHandler.current | ||
BasicText( | ||
"hello world", | ||
modifier = Modifier.clickable { handler.publish("foobar") }) | ||
} | ||
``` | ||
Would cause an error at **runtime** as there is no registered `LinkRouterEventHandlerContainer` that can handle Strings. | ||
|
||
|
||
The new composable `ComposableFor` is meant to be called with `ComposableLinkWithEvent` implementations and offers a lambda with the expected event type that can be emitted | ||
|
||
``` kotlin | ||
ComposableFor( | ||
link = FeatureBComposableLink("1 - AActivity sent this text to a Composable in B Module"), | ||
modifier = modifier | ||
.fillMaxWidth() | ||
.background(Color.Yellow) | ||
) { event -> | ||
text = event.messageSize.toString() | ||
} | ||
``` | ||
|
||
The `FeatureBComposableLink` class is defined as | ||
``` | ||
class TestComposableBLink( | ||
override val parameter: TestComposableBParameter | ||
) : ComposableLinkWithEvent<TestComposableName, TestComposableBLinkEvent> { | ||
override val composableName: TestComposableName = TestComposableName.TestComposableB | ||
} | ||
data class TestComposableBParameter(val message: String) : ComposableParameter | ||
data class TestComposableBLinkEvent(val foo: String) : ComposableEvent | ||
``` | ||
|
||
Notice that it implements `ComposableLinkWithEvent`, not `ComposableLink` as we need the `ComposableEvent` type to ensure the correct match of types when routing this link. | ||
|
||
|
||
### Notes: | ||
- Is this fine? With this we open the gate to a new class of bugs in the project, runtime errors due to missing event handlers. | ||
- Should `ComposableEvent` be renamed to `ComposableResult` ? I don't quite like the `Event` suffix. | ||
|
||
### QA: | ||
1) what will happen if I call `handler.publish()` with a type that is not directly handled by the nearest EventHandler ? | ||
The event will be forwarded to the next EventHandler until it reaches an EventHandler that can handle it or the topmost EventHandler where we are raising en exception and crashing. | ||
|
||
2) Can I use this to avoid the lambdas inside my feature composables? | ||
Yes, but be aware that Google does not recommend this! We are using this in LinkRouter because we couldn't find a better way of sharing events in a generic way without rewriting the library. | ||
As I stated above, there is no compile time check if an EventHandler is deployed in the compose hierarchy that can handle the type you want to publish. | ||
|
||
3) Can I use primitive types as Events? | ||
Yes, but you **really** should think in using a sealed class or sealed interface for your public API as the caller can use a `when` to evaluate all possible states. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
.../main/java/com/veepee/vpcore/route/link/compose/events/LinkRouterEventHandlerContainer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.veepee.vpcore.route.link.compose.events | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.ProvidableCompositionLocal | ||
import androidx.compose.runtime.compositionLocalOf | ||
import com.veepee.vpcore.route.link.compose.ComposableEvent | ||
|
||
@Composable | ||
inline fun <reified Event : ComposableEvent> LinkRouterEventHandlerContainer( | ||
current: LinkRouterEventHandler<out ComposableEvent> = LocalLinkRouterEventHandler.current, | ||
noinline onEvent: (Event) -> Unit, | ||
crossinline content: @Composable () -> Unit | ||
) { | ||
CompositionLocalProvider( | ||
LocalLinkRouterEventHandler provides LinkRouterEventHandler( | ||
parent = current, | ||
isAssignableFrom = { event -> | ||
Event::class.java.isAssignableFrom(event::class.java) | ||
}, | ||
onEvent = onEvent | ||
) | ||
) { | ||
content() | ||
} | ||
} | ||
|
||
class LinkRouterEventHandler<Event : ComposableEvent> constructor( | ||
private val parent: LinkRouterEventHandler<out ComposableEvent>? = null, | ||
private val isAssignableFrom: (event: Any) -> Boolean, | ||
private val onEvent: (Event) -> Unit | ||
) { | ||
@Suppress("UNCHECKED_CAST") | ||
fun publish(event: ComposableEvent) { | ||
if (isAssignableFrom(event)) { | ||
onEvent(event as Event) | ||
} else if (parent == null) { | ||
throw NoParentEventHandlerException() | ||
} else { | ||
parent.publish(event) | ||
} | ||
} | ||
} | ||
|
||
class NoParentEventHandlerException : RuntimeException("No parent LocalEventHandler found!") | ||
class NoEventHandlerException(event: ComposableEvent) : | ||
RuntimeException("Your event \"$event\" reached the topmost EventHandler one in the compose hierarchy without a handler!") | ||
|
||
val LocalLinkRouterEventHandler: ProvidableCompositionLocal<LinkRouterEventHandler<out ComposableEvent>> = | ||
compositionLocalOf { | ||
LinkRouterEventHandler( | ||
isAssignableFrom = { true }, | ||
onEvent = { event -> | ||
throw NoEventHandlerException(event) | ||
} | ||
) | ||
} |
Oops, something went wrong.