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

feat(android.NearbyTransitPage): leave / rejoin vehicles channel after backgrounding #579

Merged
merged 10 commits into from
Dec 17, 2024

Conversation

KaylaBrady
Copy link
Collaborator

@KaylaBrady KaylaBrady commented Dec 10, 2024

Summary

Ticket: 🤖 | Stability | Leave realtime channels when backgrounding

What is this PR for?

This PR is one slice of the above ticket, focused only on the vehicles channel for early feedback. I plan to extend this approach to the existing subscribeToPredictions and subscribeToAlerts channels.

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

Testing

What testing have you done?

  • Ran locally with logs in place, confirmed that vehicles channel properly opens & closes when changing routes and when backgrounding / restoring.
  • Added tests for pause / resume events

Comment on lines 94 to 99
var railRouteLineData: List<RouteLineData>? by remember { mutableStateOf(null) }
var stopSourceData: FeatureCollection? by remember { mutableStateOf(null) }
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just to prevent crashes while debugging

var stopDetailsDepartures by rememberSaveable { mutableStateOf<StopDetailsDepartures?>(null) }
var vehiclesData: List<Vehicle> by remember { mutableStateOf(emptyList()) }

var stopDetailsDepartures by remember { mutableStateOf<StopDetailsDepartures?>(null) }
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed to remember for debugging. Should probably move into the new VM.

Comment on lines 80 to 81
val viewModel: VehiclesViewModel =
viewModel(factory = VehiclesViewModel.Factory(vehiclesRepository))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I initially tried following the same approach as subscribeToPredictions, but onCleared was never invoked (and from what I can tell, also isn't invoked with subscribeToPredictions.

with that, having a single viewModel that persists across renders and dynamically connecting / disconnecting to the correct channel based on changing topic or lifecycle event seemed more reliable.

If this approach is sound, I'll follow-up with making subscribeToPredictions & subscribeToAlerts consistent.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm definitely up to adopt the ViewModel-centric approach and refactor the two subscribe functions. The only thing I want to suggest is that the ViewModel should abstract away the concept of Channels from the View, e.g. is I'd prefer to avoid lower-level functions like subscribeToVehicleTopic(topic: String) in favor of functions like updateFollowedVehicle(vehicle: Vehicle).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good call on naming - I'll switch to something a bit more descriptive like subscribeToVehiclesOnRoute

@@ -56,11 +56,12 @@ fun ContentView(
rememberBottomSheetScaffoldState(bottomSheetState = rememberStandardBottomSheetState())
var navBarVisible by remember { mutableStateOf(true) }

DisposableEffect(null) {
LifecycleResumeEffect(null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

}
}
}

fun handleRouteChange(route: SheetRoutes?) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What do we think about refactoring this a bit so that more logic is in the view model? Maybe even automatically after changing the StopDetailsFilter?

@KaylaBrady KaylaBrady force-pushed the kb-android-backgrounding branch 2 times, most recently from ce17eab to 40d42df Compare December 12, 2024 19:10
@KaylaBrady KaylaBrady marked this pull request as ready for review December 12, 2024 19:12
@KaylaBrady KaylaBrady requested a review from a team as a code owner December 12, 2024 19:12
@KaylaBrady KaylaBrady requested a review from EmmaSimon December 12, 2024 19:12
@KaylaBrady KaylaBrady force-pushed the kb-android-backgrounding branch from 43aaf33 to 9fa0344 Compare December 13, 2024 20:39
import org.koin.compose.koinInject

class VehiclesViewModel(
private val vehiclesRepository: IVehiclesRepository,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

One thing worth mentioning here is that I didn't use the TImerViewModel that was used in subscribeToPredictions & subscribeToAlerts to synchronize updates. My perception was it was taking vehicles a long time to appear on the screen - perhaps because waiting for the 1s synchronized update. @JackVCurtis would be curious to learn more about your findings with using the TimerViewModel though - perhaps it should just have a more frequent rate.

@KaylaBrady KaylaBrady force-pushed the kb-android-backgrounding branch from 6210e40 to 46dc1ed Compare December 17, 2024 18:10
@KaylaBrady KaylaBrady force-pushed the kb-android-backgrounding branch from 46dc1ed to 8b36601 Compare December 17, 2024 20:32
@@ -82,6 +82,7 @@ dependencies {
implementation(libs.mapbox.turf)
implementation(libs.okhttp)
implementation(libs.playServices.location)
implementation(libs.androidx.lifecycle.runtime.testing)
Copy link
Member

Choose a reason for hiding this comment

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

Should this be an androidTestImplementation dependency so it's not available in the main source root (and so it's not included in the dependency list)?

@KaylaBrady KaylaBrady merged commit a264d30 into main Dec 17, 2024
7 checks passed
@KaylaBrady KaylaBrady deleted the kb-android-backgrounding branch December 17, 2024 21:42
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.

4 participants