johncarl81 / parceler

:package: Android Parcelables made easy through code generation.
http://parceler.org
Apache License 2.0
3.56k stars 273 forks source link

Parceler & Navigation Component #372

Closed zhombie closed 5 years ago

zhombie commented 5 years ago

Is it possible to work with arg sender in Navigation Component (Jetpack)?

johncarl81 commented 5 years ago

I imagine so?

zhombie commented 5 years ago

Compiler says: Cause: part '' is keyword

My usage:

 <argument
    android:name="colors"
    android:defaultValue="@null"
    app:argType="com.nurmash.nurmash.domain.global.models.shop.Color$$Parcelable[]"
    app:nullable="true" />
zhombie commented 5 years ago

@johncarl81 could you check on your own, please, is it possible to use generated parcelables by safe args?

johncarl81 commented 5 years ago

If you put together a small demo app I will be able to validate or help you better.

zhombie commented 5 years ago

@johncarl81 Created a project: https://github.com/zhombie/testParcelerNavComponent. As you can see here, it cannot generate parceler. Compiler would say something like this: "Expecting comma or ')'"

johncarl81 commented 5 years ago

It looks like this is happening because the kotlin generated TestFragmentArgs is malformed:

package test.parcelernavcomponent

import android.os.Bundle
import android.os.Parcelable
import androidx.navigation.NavArgs
import java.io.Serializable
import java.lang.UnsupportedOperationException
import kotlin.Suppress
import kotlin.jvm.JvmStatic

data class TestFragmentArgs(val testModel: TestModel..Parcelable? = null) : NavArgs {
    @Suppress("CAST_NEVER_SUCCEEDS")
    fun toBundle(): Bundle {
        val result = Bundle()
        if (Parcelable::class.java.isAssignableFrom(TestModel..Parcelable::class.java)) {
            result.putParcelable("testModel", this.testModel as Parcelable?)
        } else if (Serializable::class.java.isAssignableFrom(TestModel..Parcelable::class.java)) {
            result.putSerializable("testModel", this.testModel as Serializable?)
        }
        return result
    }

    companion object {
        @JvmStatic
        fun fromBundle(bundle: Bundle): TestFragmentArgs {
            bundle.setClassLoader(TestFragmentArgs::class.java.classLoader)
            val __testModel : TestModel..Parcelable?
            if (bundle.containsKey("testModel")) {
                if (Parcelable::class.java.isAssignableFrom(TestModel..Parcelable::class.java) ||
                        Serializable::class.java.isAssignableFrom(TestModel..Parcelable::class.java))
                        {
                    __testModel = bundle.get("testModel") as TestModel..Parcelable?
                } else {
                    throw UnsupportedOperationException(TestModel..Parcelable::class.java.name +
                            " must implement Parcelable or Serializable or must be an Enum.")
                }
            } else {
                __testModel = null
            }
            return TestFragmentArgs(__testModel)
        }
    }
}

via the error:

e: /.../TestFragmentArgs.kt: (11, 53): Expecting comma or ')'
e: /.../TestFragmentArgs.kt: (11, 53): Expecting ')'
e: /.../TestFragmentArgs.kt: (11, 55): Expecting a top level declaration
e: /.../TestFragmentArgs.kt: (11, 65): Expecting a top level declaration
e: /.../TestFragmentArgs.kt: (11, 67): Expecting a top level declaration
e: /.../TestFragmentArgs.kt: (11, 69): Expecting a top level declaration
e: /.../TestFragmentArgs.kt: (11, 73): Expecting a top level declaration
e: /.../TestFragmentArgs.kt: (11, 75): Expecting a top level declaration
e: /.../TestFragmentArgs.kt: (11, 77): Expecting a top level declaration
e: /.../TestFragmentArgs.kt: (11, 85): Expecting a top level declaration
e: /.../TestFragmentArgs.kt: (23, 14): Unexpected tokens (use ';' to separate expressions on the same line)
e: /.../TestFragmentArgs.kt: (27, 40): Unexpected tokens (use ';' to separate expressions on the same line)
e: /.../TestFragmentArgs.kt: (32, 83): Unexpected tokens (use ';' to separate expressions on the same line)

notice on line ` it writes theTestModel$$ParcelableasTestModel..Parcelable`.

Is there a way to escape $ characters?

zhombie commented 5 years ago

Compiler says: Cause: part '' is keyword

My usage:

 <argument
    android:name="colors"
    android:defaultValue="@null"
    app:argType="com.nurmash.nurmash.domain.global.models.shop.Color$$Parcelable[]"
    app:nullable="true" />

@johncarl81 As you can see here, I have already tried Java generated parceler (There is $$ signs in class name). The same problem

zhombie commented 5 years ago

@johncarl81 Here, https://github.com/zhombie/testParcelerNavComponent/tree/java, branch java, I have created a new branch with java generated parcelable. Compiler says: java.lang.IllegalArgumentException: part '' is keyword

zhombie commented 5 years ago

@johncarl81 I think I will shift to kotlin based @Parcelize :D, I was loyal to parceler, but, all in all, the time has a property "change". Although, if you could solve this caused problem on above, it will give other users an ability to use Navigation Component in combination with parceler, also for me it will give easy migration.

johncarl81 commented 5 years ago

Fair enough. There are certainly other libraries with other focuses (like Kotlin dev.) out there.

sahuadarsh0 commented 4 years ago

@johncarl81 its not working in java based Navigation Component Safe args