joemasilotti / joemasilotti/bridge-components
Android: `ClassCastException` in `FormComponent` when used within HotwireWebBottomSheetFragment
- Dominant language
- Kotlin
- Stars
- 478
- Forks
- 28
- PR merge metrics
- No merged PRs in 30d
Description
Hey there, I stumbled into an exception when using the `FormComponent` within a web bottom sheet. I am using the latest Jumpstart Pro Android. I used AI to get a better GitHub Issue, which you can find below.
My current solution is to just use the Jumpstart Pro Android implementation, but ideally I'd use this package because it's less code to maintain for me.
---
A `java.lang.ClassCastException` occurs when a `FormComponent` is initialized inside a destination that inherits from `HotwireWebBottomSheetFragment`. This is because the component currently performs a hard cast to `HotwireFragment`.
In Hotwire Native 1.2.4, `HotwireFragment` and `HotwireWebBottomSheetFragment` are siblings (both implementing `HotwireDestination`), meaning one cannot be cast to the other.
**Stack Trace:**
```text
java.lang.ClassCastException: de.package.name.WebBottomSheetFragment cannot be cast to dev.hotwire.navigation.fragments.HotwireFragment
at com.masilotti.bridgecomponents.form.FormComponent.getFragment(FormComponent.kt:34)
at com.masilotti.bridgecomponents.form.FormComponent.addButton(FormComponent.kt:48)
at com.masilotti.bridgecomponents.form.FormComponent.onReceive(FormComponent.kt:38)
```
**Affected Code:**
The issue lies in the `fragment` property getter in `FormComponent.kt`:
```kotlin
private val fragment: HotwireFragment
get() = bridgeDelegate.destination.fragment as HotwireFragment
```
**Proposed Solution:**
The property should be widened to `Fragment` (or `HotwireDestination`), and the toolbar access should handle both standard fragments and bottom sheets.
```kotlin
private val destinationFragment: Fragment
get() = bridgeDelegate.destination.fragment
private fun getToolbar(): Toolbar? {
return when (val dest = destinationFragment) {
is HotwireFragment -> dest.toolbarForNavigation()
is HotwireWebBottomSheetFragment -> dest.toolbarForNavigation()
else -> null
}
}
```
**Environment:**
* Library Version: `0.12.1`
* Hotwire Native Android Version: `1.2.4`
* Kotlin Android Version: `2.1.0`
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in FormComponent.kt at the fragment property getter and its use from addButton and onReceive. Review how HotwireFragment and HotwireWebBottomSheetFragment expose toolbarForNavigation(), then verify that a bottom-sheet destination no longer triggers the ClassCastException while standard fragments retain toolbar behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin
- Domain
- mobile-dev
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 72/100