Skip to content

Commit ef3cc02

Browse files
authored
refactor(Android): add isTranslucent to ScreenFragmentWrapper & align method naming on Screen (#2818)
## Description Makes it more straightforward to get translucency information from fragment. ## Changes - **Expose `isTranslucent` on `ScreenFragmentWrapper` & rename `Screen.isTransparent`** - **Add comments** - **Add comments 2** - **Use newly exposed method** - **Update ScreenModalFragment** ## Test code and steps to reproduce Nothing changes in behaviour. ## Checklist - [ ] Ensured that CI passes
1 parent 4d044da commit ef3cc02

6 files changed

Lines changed: 26 additions & 7 deletions

File tree

android/src/main/java/com/swmansion/rnscreens/Screen.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,10 @@ class Screen(
260260
)
261261
}
262262

263-
fun isTransparent(): Boolean =
263+
/**
264+
* Whether this screen allows to see the content underneath it.
265+
*/
266+
fun isTranslucent(): Boolean =
264267
when (stackPresentation) {
265268
StackPresentation.TRANSPARENT_MODAL,
266269
StackPresentation.FORM_SHEET,

android/src/main/java/com/swmansion/rnscreens/ScreenFragment.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ open class ScreenFragment :
128128
ScreenWindowTraits.trySetWindowTraits(screen, activity, tryGetContext())
129129
}
130130

131+
// Plain ScreenFragments can not be translucent
132+
override fun isTranslucent() = false
133+
131134
override fun tryGetActivity(): Activity? {
132135
activity?.let { return it }
133136
val context = screen.context

android/src/main/java/com/swmansion/rnscreens/ScreenFragmentWrapper.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ interface ScreenFragmentWrapper :
2626

2727
fun onViewAnimationEnd()
2828

29+
// Fragment information
30+
31+
/**
32+
* Whether this screen fragment makes it possible to see content underneath it
33+
* (not fully opaque or does not fill full screen).
34+
*/
35+
fun isTranslucent(): Boolean
36+
37+
2938
// Helpers
3039
fun tryGetActivity(): Activity?
3140

android/src/main/java/com/swmansion/rnscreens/ScreenModalFragment.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class ScreenModalFragment :
9292
savedInstanceState: Bundle?,
9393
): View? = null
9494

95+
override fun isTranslucent(): Boolean = true
96+
9597
override fun dismissFromContainer() {
9698
check(container is ScreenStack)
9799
val container = container as ScreenStack

android/src/main/java/com/swmansion/rnscreens/ScreenStack.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class ScreenStack(
184184
newTop = notDismissedWrappers.firstOrNull()
185185
visibleBottom =
186186
notDismissedWrappers
187-
.dropWhile { it.screen.isTransparent() }
187+
.dropWhile { it.isTranslucent() }
188188
.firstOrNull()
189189
?.takeUnless { it === newTop }
190190

@@ -238,8 +238,8 @@ class ScreenStack(
238238
childDrawingOrderStrategy = SwapLastTwo()
239239
} else if (newTop != null &&
240240
newTopAlreadyInStack &&
241-
topScreenWrapper?.screen?.isTransparent() == true &&
242-
newTop.screen.isTransparent() == false
241+
topScreenWrapper?.isTranslucent() == true &&
242+
newTop.isTranslucent() == false
243243
) {
244244
// In case where we dismiss multiple transparent views we want to ensure
245245
// that they are drawn in correct order - Android swaps them by default,
@@ -250,7 +250,7 @@ class ScreenStack(
250250
.asSequence()
251251
.takeWhile {
252252
it !== newTop &&
253-
it.screen.isTransparent()
253+
it.isTranslucent()
254254
}.count()
255255
if (dismissedTransparentScreenApproxCount > 1) {
256256
childDrawingOrderStrategy =
@@ -311,7 +311,7 @@ class ScreenStack(
311311
private fun turnOffA11yUnderTransparentScreen(visibleBottom: ScreenFragmentWrapper?) {
312312
if (screenWrappers.size > 1 && visibleBottom != null) {
313313
topScreenWrapper?.let {
314-
if (it.screen.isTransparent()) {
314+
if (it.isTranslucent()) {
315315
val screenFragmentsBeneathTop = screenWrappers.slice(0 until screenWrappers.size - 1).asReversed()
316316
// go from the top of the stack excluding the top screen
317317
for (fragmentWrapper in screenFragmentsBeneathTop) {

android/src/main/java/com/swmansion/rnscreens/ScreenStackFragment.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ class ScreenStackFragment :
8686
)
8787
}
8888

89+
override fun isTranslucent(): Boolean = screen.isTranslucent()
90+
8991
override fun removeToolbar() {
9092
appBarLayout?.let {
9193
toolbar?.let { toolbar ->
@@ -380,7 +382,7 @@ class ScreenStackFragment :
380382
// If the screen is a transparent modal with hidden header we don't want to update the toolbar
381383
// menu because it may erase the menu of the previous screen (which is still visible in these
382384
// circumstances). See here: https://github.com/software-mansion/react-native-screens/issues/2271
383-
if (!screen.isTransparent() || screen.headerConfig?.isHeaderHidden == false) {
385+
if (!screen.isTranslucent() || screen.headerConfig?.isHeaderHidden == false) {
384386
updateToolbarMenu(menu)
385387
}
386388
return super.onPrepareOptionsMenu(menu)

0 commit comments

Comments
 (0)