Skip to content

Add fromSavedState handle method to generated nav args - #122

Closed
simonschiller wants to merge 4 commits into
androidx:androidx-mainfrom
simonschiller:feature/from-saved-state-handle
Closed

Add fromSavedState handle method to generated nav args#122
simonschiller wants to merge 4 commits into
androidx:androidx-mainfrom
simonschiller:feature/from-saved-state-handle

Conversation

@simonschiller

Copy link
Copy Markdown
Contributor

Proposed Changes

  • Generate a fromSavedState method for all nav args classes, similar to the fromBundle one.

Testing

Test: Adapted existing tests to also include the new method

Issues Fixed

Fixes: 136967621

@dlam
dlam requested a review from jbw0033 January 22, 2021 20:40
@jbw0033
jbw0033 requested a review from danysantiago January 22, 2021 20:45

@danysantiago danysantiago left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for making this PR!

addStatement("return $N", result)
}.build()

val fromSavedStateHandleMethod = MethodSpec.methodBuilder("fromSavedStateHandle").apply {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There is a lot of shared code between this method spec and fromBundleMethod, lets try to combine it by creating a local function with the common code and then invoking it with the distinct, something like this:

fun MethodSpec.Builder.commonFromArgGetCode(
    fromVariableName: String,
    resultVariableName: String,
    arg: Argument,
    argGetBlock: MethodSpec.Builder.() -> Unit
) {
    beginControlFlow("if ($N.contains($S))", fromVariableName, arg.name).apply {
        argGetBlock()
        addNullCheck(arg, arg.sanitizedName)
        addStatement(
            "$resultVariableName.$N.put($S, $N)",
            specs.hashMapFieldSpec,
            arg.name,
            arg.sanitizedName
        )
    }
    if (arg.defaultValue == null) {
        nextControlFlow("else")
        addStatement(
            "throw new $T($S)", java.lang.IllegalArgumentException::class.java,
            "Required argument \"${arg.name}\" is missing and does " +
                "not have an android:defaultValue"
        )
    } else {
        nextControlFlow("else")
        addStatement(
            "$resultVariableName.$N.put($S, $L)",
            specs.hashMapFieldSpec,
            arg.name,
            arg.defaultValue.write()
        )
    }
    endControlFlow()
}

then you can use it like this:

val fromSavedStateHandleMethod = MethodSpec.methodBuilder("fromSavedStateHandle").apply {
    addAnnotation(annotations.NONNULL_CLASSNAME)
    addModifiers(Modifier.PUBLIC, Modifier.STATIC)
    addAnnotation(specs.suppressAnnotationSpec)
    val savedStateHandle = "savedStateHandle"
    addParameter(
        ParameterSpec.builder(SAVED_STATE_HANDLE_CLASSNAME, savedStateHandle)
            .addAnnotation(specs.androidAnnotations.NONNULL_CLASSNAME)
            .build()
    )
    returns(className)
    val result = "__result"
    addStatement("$T $N = new $T()", className, result, className)
    args.forEach { arg ->
        commonFromArgGetCode(
            fromVariableName = savedStateHandle,
            resultVariableName = result,
            arg= arg
        ) {
            addStatement("$T $N", arg.type.typeName(), arg.sanitizedName)
            addStatement("$N = $N.get($S)", arg.sanitizedName, savedStateHandle, arg.name)
        }
    }
    addStatement("return $N", result)
}.build()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done for the JavaNavWriter.

I also checked the KotlinNavWriter and the only thing that could make sense here is the else branch where we read default values, but given that it's only ~7 lines I'd keep it duplicated because I think it makes it more readable that way.

addStatement("$T $N = new $T()", className, result, className)
args.forEach { arg ->
beginControlFlow("if ($N.contains($S))", savedStateHandle, arg.name).apply {
addStatement("$T $N", arg.type.typeName(), arg.sanitizedName)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks like the variable declaration and initialization can be done in the same statement

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not anymore with the newest changes using the common addReadSingleArgBlock function 😄

@danysantiago danysantiago left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good!

@simonschiller
simonschiller deleted the feature/from-saved-state-handle branch February 19, 2021 08:50
harryzz pushed a commit to harryzz/compose-multiplatform-core that referenced this pull request Jul 28, 2026
## Proposed Changes

  - Generate a `fromSavedState` method for all nav args classes, similar to the `fromBundle` one.

## Testing

Test: Adapted existing tests to also include the new method

## Issues Fixed

Fixes: 136967621

This is an imported pull request from androidx#122.

Resolves JetBrains#122
Github-Pr-Head-Sha: d235f9a
GitOrigin-RevId: aa9caad

Change-Id: I74ef156e29b8955ef2422f59c8cc69c86424a43d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants