From 238f6e92c8f09a30104c62980809aad3af1bab4f Mon Sep 17 00:00:00 2001 From: David Perez Date: Thu, 9 Nov 2023 15:56:57 -0600 Subject: [PATCH] Update the 'BitwardenTopAppBar' to make the navigation icon optional (#232) --- .../platform/components/BitwardenTopAppBar.kt | 68 ++++++++++++++++--- 1 file changed, 57 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/BitwardenTopAppBar.kt b/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/BitwardenTopAppBar.kt index 32e633dce..34a6407c2 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/BitwardenTopAppBar.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/BitwardenTopAppBar.kt @@ -36,6 +36,35 @@ fun BitwardenTopAppBar( navigationIcon: Painter, navigationIconContentDescription: String, onNavigationIconClick: () -> Unit, + actions: @Composable RowScope.() -> Unit = { }, +) { + BitwardenTopAppBar( + title = title, + scrollBehavior = scrollBehavior, + navigationIcon = NavigationIcon( + navigationIcon = navigationIcon, + navigationIconContentDescription = navigationIconContentDescription, + onNavigationIconClick = onNavigationIconClick, + ), + actions = actions, + ) +} + +/** + * Represents a Bitwarden styled [TopAppBar] that assumes the following components: + * + * - an optional single navigation control in the upper-left defined by [navigationIcon]. + * - a [title] in the middle. + * - a [actions] lambda containing the set of actions (usually icons or similar) to display + * in the app bar's trailing side. This lambda extends [RowScope], allowing flexibility in + * defining the layout of the actions. + */ +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun BitwardenTopAppBar( + title: String, + scrollBehavior: TopAppBarScrollBehavior, + navigationIcon: NavigationIcon?, actions: @Composable RowScope.() -> Unit = {}, ) { TopAppBar( @@ -45,14 +74,16 @@ fun BitwardenTopAppBar( ), scrollBehavior = scrollBehavior, navigationIcon = { - IconButton( - onClick = { onNavigationIconClick() }, - ) { - Icon( - painter = navigationIcon, - contentDescription = navigationIconContentDescription, - tint = MaterialTheme.colorScheme.onSurface, - ) + navigationIcon?.let { + IconButton( + onClick = it.onNavigationIconClick, + ) { + Icon( + painter = it.navigationIcon, + contentDescription = it.navigationIconContentDescription, + tint = MaterialTheme.colorScheme.onSurface, + ) + } } }, title = { @@ -77,9 +108,24 @@ private fun BitwardenTopAppBar_preview() { .exitUntilCollapsedScrollBehavior( rememberTopAppBarState(), ), - navigationIcon = painterResource(id = R.drawable.ic_close), - navigationIconContentDescription = stringResource(id = R.string.close), - onNavigationIconClick = {}, + navigationIcon = NavigationIcon( + navigationIcon = painterResource(id = R.drawable.ic_close), + navigationIconContentDescription = stringResource(id = R.string.close), + onNavigationIconClick = { }, + ), ) } } + +/** + * Represents all data required to display a [navigationIcon]. + * + * @property navigationIcon The [Painter] displayed as part of the icon. + * @property navigationIconContentDescription The content description associated with the icon. + * @property onNavigationIconClick The click action that is invoked when the icon is tapped. + */ +data class NavigationIcon( + val navigationIcon: Painter, + val navigationIconContentDescription: String, + val onNavigationIconClick: () -> Unit, +)