Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting viewModel inside BoxWithConstraints #502

Open
IlyaChizhanov opened this issue Oct 22, 2024 · 0 comments
Open

Getting viewModel inside BoxWithConstraints #502

IlyaChizhanov opened this issue Oct 22, 2024 · 0 comments

Comments

@IlyaChizhanov
Copy link

IlyaChizhanov commented Oct 22, 2024

On Voyager screen with ViewModel, if we get viewModel inside BoxWithConstraints, then viewModel will be get twice.

Here's a demo project with the issue:

val koinModule = module {
    viewModelOf(::ScreenAViewModel)
    viewModelOf(::SubScreenViewModel)
}

class ScreenAViewModel(val id: Int) : ViewModel() {
    init {
        Timber.d("init $id")
    }

    override fun onCleared() {
        Timber.d("onCleared $id")
        super.onCleared()
    }
}

class SubScreenViewModel(val id: Int) : ViewModel() {
    init {
        Timber.d("init $id")
    }

    override fun onCleared() {
        Timber.d("onCleared $id")
        super.onCleared()
    }
}

data class ScreenA(val id: Int) : Screen {

    override val key: ScreenKey = uniqueScreenKey

    @Composable
    override fun Content() {
        val navigator = LocalNavigator.currentOrThrow
        val viewModel: ScreenAViewModel = koinViewModel { parametersOf(id) }

        Column(
            Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Box(
                Modifier
                    .size(200.dp)
                    .background(Color.Red)
                    .clickable {
                        Timber.d("Red clicked")
                        navigator.push(ScreenA(id + 1))
                    }
            ) {
                Text("id: ${viewModel.id}")
            }
            BoxWithConstraints {
                SubScreen(id)
            }
        }
    }
}

@Composable
fun SubScreen(id: Int) {
    val navigator = LocalNavigator.currentOrThrow
    val viewModel: SubScreenViewModel = koinViewModel(key = id.toString()) { parametersOf(id) }

    Box(
        Modifier
            .size(200.dp)
            .background(Color.Blue)
            .clickable {
                Timber.d("Blue clicked")
                navigator.pop()
            }
    ) {
        Text("id: ${viewModel.id}")
    }
}

This is what the log outputs:

log

This is completely unexpected behavior.
I encountered this in my project inside a complex UI tree and it was not easy to understand what was happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant