-
-
Notifications
You must be signed in to change notification settings - Fork 608
feat(Android): Add support for different Top App Bar styles #1874
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
Closed
tboba
wants to merge
63
commits into
main
from
@tboba/introduce-material3-finally-prettier-android-ui
Closed
Changes from all commits
Commits
Show all changes
63 commits
Select commit
Hold shift + click to select a range
004f42e
Create first version of large top app bar
tboba 17c4885
Add better prototype of large top app bar
tboba 28e9ccc
Fix missing background in nested scroll view
tboba 0a5412f
Remove unnecessary lines, fix problem with existing view in hierarchy
tboba b2173fe
Add ability to choose header type in RN
tboba 68c3cf2
Fix issue with not working toolbar after transitioning to another screen
tboba 3188b25
Move headerType prop from ScreenStackHeaderConfig to Screen, fix tran…
tboba 69df636
Make header swipeable
tboba fa3e5b6
Apply fix for incorrect behavior when navigation bar appearance is set
tboba 2d22607
Apply fix for jumping header
tboba dddd72f
Remove redundant mToolbar#let
tboba 787c9ac
Remove incorrect inset on large headers
tboba b45db30
Add `fitsSystemWindows` prop to createNestedScrollViewFromScreen
tboba bae6092
Apply propagating props from toolbar to collapsing toolbar layout
tboba 390344c
Refactorize way how `collapsingToolbarLayout is being created`
tboba cf32cbc
Fix not working `headerShown` prop for medium / large headers
tboba 2a35a83
De-addict creating CollapsingToolbarLayout from header mode
tboba 1f7f6a7
Refactor classes related to toolbar management, remove NestedScrollView
tboba d2f48e8
Revert ScreenStackToolbar class, add support for most of the props to…
tboba 4520dfa
Add fallback values to `getHeightOfToolbar`, change default value of …
tboba cab619f
Cleanup code, add warning when user changes header type in Screen on …
tboba 4a4c25c
Change misleading comment
tboba 0be4b17
Fix shadow for AppBarLayout on Material 3, add note about bundling Ma…
tboba d5fa5b0
Remove `getViewTree` utility method
tboba c4464e5
Add finishing touches for test
tboba 6df79f1
Merge branch 'main' into @tboba/introduce-material3-finally-prettier-…
tboba dbb8bdc
Add import for Test1874 at FabricTestExample's App.js
tboba 7d144c0
Revert `headerTopInsetEnabled` to false, fix top inset on header
tboba f63976e
Revert removing private from prop
tboba 7292b92
Cleanup unncessary commits, revert rnsDefaultTargetSdkVersion to 31
tboba a1d7966
Revert unnecessary changes from Test1844, change default target/compi…
tboba 651b8c3
Comment headerLargeTitle
tboba 4cb1db6
My bad
tboba c53b773
Add comment to Test1844
tboba a9942be
Merge branch 'main' into @tboba/introduce-material3-finally-prettier-…
tboba 2bb3e5f
Change `layoutParams` declaration
tboba 76cedb4
Merge branch 'main' into @tboba/introduce-material3-finally-prettier-…
tboba 0a9c345
Merge branch 'main' into @tboba/introduce-material3-finally-prettier-…
tboba ab0637e
Merge branch 'main' into @tboba/introduce-material3-finally-prettier-…
tboba c3d2dca
Merge branch 'main' into @tboba/introduce-material3-finally-prettier-…
tboba 493a15c
Fix naming
tboba 4bb960b
Fix issues with using R.attr
tboba 83b0ba3
Remove unncessary from ScreenStackHeaderConfig
tboba 49c0560
Remove center-aligned header type, add headerTItleAlign prop
tboba 925e42c
Fix documentation
tboba 25fc505
Prettify docs, move headerType to the better place
tboba cdea990
Revert removing adjustment for AppBarLayout, revert removing toolbar
tboba 71c07b9
Move logic of creating toolbars to ScreenStackHeader
tboba ff4f51d
Create wrapper ScreenStackHeader for combining toolbar and collapsing…
tboba 7168134
Add another screen to the Test1874
tboba 144ddf2
Add ability to change header type during fast-refresh/live, moar exam…
tboba 1b500a3
Remove wrong message about unsupported header type update
tboba b20c9da
Merge branch 'main' into @tboba/introduce-material3-finally-prettier-…
tboba 4c5612a
Remove fitsSystemWindows from appBarLayout
tboba a4f4650
Fix docs
tboba e04221f
Merge branch 'main' into @tboba/introduce-material3-finally-prettier-…
tboba 6659095
Merge branch 'main' into @tboba/introduce-material3-finally-prettier-…
tboba 2d83af5
Merge branch 'main' into @tboba/introduce-material3-finally-prettier-…
tboba bb18c04
Merge branch 'main' into @tboba/introduce-material3-finally-prettier-…
tboba 9d75bd9
Remove leftovers after merge
tboba 88ac350
Update material to 1.9.0, fix config manager delegate
tboba ad91a46
Fix issue with linting
tboba 848254f
Fix issues with calculating header height
tboba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ package com.swmansion.rnscreens | |
|
|
||
| import android.annotation.SuppressLint | ||
| import android.content.Context | ||
| import android.content.res.TypedArray | ||
| import android.graphics.Color | ||
| import android.os.Build | ||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.Menu | ||
|
|
@@ -20,14 +22,17 @@ import com.facebook.react.uimanager.PixelUtil | |
| import com.google.android.material.appbar.AppBarLayout | ||
| import com.google.android.material.appbar.AppBarLayout.ScrollingViewBehavior | ||
| import com.swmansion.rnscreens.utils.DeviceUtils | ||
| import com.google.android.material.R as MaterialR | ||
|
|
||
| class ScreenStackFragment : | ||
| ScreenFragment, | ||
| ScreenStackFragmentWrapper { | ||
| private var appBarLayout: AppBarLayout? = null | ||
| private var toolbar: Toolbar? = null | ||
| var screenStackHeader = ScreenStackHeader(screen) | ||
|
|
||
| private var isToolbarShadowHidden = false | ||
| private var isToolbarTranslucent = false | ||
| private var isToolbarHidden = false | ||
|
|
||
| private var lastFocusedChild: View? = null | ||
|
|
||
|
|
@@ -44,25 +49,43 @@ class ScreenStackFragment : | |
| } | ||
|
|
||
| override fun removeToolbar() { | ||
| appBarLayout?.let { | ||
| toolbar?.let { toolbar -> | ||
| if (toolbar.parent === it) { | ||
| it.removeView(toolbar) | ||
| isToolbarHidden = true | ||
|
|
||
| appBarLayout?.let { appBarLayout -> | ||
| screenStackHeader.collapsingToolbarLayout?.let { collapsingToolbar -> | ||
| if (collapsingToolbar.parent === appBarLayout) { | ||
| appBarLayout.removeView(collapsingToolbar) | ||
| } | ||
| } | ||
| } | ||
| toolbar = null | ||
|
|
||
| screenStackHeader.removeToolbar() | ||
|
|
||
| // As AppBarLayout may have dimensions of expanded medium / large header, | ||
| // We need to change its layout params to `WRAP_CONTENT`. | ||
| appBarLayout?.layoutParams = | ||
| CoordinatorLayout.LayoutParams( | ||
| CoordinatorLayout.LayoutParams.MATCH_PARENT, | ||
| CoordinatorLayout.LayoutParams.WRAP_CONTENT, | ||
| ) | ||
| } | ||
|
|
||
| override fun setToolbar(toolbar: Toolbar) { | ||
| appBarLayout?.addView(toolbar) | ||
| toolbar.layoutParams = | ||
| AppBarLayout | ||
| .LayoutParams( | ||
| AppBarLayout.LayoutParams.MATCH_PARENT, | ||
| AppBarLayout.LayoutParams.WRAP_CONTENT, | ||
| ).apply { scrollFlags = 0 } | ||
| this.toolbar = toolbar | ||
| isToolbarHidden = false | ||
| screenStackHeader.toolbar = toolbar as CustomToolbar | ||
| screenStackHeader.recreateToolbar() | ||
|
|
||
| screenStackHeader.collapsingToolbarLayout?.apply { | ||
| appBarLayout?.addView(this) | ||
| } | ||
|
|
||
| // As `setToolbar` may be called after changing header's visibility, | ||
| // we need to apply correction to layoutParams with proper dimensions. | ||
| appBarLayout?.layoutParams = | ||
| CoordinatorLayout.LayoutParams( | ||
| CoordinatorLayout.LayoutParams.MATCH_PARENT, | ||
| getHeightOfToolbar(toolbar.context), | ||
| ) | ||
| } | ||
|
|
||
| override fun setToolbarShadowHidden(hidden: Boolean) { | ||
|
|
@@ -78,6 +101,7 @@ class ScreenStackFragment : | |
| val params = screen.layoutParams | ||
| (params as CoordinatorLayout.LayoutParams).behavior = | ||
| if (translucent) null else ScrollingViewBehavior() | ||
|
|
||
| isToolbarTranslucent = translucent | ||
| } | ||
| } | ||
|
|
@@ -128,19 +152,32 @@ class ScreenStackFragment : | |
| // role. On top of that it breaks screens animations when alfa offscreen compositing is off | ||
| // (which is the default) | ||
| setBackgroundColor(Color.TRANSPARENT) | ||
|
|
||
| layoutParams = | ||
| AppBarLayout.LayoutParams( | ||
| AppBarLayout.LayoutParams.MATCH_PARENT, | ||
| AppBarLayout.LayoutParams.WRAP_CONTENT, | ||
| getHeightOfToolbar(context), | ||
| ) | ||
|
|
||
| // On Material 3 the elevation is not visible on AppBarLayout. | ||
| // To prevent this behavior, we're setting outline shadow colors to black. | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
| outlineAmbientShadowColor = Color.BLACK | ||
| outlineSpotShadowColor = Color.BLACK | ||
| } | ||
|
|
||
| screenStackHeader.collapsingToolbarLayout?.let { | ||
| addView(recycleView(it)) | ||
| } | ||
| } | ||
|
|
||
| view?.addView(appBarLayout) | ||
| appBarLayout?.let { view?.addView(it) } | ||
|
|
||
| if (isToolbarShadowHidden) { | ||
| appBarLayout?.elevation = 0f | ||
| appBarLayout?.stateListAnimator = null | ||
| } | ||
| toolbar?.let { appBarLayout?.addView(recycleView(it)) } | ||
|
|
||
| setHasOptionsMenu(true) | ||
| return view | ||
| } | ||
|
|
@@ -166,6 +203,28 @@ class ScreenStackFragment : | |
| return super.onCreateOptionsMenu(menu, inflater) | ||
| } | ||
|
|
||
| private fun getHeightOfToolbar(context: Context): Int { | ||
| if (isToolbarHidden) { | ||
| return CoordinatorLayout.LayoutParams.WRAP_CONTENT | ||
| } | ||
|
|
||
| val resolvedSize = | ||
| when (screen.headerType) { | ||
| Screen.HeaderType.Medium -> MaterialR.attr.collapsingToolbarLayoutMediumSize.resolveAttribute(context) | ||
| Screen.HeaderType.Large -> MaterialR.attr.collapsingToolbarLayoutLargeSize.resolveAttribute(context) | ||
| else -> CoordinatorLayout.LayoutParams.WRAP_CONTENT | ||
| } | ||
|
|
||
| // For apps that don't support Material 3 it's possible that resolved attribute of | ||
| // given header type size will return -1. In such case we want to return fallback value of | ||
| // desired header type. | ||
| return when (screen.headerType) { | ||
| Screen.HeaderType.Medium -> if (resolvedSize != -1) resolvedSize else PixelUtil.toPixelFromDIP(112f).toInt() | ||
| Screen.HeaderType.Large -> if (resolvedSize != -1) resolvedSize else PixelUtil.toPixelFromDIP(152f).toInt() | ||
| else -> resolvedSize | ||
| } | ||
| } | ||
|
|
||
| private fun shouldShowSearchBar(): Boolean { | ||
| val config = screen.headerConfig | ||
| val numberOfSubViews = config?.configSubviewsCount ?: 0 | ||
|
|
@@ -229,6 +288,17 @@ class ScreenStackFragment : | |
| container.dismiss(this) | ||
| } | ||
|
|
||
| private fun Int.resolveAttribute(context: Context): Int { | ||
| val textSizeAttr = intArrayOf(this) | ||
| val indexOfAttrTextSize = 0 | ||
|
|
||
| val obtainedAttributesTa: TypedArray = context.obtainStyledAttributes(textSizeAttr) | ||
| val parsedAttribute = obtainedAttributesTa.getDimensionPixelSize(indexOfAttrTextSize, -1) | ||
|
|
||
| obtainedAttributesTa.recycle() | ||
| return parsedAttribute | ||
| } | ||
|
|
||
|
Comment on lines
+291
to
+301
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tried to just resolve attributes with TypedValue (just as in ScreenStackHeaderConfig) but unfortunately I was getting wrong results with it. That's why I've added a helper method for resolving such attributes with TypedArray. |
||
| private class ScreensCoordinatorLayout( | ||
| context: Context, | ||
| private val mFragment: ScreenFragment, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.