Repository navigation
Add fromSavedState handle method to generated nav args #122
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
Changes from 3 commits
07894f0
80e7756
7444f78
d235f9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -248,6 +248,52 @@ class JavaNavWriter(private val useAndroidX: Boolean = true) : NavWriter<JavaCod | |
| addStatement("return $N", result) | ||
| }.build() | ||
|
|
||
| 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 -> | ||
| beginControlFlow("if ($N.contains($S))", savedStateHandle, arg.name).apply { | ||
| addStatement("$T $N", arg.type.typeName(), arg.sanitizedName) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not anymore with the newest changes using the common |
||
| addStatement("$N = $N.get($S)", arg.sanitizedName, savedStateHandle, arg.name) | ||
| addNullCheck(arg, arg.sanitizedName) | ||
| addStatement( | ||
| "$result.$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( | ||
| "$result.$N.put($S, $L)", | ||
| specs.hashMapFieldSpec, | ||
| arg.name, | ||
| arg.defaultValue.write() | ||
| ) | ||
| } | ||
| endControlFlow() | ||
| } | ||
| addStatement("return $N", result) | ||
| }.build() | ||
|
|
||
| val constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).build() | ||
|
|
||
| val copyConstructor = MethodSpec.constructorBuilder() | ||
|
|
@@ -298,6 +344,7 @@ class JavaNavWriter(private val useAndroidX: Boolean = true) : NavWriter<JavaCod | |
| .addMethod(constructor) | ||
| .addMethod(fromMapConstructor) | ||
| .addMethod(fromBundleMethod) | ||
| .addMethod(fromSavedStateHandleMethod) | ||
| .addMethods(specs.getters()) | ||
| .addMethod(specs.toBundleMethod("toBundle")) | ||
| .addMethod(specs.equalsMethod(className)) | ||
|
|
||
There was a problem hiding this comment.
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:then you can use it like this:
There was a problem hiding this comment.
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
KotlinNavWriterand the only thing that could make sense here is theelsebranch 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.