From 0023619544ba09dadf61c1606e2d70b0423b157b Mon Sep 17 00:00:00 2001 From: OmkarTenkale Date: Sat, 20 Jul 2024 12:30:00 +0530 Subject: [PATCH] Fix stateflow not triggered bug --- .../kotlin/dev.omkartenkale.nodal/Node.kt | 5 +- .../dev.omkartenkale.nodal/compose/UI.kt | 6 +- .../nodes/root/loggedin/LoggedInNode.kt | 10 +- .../root/loggedin/myaccoubt/MyAccountNode.kt | 10 +- .../nodes/root/loggedin/ride/RideNode.kt | 8 +- .../active/assigned/pickup/PrePickupNode.kt | 8 +- .../active/driversearch/DriverSearchNode.kt | 8 +- .../ride/completed/DriverRatingNode.kt | 9 +- .../loggedin/ride/request/RequestRideNode.kt | 115 +++++++++--------- .../paymentmode/PaymentModeSelectionNode.kt | 7 +- .../select/route/AddressSelectionNode.kt | 10 +- .../select/route/RouteSelectionNode.kt | 8 +- .../nodes/root/loggedout/ConfirmOtpNode.kt | 7 +- .../nodes/root/loggedout/LoggedOutNode.kt | 7 +- 14 files changed, 77 insertions(+), 141 deletions(-) diff --git a/nodal/src/commonMain/kotlin/dev.omkartenkale.nodal/Node.kt b/nodal/src/commonMain/kotlin/dev.omkartenkale.nodal/Node.kt index 00563ed..d0ef40e 100644 --- a/nodal/src/commonMain/kotlin/dev.omkartenkale.nodal/Node.kt +++ b/nodal/src/commonMain/kotlin/dev.omkartenkale.nodal/Node.kt @@ -23,6 +23,8 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import kotlinx.coroutines.yield import kotlin.jvm.JvmStatic import kotlin.reflect.KClass @@ -121,9 +123,10 @@ public open class Node { public open fun onRemoved() {} internal fun dispatchRemoved() { - coroutineScope.launch { + coroutineScope.launch(Dispatchers.Unconfined) { onRemoved() _isAddedEvents.emit(false) + yield() children.forEach { it.dispatchRemoved() } coroutineScope.cancel() dependencies.close() diff --git a/nodal/src/commonMain/kotlin/dev.omkartenkale.nodal/compose/UI.kt b/nodal/src/commonMain/kotlin/dev.omkartenkale.nodal/compose/UI.kt index 3c492b6..0db9e0a 100644 --- a/nodal/src/commonMain/kotlin/dev.omkartenkale.nodal/compose/UI.kt +++ b/nodal/src/commonMain/kotlin/dev.omkartenkale.nodal/compose/UI.kt @@ -48,10 +48,8 @@ public class UI { } } -private fun List.secondToTop(): UI.Layer? = if(size < 2 ) null else get(lastIndex-1) - -public fun Node.draw(content: @Composable (Modifier) -> Unit) { - val layer = ui.draw(content = content) +public fun Node.draw(transitionSpec: TransitionSpec = TransitionSpec.None, content: @Composable (Modifier) -> Unit) { + val layer = ui.draw(transitionSpec = transitionSpec, content = content) doOnRemoved { layer.destroy() } diff --git a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/LoggedInNode.kt b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/LoggedInNode.kt index 2d89532..c64e171 100644 --- a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/LoggedInNode.kt +++ b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/LoggedInNode.kt @@ -9,6 +9,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.layout.ContentScale import dev.omkartenkale.nodal.Node import dev.omkartenkale.nodal.compose.UI +import dev.omkartenkale.nodal.compose.draw import dev.omkartenkale.nodal.sample.ride.nodes.root.loggedin.myaccoubt.MyAccountNode import dev.omkartenkale.nodal.util.addChild import dev.omkartenkale.nodal.sample.ride.nodes.root.loggedin.ride.RideNode @@ -26,12 +27,10 @@ class LoggedInNode: Node() { class Args(val userName: String) private val args: Args by dependencies() - private lateinit var layer: UI.Layer - @OptIn(ExperimentalResourceApi::class) override fun onAdded() { - layer = ui.draw { + draw { println("LoggedInNode: Recompose") LaunchedEffect(Unit){ @@ -66,9 +65,4 @@ class LoggedInNode: Node() { // }.launchIn(coroutineScope) } - - override fun onRemoved() { - super.onRemoved() - layer.destroy() - } } diff --git a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/myaccoubt/MyAccountNode.kt b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/myaccoubt/MyAccountNode.kt index b7254dd..2cb05e6 100644 --- a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/myaccoubt/MyAccountNode.kt +++ b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/myaccoubt/MyAccountNode.kt @@ -9,6 +9,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.layout.ContentScale import dev.omkartenkale.nodal.Node import dev.omkartenkale.nodal.compose.UI +import dev.omkartenkale.nodal.compose.draw import dev.omkartenkale.nodal.compose.transitions.BackstackTransition import dev.omkartenkale.nodal.compose.transitions.TransitionSpec import dev.omkartenkale.nodal.misc.BackPressCallback @@ -24,14 +25,12 @@ import org.jetbrains.compose.resources.painterResource class MyAccountNode: Node() { - private lateinit var layer: UI.Layer - @OptIn(ExperimentalResourceApi::class) override fun onAdded() { backPressHandler.addBackPressCallback(BackPressCallback(isEnabled = true) { removeSelf() }) - layer = ui.draw(TransitionSpec.Slide) { + draw(TransitionSpec.Slide) { Column { Image( @@ -46,9 +45,4 @@ class MyAccountNode: Node() { } } } - - override fun onRemoved() { - super.onRemoved() - layer.destroy() - } } diff --git a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/RideNode.kt b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/RideNode.kt index ebda6d5..958caf0 100644 --- a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/RideNode.kt +++ b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/RideNode.kt @@ -33,15 +33,13 @@ import kotlin.time.Duration.Companion.seconds class RideNode: Node() { - private lateinit var layer: UI.Layer - override fun onAdded() { // childrenUpdatedEvents.onEach { // if(children.isEmpty()){ // removeSelf() // } // }.launchIn(coroutineScope) - layer = ui.draw(TransitionSpec.Fade) { + draw(TransitionSpec.Fade) { Image( modifier = it.clickable { removeSelf() @@ -74,8 +72,4 @@ class RideNode: Node() { removeChild(it) } } - - override fun onRemoved() { - layer.destroy() - } } \ No newline at end of file diff --git a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/active/assigned/pickup/PrePickupNode.kt b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/active/assigned/pickup/PrePickupNode.kt index 79b5432..8038167 100644 --- a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/active/assigned/pickup/PrePickupNode.kt +++ b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/active/assigned/pickup/PrePickupNode.kt @@ -18,11 +18,9 @@ import org.jetbrains.compose.resources.ExperimentalResourceApi import org.jetbrains.compose.resources.painterResource class PrePickupNode() : Node(/*dismissible = false*/) { - private lateinit var layer: UI.Layer - @OptIn(ExperimentalResourceApi::class) override fun onAdded() { - layer = ui.draw { + draw { FlexibleBottomSheet( onDismissRequest = { removeSelf() @@ -46,8 +44,4 @@ class PrePickupNode() : Node(/*dismissible = false*/) { } } } - - override fun onRemoved() { - layer.destroy() - } } \ No newline at end of file diff --git a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/active/driversearch/DriverSearchNode.kt b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/active/driversearch/DriverSearchNode.kt index 3975055..c32c90b 100644 --- a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/active/driversearch/DriverSearchNode.kt +++ b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/active/driversearch/DriverSearchNode.kt @@ -20,10 +20,8 @@ import org.jetbrains.compose.resources.ExperimentalResourceApi import org.jetbrains.compose.resources.painterResource class DriverSearchNode() : Node() { - private lateinit var layer: UI.Layer - override fun onAdded() { - layer = ui.draw { + draw { FlexibleBottomSheet( onDismissRequest = {}, sheetState = rememberFlexibleBottomSheetState() @@ -37,8 +35,4 @@ class DriverSearchNode() : Node() { } } } - - override fun onRemoved() { - layer.destroy() - } } \ No newline at end of file diff --git a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/completed/DriverRatingNode.kt b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/completed/DriverRatingNode.kt index d09a192..8499375 100644 --- a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/completed/DriverRatingNode.kt +++ b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/completed/DriverRatingNode.kt @@ -20,6 +20,7 @@ import androidx.compose.ui.unit.dp import dev.omkartenkale.nodal.Node import dev.omkartenkale.nodal.compose.UI import dev.omkartenkale.nodal.compose.draw +import dev.omkartenkale.nodal.compose.transitions.TransitionSpec import dev.omkartenkale.nodal.misc.VoidCallback import dev.omkartenkale.nodal.util.addChild import nodal.ride.generated.resources.Res @@ -34,10 +35,9 @@ fun Node.addDriverRatingNode(onDriverRated: () -> Unit) = class DriverRatingNode : Node() { val driverRatedCallback: DriverRatedCallback by dependencies() - private lateinit var layer: UI.Layer override fun onAdded() { - layer = ui.draw { + draw(TransitionSpec.Slide) { var submitVisible by remember { mutableStateOf(false) } Column { Image( @@ -55,11 +55,6 @@ class DriverRatingNode : Node() { }, color = Color.White) } } - } } - - override fun onRemoved() { - layer.destroy() - } } \ No newline at end of file diff --git a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/RequestRideNode.kt b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/RequestRideNode.kt index d89f24a..0e887d3 100644 --- a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/RequestRideNode.kt +++ b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/RequestRideNode.kt @@ -62,11 +62,10 @@ class RequestRideNode : Node() { var selectedRideType: String? = null var selectedPaymentMode: String? = null - private lateinit var layer: UI.Layer @OptIn(ExperimentalResourceApi::class) override fun onAdded() { - layer = ui.draw(TransitionSpec.Fade) { + draw(TransitionSpec.Fade) { var selectedRoute by remember { mutableStateOf(null) } LaunchedEffect(Unit) { addRouteSelectionNode { @@ -74,74 +73,74 @@ class RequestRideNode : Node() { } } selectedRoute?.let { - FlexibleBottomSheet( - onDismissRequest = { - removeSelf() - }, - sheetState = rememberFlexibleBottomSheetState( - confirmValueChange = { - it != FlexibleSheetValue.Hidden + Column { + FlexibleBottomSheet( + onDismissRequest = { + removeSelf() }, - isModal = true - ) - ) { - Column(modifier = Modifier - .verticalScroll(rememberScrollState()) - .fillMaxSize() - .weight(1f, fill = true) - .background(Color.Red)) { + sheetState = rememberFlexibleBottomSheetState( + confirmValueChange = { + it != FlexibleSheetValue.Hidden + }, + isModal = true + ) + ) { + Column( + modifier = Modifier + .verticalScroll(rememberScrollState()) + .fillMaxSize() + .weight(1f, fill = true) + .background(Color.Red) + ) { - Box(modifier = Modifier) { - var rideSelectionNodeAdded by remember { mutableStateOf(false) } - LaunchedEffect(Unit) { - addRideSelectionNode { - selectedRideType = it + Box(modifier = Modifier) { + var rideSelectionNodeAdded by remember { mutableStateOf(false) } + LaunchedEffect(Unit) { + addRideSelectionNode { + selectedRideType = it + } + rideSelectionNodeAdded = true + } + if (rideSelectionNodeAdded) { + child().Content() } - rideSelectionNodeAdded = true - } - if(rideSelectionNodeAdded){ - child().Content() } - } - Box(modifier = Modifier.fillMaxWidth()) { - var selectedPaymentModeNodeAdded by remember { mutableStateOf(false) } - LaunchedEffect(Unit) { - addSelectedPaymentModeNode { - selectedPaymentMode = it + Box(modifier = Modifier.fillMaxWidth()) { + var selectedPaymentModeNodeAdded by remember { mutableStateOf(false) } + LaunchedEffect(Unit) { + addSelectedPaymentModeNode { + selectedPaymentMode = it + } + selectedPaymentModeNodeAdded = true + } + if (selectedPaymentModeNodeAdded) { + child().Content() } - selectedPaymentModeNodeAdded = true - } - if(selectedPaymentModeNodeAdded) { - child().Content() } - } - Image( - modifier = Modifier.fillMaxWidth().clickable { - selectedRideType?.let { selectedRideType -> - selectedPaymentMode?.let { selectedPaymentMode -> - requestRideCallback( - RideRequest( - selectedRideType, - it, - selectedPaymentMode + Image( + modifier = Modifier.fillMaxWidth().clickable { + selectedRideType?.let { selectedRideType -> + selectedPaymentMode?.let { selectedPaymentMode -> + requestRideCallback( + RideRequest( + selectedRideType, + it, + selectedPaymentMode + ) ) - ) - removeSelf() + removeSelf() + } } - } - }, - painter = painterResource(Res.drawable.submit_button), - contentScale = ContentScale.FillWidth, - contentDescription = null - ) + }, + painter = painterResource(Res.drawable.submit_button), + contentScale = ContentScale.FillWidth, + contentDescription = null + ) + } } } } } } - - override fun onRemoved() { - layer.destroy() - } } \ No newline at end of file diff --git a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/select/paymentmode/PaymentModeSelectionNode.kt b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/select/paymentmode/PaymentModeSelectionNode.kt index 1d73f5e..d266e49 100644 --- a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/select/paymentmode/PaymentModeSelectionNode.kt +++ b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/select/paymentmode/PaymentModeSelectionNode.kt @@ -28,10 +28,9 @@ class PaymentModeSelectionCallback(block: (String)-> Unit): Callback by class PaymentModeSelectionNode : Node() { private val onPaymentModeSelected by dependencies() - private lateinit var layer: UI.Layer override fun onAdded() { - layer = ui.draw(TransitionSpec.None) { + draw(TransitionSpec.None) { FlexibleBottomSheet( onDismissRequest = { removeSelf() @@ -50,8 +49,4 @@ class PaymentModeSelectionNode : Node() { } } } - - override fun onRemoved() { - layer.destroy() - } } \ No newline at end of file diff --git a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/select/route/AddressSelectionNode.kt b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/select/route/AddressSelectionNode.kt index 7388102..12156b0 100644 --- a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/select/route/AddressSelectionNode.kt +++ b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/select/route/AddressSelectionNode.kt @@ -26,11 +26,8 @@ class AddressSelectionNode : Node() { private val onAddressSelected by dependencies() - private lateinit var layer: UI.Layer - override fun onAdded() { - - layer = ui.draw { + draw { FlexibleBottomSheet( onDismissRequest = { removeSelf() @@ -49,9 +46,4 @@ class AddressSelectionNode : Node() { } } } - - override fun onRemoved() { - super.onRemoved() - layer.destroy() - } } \ No newline at end of file diff --git a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/select/route/RouteSelectionNode.kt b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/select/route/RouteSelectionNode.kt index 4037445..92c897d 100644 --- a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/select/route/RouteSelectionNode.kt +++ b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedin/ride/request/select/route/RouteSelectionNode.kt @@ -33,11 +33,9 @@ class RouteSelectionNode : Node() { private val onRouteSelected by dependencies() - private lateinit var layer: UI.Layer - @OptIn(ExperimentalResourceApi::class) override fun onAdded() { - layer = ui.draw(TransitionSpec.None) { + draw(TransitionSpec.None) { FlexibleBottomSheet( onDismissRequest = { removeSelf() @@ -61,8 +59,4 @@ class RouteSelectionNode : Node() { } } } - - override fun onRemoved() { - layer.destroy() - } } \ No newline at end of file diff --git a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedout/ConfirmOtpNode.kt b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedout/ConfirmOtpNode.kt index dc66a1a..0578d55 100644 --- a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedout/ConfirmOtpNode.kt +++ b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedout/ConfirmOtpNode.kt @@ -33,11 +33,10 @@ import org.jetbrains.compose.resources.painterResource @ExperimentalResourceApi class ConfirmOtpNode : Node() { - private lateinit var layer: UI.Layer private val onOtpConfirmed: OTPConfirmedCallback by dependencies() override fun onAdded() { - layer = ui.draw(TransitionSpec.None) { + draw(TransitionSpec.None) { // Box(Modifier.background(Color.Red).fillMaxSize()) { val scope = rememberCoroutineScope() val state = nonExpandingSheetState() @@ -70,8 +69,4 @@ class ConfirmOtpNode : Node() { // } } } - - override fun onRemoved() { - layer.destroy() - } } \ No newline at end of file diff --git a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedout/LoggedOutNode.kt b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedout/LoggedOutNode.kt index 4d6da6f..9693cce 100644 --- a/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedout/LoggedOutNode.kt +++ b/samples/ride/src/commonMain/kotlin/dev.omkartenkale.nodal.sample.ride/nodes/root/loggedout/LoggedOutNode.kt @@ -33,10 +33,9 @@ fun Node.addLoggedOutNode(onLoggedIn: (String) -> Unit) = class LoggedOutNode : Node() { val onLoggedIn: LoggedInCallback by dependencies() - private lateinit var layer: UI.Layer override fun onAdded() { - layer = ui.draw { + draw { Box { Image( modifier = Modifier.fillMaxSize(), @@ -58,8 +57,4 @@ class LoggedOutNode : Node() { } } } - - override fun onRemoved() { - layer.destroy() - } } \ No newline at end of file