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

java.lang.ClassCastException: com.revenuecat.purchases.models.GoogleStoreProduct cannot be cast to io.shortway.kobankat.models.StoreProduct #48

Closed
Moozart opened this issue Mar 30, 2024 · 10 comments · Fixed by #57
Assignees

Comments

@Moozart
Copy link

Moozart commented Mar 30, 2024

Hello,

When I use map like this in composeApp module

availablePackages.map { pack -> pack.product as StoreProduct}

Throws java.lang.ClassCastException: com.revenuecat.purchases.models.GoogleStoreProduct cannot be cast to io.shortway.kobankat.models.StoreProduct

If don't add type casting, throws build time error

Type mismatch: inferred type is com.revenuecat.purchases.models.StoreProduct but io.shortway.kobankat.models.StoreProduct was expected

Do you have any idea or advice.
Thanks to you for good libray

@JayShortway
Copy link
Member

Hi, thanks for flagging this!

If you don't add type casting, which target are you building that throws the build error?
What's the reason for the cast? To attempt to fix the build error, or is there an additional reason?

I'm currently on holiday so I can't try to reproduce it myself unfortunately.

@Moozart
Copy link
Author

Moozart commented Mar 31, 2024

Target Android has this error. IOS not. I just want to copy products in cacheStoreProductsList: List to purchase it via productId finding from cacheStoreProductList. Because I pass only Id to uiState.

I changed my strategy after that crash. I used Package to purchase via identifier. There is no issue with Package

@Qw4z1
Copy link

Qw4z1 commented Apr 3, 2024

I'm also seeing Type mismatch: inferred type is com.revenuecat.purchases.models.StoreProduct but io.shortway.kobankat.models.StoreProduct was expected at compile time, even though I'm importing io.shortway.kobankat.models.StoreProduct and just call offering.package.product to get the StoreProduct.

Could it be this line that is confusing things?

@JayShortway
Copy link
Member

@Moozart Good to hear you found a workaround!
@Qw4z1 I think you might be right that that line is the culprit! Thanks for pinpointing it.

For some reason StoreProduct is not a type alias, while most other classes are. I'm not sure what the reason for that was anymore.

Since I'm currently on holiday, I'll be able to look at this in about 2 weeks or so.

@Qw4z1
Copy link

Qw4z1 commented Apr 4, 2024

@JayShortway No stress! You've done plenty already! =)

I might take a stab at it if I get time before you're back. Not my area of expertise, though, so no promises. 😅

@JayShortway
Copy link
Member

@Qw4z1 Oh yes, that's very welcome of course! Feel free to take a look if you have the time, but no worries if you don't.

@JayShortway
Copy link
Member

I have a reproducer which should be run on CI once #56 is merged, to prevent regressions. Haven't had time to think about a proper fix yet though.

@JayShortway
Copy link
Member

For some reason StoreProduct is not a type alias, while most other classes are. I'm not sure what the reason for that was anymore.

The reason for this is that the official SDK defines StoreProduct as an interface on Android, and as a class on iOS. Since the common definition in KobanKat is expect class, it's impossible to type-alias that to an interface. (It is type aliased on iOS.) Changing the common definition to expect interface would inverse the situation, but I'm not sure yet if that's preferable.

@Qw4z1
Copy link

Qw4z1 commented Apr 22, 2024

Another way to solve it is to write wrapper classes instead of expect/actuals and expose NativeXyz interfaces from Android and IOS. It's not as elegant, but at least there is no shadowing. Works well in Firebase-kotlin-sdk.

@JayShortway
Copy link
Member

@Qw4z1 That's true! In general it's a good suggestion to look at firebase-kotlin-sdk for inspiration. However that's quite a big refactor. I think for now a more pragmatic solution would be to rename the Package.product field so as to not shadow the official SDK.

@Moozart That means the field to get the StoreProduct from a Package will be called storeProduct (instead of product).

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 a pull request may close this issue.

3 participants