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

Use ActivityViewModelContext for activity scope #676

Merged
merged 4 commits into from
Jul 20, 2023
Merged

Use ActivityViewModelContext for activity scope #676

merged 4 commits into from
Jul 20, 2023

Conversation

apramana
Copy link
Contributor

@apramana apramana commented Mar 21, 2023

Fixes: #674

parentFragment should not get set if the scope is an activity.

@vulpeszerda
Copy link
Contributor

@apramana
I think just removing findFragmentFromView discards some functionality.

How about this?

val parentFragment = scope as? Fragment ?: if (scope !is Activity) findFragmentFromView(view) else null

if (parentFragment != null) {
    val args = argsFactory?.invoke() ?: parentFragment.arguments?.get(Mavericks.KEY_ARG)
    FragmentViewModelContext(activity, args, parentFragment)
} else {
    val args = argsFactory?.invoke() ?: activity.intent.extras?.get(Mavericks.KEY_ARG)
    ActivityViewModelContext(activity, args, viewModelStoreOwner, savedStateRegistry)
}

@apramana
Copy link
Contributor Author

@apramana I think just removing findFragmentFromView discards some functionality.

How about this?

val parentFragment = scope as? Fragment ?: if (scope !is Activity) findFragmentFromView(view) else null

if (parentFragment != null) {
    val args = argsFactory?.invoke() ?: parentFragment.arguments?.get(Mavericks.KEY_ARG)
    FragmentViewModelContext(activity, args, parentFragment)
} else {
    val args = argsFactory?.invoke() ?: activity.intent.extras?.get(Mavericks.KEY_ARG)
    ActivityViewModelContext(activity, args, viewModelStoreOwner, savedStateRegistry)
}

Yeah I suppose scope could be a non-fragment/activity lifecycle owner... updated to use a when expression.

Copy link
Contributor

@elihart elihart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix @apramana - can you add a unit test?

@elihart
Copy link
Contributor

elihart commented Jul 20, 2023

Thanks!

@elihart elihart merged commit 210b92a into airbnb:main Jul 20, 2023
@rossbacher
Copy link
Collaborator

@apramana This is now part of release v3.0.6 that I just made.

@apramana
Copy link
Contributor Author

@apramana This is now part of release v3.0.6 that I just made.

Thanks @rossbacher !

@apramana apramana deleted the patch-1 branch July 24, 2023 16:48
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

Successfully merging this pull request may close these issues.

Calling mavericksActivityViewModel in fragment, not working properly
4 participants