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

chore: Change the confusing parameter position to a clear name. #637 #638

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

JSpiner
Copy link

@JSpiner JSpiner commented Oct 16, 2024

Thank you for opening a Pull Request!


Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open a GitHub issue as a bug/feature request before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #637 🦕

It may be confusing that it is automatically updated by the position parameter. I renamed it to initialPosition to make it clear that it only works with the initial value.
(The compose foundation libraries also add the init prefix to the initial values ​​that go into rememberXXX.)


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.

Copy link

google-cla bot commented Oct 16, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@JSpiner JSpiner force-pushed the main branch 2 times, most recently from 7c63b15 to 34a1a7d Compare October 16, 2024 05:47
@JSpiner JSpiner changed the title Change the confusing parameter position to a clear name. #637 chore: Change the confusing parameter position to a clear name. #637 Nov 4, 2024
@JSpiner
Copy link
Author

JSpiner commented Nov 4, 2024

I resolved the conflict.

@kikoso
Copy link
Collaborator

kikoso commented Dec 2, 2024

Hi @JSpiner . This is a reasonable proposal. Typically we would like to not drop a breaking change, but to propose a transition time for folks to change.

I think we could offer an alternative constructor to the current one, deprecate the former and leave folks one version to upgrade to the new one. We could eventually pack this PR with another breaking change.

*/
@StateFactoryMarker
public operator fun invoke(
position: LatLng = LatLng(0.0, 0.0)
): MarkerState = MarkerState(position)
initialPosition: LatLng = LatLng(0.0, 0.0)
Copy link
Contributor

@bubenheimer bubenheimer Dec 3, 2024

Choose a reason for hiding this comment

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

I don't see the rationale for changing the parameter name here. Makes sense for rememberMarkerState, but not here. If you end up recomposing MarkerState.invoke(position) you will get a new MarkerState object.

It may look tempting because of the "initial marker position" KDoc verbiage, but that's how State objects generally work, including MutableState. It's mutableStateOf(value="bla").

Copy link
Author

Choose a reason for hiding this comment

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

You are right. This part didn't need to be updated.
However, I completely reverted and rewrote this part based on @kikoso 's opinion.
Please review again. 🙇

@JSpiner
Copy link
Author

JSpiner commented Dec 14, 2024

@kikoso @bubenheimer

Following @kikoso 's suggestion, I kept the existing rememberMarkerState function without breaking changes.
All existing changes have been reverted and rewritten.

  • Instead of leaving the existing 'rememberMarkerState' function as is, I added a comment that the state is not updated depending on the parameter.
  • Added 'rememberUpdatedMarkerState' which updates the state based on the parameter.
  • I wrote a test code for 'rememberUpdatedMarkerState'.

I think it would be a good idea to deprecate rememberMarkerState like below. I'd like to hear your opinions.

@Composable
@Deprecated(
    message = "Use 'rememberUpdatedMarkerState' instead - It may be confusing to think " +
            "that the state is automatically updated as the position changes, " +
            "so it will be changed.",
    replaceWith = ReplaceWith(
        expression = """
            val markerState = rememberUpdatedMarkerState()
        """
    )
)
public fun rememberMarkerState(
    key: String? = null,
    position: LatLng = LatLng(0.0, 0.0)
): MarkerState = rememberSaveable(key = key, saver = MarkerState.Saver) {
    MarkerState(position)
}

@bubenheimer
Copy link
Contributor

@JSpiner I agree with deprecating rememberMarkerState, it's a misleading API. The @ReplaceWith part seems off, though; I think it should give the original implementation of rememberMarkerState, to preserve semantics, and because users need to know where to find the Saver.

I disagree with the changed implementation of rememberUpdatedMarkerState, basing it on rememberSaveable instead of remember. This is invalid for the examples where it is used, and does not align with the rationale from the posts in #637 (comment)

@JSpiner
Copy link
Author

JSpiner commented Dec 14, 2024

@bubenheimer

Thanks for comments.

I disagree with the changed implementation of rememberUpdatedMarkerState, basing it on rememberSaveable instead of remember.

Sorry, my mistake. I changed it to use the original implementation.
356d429

I think it should give the original implementation of rememberMarkerState

I apply it. thanks
17f3857

Copy link
Contributor

@bubenheimer bubenheimer left a comment

Choose a reason for hiding this comment

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

A maintainer should review this as well.

@@ -281,6 +282,30 @@ class GoogleMapViewTests {
.performClick()
}

@Test
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this test actually pass?

Copy link
Author

@JSpiner JSpiner Dec 16, 2024

Choose a reason for hiding this comment

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

yes it is.

When I run the test with the existing rememberMarkerState, the test fails as below.
스크린샷 2024-12-16 23 14 02

When I run the test with rememberUpdatedMarkerState, the test succeeds as shown below.
스크린샷 2024-12-16 23 15 52

This repo requires approval to run CI, so it cannot be verified, but you can verify it by running the tests locally.

Copy link
Collaborator

Choose a reason for hiding this comment

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

FYI: the key is being read from the secrets in the local repo, so (unless you have them set on your fork) it will not run. Typically we run the tests on a PR copy to make sure they pass.

@JSpiner JSpiner changed the title chore: Change the confusing parameter position to a clear name. #637 chore: Change the confusing parameter position to a clear name. #637 Dec 16, 2024
@kikoso
Copy link
Collaborator

kikoso commented Dec 16, 2024

Also, @JSpiner : did you have the chance to take a look at the CLA agreement?

@JSpiner
Copy link
Author

JSpiner commented Dec 17, 2024

Also, @JSpiner : did you have the chance to take a look at the CLA agreement?

@kikoso I agreed to the CLA agreement. When I looked at the commit logs, my secondary git account name was included and was processed as a non-agreement. I re-organized them by rebasing. Thanks.


When merging @bubenheimer 's suggestion on github, he's name was included in the commit author 5761aaf. Would you also agree to the CLA?

스크린샷 2024-12-17 09 42 26

@bubenheimer
Copy link
Contributor

When merging @bubenheimer 's suggestion on github, he's name was included in the commit author 5761aaf. Would you also agree to the CLA?

Done

@JSpiner
Copy link
Author

JSpiner commented Dec 31, 2024

@kikoso
Hi, CLA validation passed. Can this be merged?

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.

[Feature request] Change the confusing markerState parameter position to a clear name.
4 participants