Skip to content

Commit

Permalink
Fix stateflow not triggered bug
Browse files Browse the repository at this point in the history
  • Loading branch information
omkar-tenkale committed Jul 20, 2024
1 parent bd7cfc5 commit 0023619
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 141 deletions.
5 changes: 4 additions & 1 deletion nodal/src/commonMain/kotlin/dev.omkartenkale.nodal/Node.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ public class UI {
}
}

private fun List<UI.Layer>.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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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){
Expand Down Expand Up @@ -66,9 +65,4 @@ class LoggedInNode: Node() {
// }.launchIn(coroutineScope)

}

override fun onRemoved() {
super.onRemoved()
layer.destroy()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -46,9 +45,4 @@ class MyAccountNode: Node() {
}
}
}

override fun onRemoved() {
super.onRemoved()
layer.destroy()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -74,8 +72,4 @@ class RideNode: Node() {
removeChild(it)
}
}

override fun onRemoved() {
layer.destroy()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -46,8 +44,4 @@ class PrePickupNode() : Node(/*dismissible = false*/) {
}
}
}

override fun onRemoved() {
layer.destroy()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -37,8 +35,4 @@ class DriverSearchNode() : Node() {
}
}
}

override fun onRemoved() {
layer.destroy()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -55,11 +55,6 @@ class DriverRatingNode : Node() {
}, color = Color.White)
}
}

}
}

override fun onRemoved() {
layer.destroy()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,86 +62,85 @@ 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<SelectedRoute?>(null) }
LaunchedEffect(Unit) {
addRouteSelectionNode {
selectedRoute = it
}
}
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<RideSelectionNode>().Content()
}
rideSelectionNodeAdded = true
}
if(rideSelectionNodeAdded){
child<RideSelectionNode>().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<SelectedPaymentModeNode>().Content()
}
selectedPaymentModeNodeAdded = true
}
if(selectedPaymentModeNodeAdded) {
child<SelectedPaymentModeNode>().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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ class PaymentModeSelectionCallback(block: (String)-> Unit): Callback<String> by
class PaymentModeSelectionNode : Node() {

private val onPaymentModeSelected by dependencies<PaymentModeSelectionCallback>()
private lateinit var layer: UI.Layer

override fun onAdded() {
layer = ui.draw(TransitionSpec.None) {
draw(TransitionSpec.None) {
FlexibleBottomSheet(
onDismissRequest = {
removeSelf()
Expand All @@ -50,8 +49,4 @@ class PaymentModeSelectionNode : Node() {
}
}
}

override fun onRemoved() {
layer.destroy()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ class AddressSelectionNode : Node() {

private val onAddressSelected by dependencies<AddressSelectionCallback>()

private lateinit var layer: UI.Layer

override fun onAdded() {

layer = ui.draw {
draw {
FlexibleBottomSheet(
onDismissRequest = {
removeSelf()
Expand All @@ -49,9 +46,4 @@ class AddressSelectionNode : Node() {
}
}
}

override fun onRemoved() {
super.onRemoved()
layer.destroy()
}
}
Loading

0 comments on commit 0023619

Please sign in to comment.