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

Shared ScreenModel between screens #168

Closed
renaudmathieu opened this issue Jun 22, 2023 · 9 comments
Closed

Shared ScreenModel between screens #168

renaudmathieu opened this issue Jun 22, 2023 · 9 comments

Comments

@renaudmathieu
Copy link

Voyager by default expect that it screens can be stored inside a Bundle. This means both Java Serializable and Parcelable are supported. By default all Voyager Screen is Java Serializable this means that Screen can be restored if all parameters are Java Serializable.

Is there a way to share the same ScreenModel between screens?

@programadorthi
Copy link
Collaborator

programadorthi commented Jun 23, 2023

Well, a ScreenModel is remembered by Compose SlotTable and associated to a Screen. The idea is the same as Android ViewModel. Can you share a ViewModel with two Activities?
So, a hack try to you have a ScreenModel available to two Screen is:

  1. Create a Singleton Screen.
  2. In both Screen do: val model by SingletonScreen.rememberScreenModel<YourScreenModel>()

I have not tested that but as you can see in the code a remembered ScreenModel is associated to a Screen. So, a Singleton Screen should works

@osrl
Copy link
Contributor

osrl commented Aug 31, 2023

but you can share a viewmodel with two fragments via navhost. it's a pretty common usage.

@renaudmathieu
Copy link
Author

I've found a temporary solution using Koin.

  1. Add the following dependencies:
implementation("io.insert-koin:koin-core:3.4.3")
implementation("io.insert-koin:koin-compose:1.0.4")
  1. Start Koin and define a Single ScreenModel
KoinApplication(application = {
        modules(appModule)
}) {
  1. Then you can inject it
val appScreenModel = koinInject<AppScreenModel>()

@osrl
Copy link
Contributor

osrl commented Sep 8, 2023

but this is scoped to entire app. it's never disposed right?

@Ric-AB
Copy link

Ric-AB commented Sep 20, 2023

Has anyone found a solution to this?

@osrl
Copy link
Contributor

osrl commented Sep 25, 2023

checkout #217

@DevSrSouza
Copy link
Collaborator

DevSrSouza commented Oct 17, 2023

I think the main question here is: Where do we will scope it.

  1. If you are running Single Activity, and using only one Navigator for the hole app, there is no place to Scope and free de ScreenModel/ViewModel, we could have a be scoped at the Navigator level but it will always stay alive.
  2. If you are running Multiple Activities, one per flow, you can use for example Kodein Scoped Injection or Koin.
  3. If you are running Single Activity but Nested Navigation for scoping features, in this case, the feat: added support to have navigator scoped ViewModel #217 should be what you are looking for.

@DevSrSouza
Copy link
Collaborator

I had add new API for Navigator scoped ScreenModel, checkout, it is available starting from 1.0.0-rc08
#233

Docs: https://voyager.adriel.cafe/screenmodel#navigator-scoped-screenmodel

@osrl
Copy link
Contributor

osrl commented Nov 6, 2023

#217 will still be needed for ViewModels. Do you plan merging it?

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

5 participants