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

fix(Android): reset nearby transit state on pan/recenter #635

Merged
merged 5 commits into from
Jan 10, 2025

Conversation

boringcactus
Copy link
Member

Summary

Ticket: 🤖 | Nearby | Reset state when panning or recentering

I thought this would be easy, and if I knew one more thing about ViewModels it would've been easy, but I did not, so this was really difficult.

iOS

  • [ ] If you added any user-facing strings on iOS, are they included in Localizable.xcstrings?
    • [ ] Add temporary machine translations, marked "Needs Review"

android

  • [ ] All user-facing strings added to strings resource
  • [ ] Expensive calculations are run in withContext(Dispatchers.Default) where possible

Testing

Manually verified that the ViewModel gets reset and the stale data does not flash in for a frame or two.

@boringcactus boringcactus requested a review from a team as a code owner January 9, 2025 17:26
Comment on lines 220 to +222
composable<SheetRoutes.NearbyTransit> {
// for ViewModel reasons, must be within the `composable` to be the same instance
val nearbyViewModel: NearbyTransitViewModel = koinViewModel()
Copy link
Member Author

@boringcactus boringcactus Jan 9, 2025

Choose a reason for hiding this comment

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

Originally, I had nearbyViewModel: NearbyTransitViewModel = koinViewModel() as an argument to NearbyTransitPage, but since navigation states can own ViewModels, calling koinViewModel() outside this composable creates a different instance than the one that's created as an argument in NearbyTransitView, but calling it here gives the same instance. This was absolutely miserable to try to debug.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That is tricky. To clarify my own understanding, that would be an issue even if we weren't using koin and were instantiating the view models directly, is that right? I haven't been using koinViewModel regularly, and wondering about differences / benefits we get with it aside from test defaults.

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe that this is inherent to the Android ViewModel subsystem and not specific to Koin's integration with it. My understanding is that using koinViewModel lets us pass repositories via Koin into the ViewModel without having to define a custom ViewModelProvider.Factory or deal with CreationExtras.

Comment on lines -237 to +241
// TODO reset view model
nearbyViewModel.reset()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Love a good TODO -> done

@boringcactus boringcactus merged commit e05b6c0 into main Jan 10, 2025
5 checks passed
@boringcactus boringcactus deleted the mth-android-reset-nearby branch January 10, 2025 20:55
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.

2 participants