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

[Feature request] Change the confusing markerState parameter position to a clear name. #637

Open
JSpiner opened this issue Oct 16, 2024 · 3 comments · May be fixed by #638
Open

[Feature request] Change the confusing markerState parameter position to a clear name. #637

JSpiner opened this issue Oct 16, 2024 · 3 comments · May be fixed by #638
Labels
triage me I really want to be triaged. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@JSpiner
Copy link

JSpiner commented Oct 16, 2024

MarkerState has rememberMarkerState for use in compose.

@Composable
public fun rememberMarkerState(
    key: String? = null,
    position: LatLng = LatLng(0.0, 0.0)
): MarkerState = rememberSaveable(key = key, saver = MarkerState.Saver) {
    MarkerState(position)
}
// wrong usage example
@Composable
public fun MyGoogleMapScreen(stationPosition: LatLng) {
    val myMarkerState = rembmerMarkerState(position = stationPosition)
}

But looking at the example above, it's easy to get confused.
This is because there is a risk of misunderstanding that the value entered as a parameter to remember (position in this case) acts as a key that is automatically reflected when the value changes.
I also had a hard time because the position wasn't updated.
To avoid this misunderstanding, compose foundation libraries add an inital prefix to rememberXXX functions.

// compose library example
// rememberScrollState from `androidx.compose.foundation`
@Composable
fun rememberScrollState(initial: Int = 0): ScrollState {
    return rememberSaveable(saver = ScrollState.Saver) {
        ScrollState(initial = initial)
    }
}

// rememberLazyListState from `androidx.compose.foundation.lazy`
@Composable
fun rememberLazyListState(
    initialFirstVisibleItemIndex: Int = 0,
    initialFirstVisibleItemScrollOffset: Int = 0
): LazyListState {
    return rememberSaveable(saver = LazyListState.Saver) {
        LazyListState(
            initialFirstVisibleItemIndex,
            initialFirstVisibleItemScrollOffset
        )
    }
}

// rememberLazyGridState from `androidx.compose.foundation.lazy.grid`
@Composable
fun rememberLazyGridState(
    initialFirstVisibleItemIndex: Int = 0,
    initialFirstVisibleItemScrollOffset: Int = 0
): LazyGridState {
    return rememberSaveable(saver = LazyGridState.Saver) {
        LazyGridState(
            initialFirstVisibleItemIndex,
            initialFirstVisibleItemScrollOffset
        )
    }
}

So my suggestion is to change the name from position to initialPosition to reduce confusion.
It seems like a simple fix, I'll create a PR. Please review it.
Thanks!

@JSpiner JSpiner added triage me I really want to be triaged. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. labels Oct 16, 2024
JSpiner added a commit to JSpiner/android-maps-compose that referenced this issue Oct 16, 2024
JSpiner added a commit to JSpiner/android-maps-compose that referenced this issue Oct 16, 2024
JSpiner added a commit to JSpiner/android-maps-compose that referenced this issue Oct 16, 2024
JSpiner added a commit to JSpiner/android-maps-compose that referenced this issue Oct 16, 2024
@bubenheimer
Copy link
Contributor

Personally I'd be in favor of eliminating rememberMarkerState entirely. I find it misleading and an anti-pattern:

https://dev.to/bubenheimer/effective-map-composables-non-draggable-markers-2b2#:~:text=Be%20aware%20that,recommend%20ignoring%20it.

https://dev.to/bubenheimer/effective-map-composables-draggable-markers-3bea#:~:text=This%20behavior%20is,with%20a%20model.

@dkhawk
Copy link
Contributor

dkhawk commented Oct 16, 2024 via email

@JSpiner
Copy link
Author

JSpiner commented Nov 4, 2024

@dkhawk

Yes, it can be confusing. Not only me, but my coworkers are also confused.

@Composable
fun rememberUpdatedMarkerState(position: LatLng): MarkerState =
    // This pattern is equivalent to what rememberUpdatedState() does:
    // rememberUpdatedState() uses MutableState, we use MarkerState.
    // This is more efficient than updating position in an effect,
    // as we avoid an additional recomposition.
    remember { MarkerState(position = position) }.also {
        it.position = position
    }

The example code included in this repository also creates and uses rememberUpdatedMarkerState to solve this problem.
I also solved the problem in a similar way.
It seems okay to provide this function, but I think the first thing to do is to distinguish between initPosition and position so that there is no confusion.

#638
Can you review my PR and leave comments?

JSpiner added a commit to JSpiner/android-maps-compose that referenced this issue Nov 4, 2024
JSpiner added a commit to JSpiner/android-maps-compose that referenced this issue Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage me I really want to be triaged. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants