forked from software-mansion/react-native-screens
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refact(Android): allow for different fragment types inside ScreenCont…
…ainer (software-mansion#1887) ## Description This PR is laying out ground work under not-so-distant Android modal & bottom sheet implementation. Currently `ScreenContainer<T: ScreenFragment>: ViewGroup` (and thus `ScreenStack: ScreenContainer<ScreenStackFragment>`) operates on `ScreenFragment: Fragment` objects, making it impossible to use any other `Fragment` subclass with the stack (only single inheritance is possible on JVM). This PR changes the situation by introducing `ScreenFragmentWrapper` interface which both: 1. holds the reference to underlaying `Fragment` 2. provides additional API previously directly attached to the `ScreenFragment` Also introduced `ScreenStackFragmentWrapper` extends `ScreenFragmentWrapper` with API previously attached to `ScreenStackFragment`. Thereafter by implementing such interface by the fragment subclass itself we can have both of worlds: 1. Customized fragment behaviour 2. Implementation of all methods required for fragment<->container communication. Disclaimer: *I have decided to split "Android modals PR" into few smaller ones to reduce its size and complexity. I won't use "stack PR" technique to spare myself whole bunch of rebasing & conflict resolving.* ## Changes 1. `ScreenContainer` is no longer parameterized with `T: ScreenFragment`. It uses `ScreenFragmentWrapper` as screen-primitve base type now. 2. Our fragment types `ScreenFragment` & `ScreenStackFragment` now implement `ScreenFragmentWrapper` & `ScreenStackFragmentWrapper` interfaces respectively. 3. `ScreenContainer` has access to the fragments it operates on (fragment API) via `ScreenFragmentWrapper.fragment` property. 4. Our custom methods previously implemented directly on `Fragment` subclass are exposed via the new interfaces so `ScreenContainer` still has access to them. ## Test code and steps to reproduce See that test examples build & CI passes. I did some manual testing in our example apps & didn't notice any regression. ## Checklist - [x] Ensured that CI passes
- Loading branch information
Showing
13 changed files
with
206 additions
and
135 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
android/src/main/java/com/swmansion/rnscreens/FragmentHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.swmansion.rnscreens | ||
|
||
import androidx.fragment.app.Fragment | ||
|
||
interface FragmentHolder { | ||
val fragment: Fragment | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
android/src/main/java/com/swmansion/rnscreens/ScreenEventDispatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.swmansion.rnscreens | ||
|
||
interface ScreenEventDispatcher { | ||
fun canDispatchLifecycleEvent(event: ScreenFragment.ScreenLifecycleEvent): Boolean | ||
fun updateLastEventDispatched(event: ScreenFragment.ScreenLifecycleEvent) | ||
|
||
/** | ||
* Dispatches given screen lifecycle event to JS using screen from given fragment `fragmentWrapper` | ||
*/ | ||
fun dispatchLifecycleEvent(event: ScreenFragment.ScreenLifecycleEvent, fragmentWrapper: ScreenFragmentWrapper) | ||
|
||
/** | ||
* Dispatches given screen lifecycle event from all non-empty child containers to JS | ||
*/ | ||
fun dispatchLifecycleEventInChildContainers(event: ScreenFragment.ScreenLifecycleEvent) | ||
|
||
fun dispatchHeaderBackButtonClickedEvent() | ||
fun dispatchTransitionProgressEvent(alpha: Float, closing: Boolean) | ||
|
||
// Concrete dispatchers | ||
} |
Oops, something went wrong.