From 7bcb68969fe0277ce266f0518aac58da03d327eb Mon Sep 17 00:00:00 2001 From: luisoliveirazup <59032818+luisoliveirazup@users.noreply.github.com> Date: Fri, 23 Apr 2021 16:20:43 -0300 Subject: [PATCH] fix: toolbar set title in beagle android (#1513) * fix: set title toolbar beagle android * fix: rename mtests method --- .../beagle/android/utils/ToolbarManager.kt | 115 ++++++++++++------ .../android/utils/ToolbarManagerTest.kt | 62 +++++++++- 2 files changed, 137 insertions(+), 40 deletions(-) diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/utils/ToolbarManager.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/utils/ToolbarManager.kt index 9559388a2a..1deb96cccd 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/utils/ToolbarManager.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/utils/ToolbarManager.kt @@ -86,56 +86,95 @@ internal class ToolbarManager(private val toolbarTextManager: ToolbarTextManager (rootView.getContext() as BeagleActivity).getToolbar().apply { visibility = View.VISIBLE menu.clear() - configToolbarStyle(rootView.getContext(), this, navigationBar) + setAttributeToolbar(rootView.getContext(), this, navigationBar) navigationBar.navigationBarItems?.let { items -> configToolbarItems(rootView, this, items, container, screenComponent) } } } + private fun setAttributeToolbar( + context: Context, + toolbar: Toolbar, + navigationBar: NavigationBar + ) { + val designSystem = BeagleEnvironment.beagleSdk.designSystem + val toolbarStyle = navigationBar.styleId?.let { designSystem?.toolbarStyle(it) } + if (toolbarStyle == null) { + setTitle(context, toolbar, navigationBar) + } else { + configToolbarStyle(context, toolbar, navigationBar, toolbarStyle) + } + } + private fun configToolbarStyle( context: Context, toolbar: Toolbar, navigationBar: NavigationBar, + toolbarStyle: Int ) { - val designSystem = BeagleEnvironment.beagleSdk.designSystem - val style = navigationBar.styleId ?: "" - designSystem?.toolbarStyle(style)?.let { toolbarStyle -> - val typedArray = context.obtainStyledAttributes( - toolbarStyle, - R.styleable.BeagleToolbarStyle - ) - if (navigationBar.showBackButton) { - typedArray.getDrawable(R.styleable.BeagleToolbarStyle_navigationIcon)?.let { - toolbar.navigationIcon = it - } - } else { - toolbar.navigationIcon = null - } - val textAppearance = typedArray.getResourceId( - R.styleable.BeagleToolbarStyle_titleTextAppearance, 0 - ) - removePreviousToolbarTitle(toolbar) - if (typedArray.getBoolean(R.styleable.BeagleToolbarStyle_centerTitle, false)) { - val titleTextView = toolbarTextManager.generateTitle(context, navigationBar, textAppearance) - toolbar.addView(titleTextView) - toolbarTextManager.centerTitle(toolbar, titleTextView) - toolbar.title = "title" - toolbar.contentInsetStartWithNavigation = CONTENT_INSET_ZERO - toolbar.setContentInsetsAbsolute(CONTENT_INSET_LEFT_ZERO, CONTENT_INSET_RIGHT_ZERO) - } else { - toolbar.title = navigationBar.title - if (textAppearance != 0) { - toolbar.setTitleTextAppearance(context, textAppearance) - } - } - val backgroundColor = typedArray.getColor( - R.styleable.BeagleToolbarStyle_backgroundColor, 0 - ) - if (backgroundColor != 0) { - toolbar.setBackgroundColor(backgroundColor) + val typedArray = context.obtainStyledAttributes( + toolbarStyle, + R.styleable.BeagleToolbarStyle + ) + if (navigationBar.showBackButton) { + typedArray.getDrawable(R.styleable.BeagleToolbarStyle_navigationIcon)?.let { + toolbar.navigationIcon = it } - typedArray.recycle() + } else { + toolbar.navigationIcon = null + } + val textAppearance = typedArray.getResourceId( + R.styleable.BeagleToolbarStyle_titleTextAppearance, 0 + ) + val isCenterTitle = typedArray.getBoolean(R.styleable.BeagleToolbarStyle_centerTitle, false) + setTitle(context, toolbar, navigationBar, textAppearance, isCenterTitle) + val backgroundColor = typedArray.getColor( + R.styleable.BeagleToolbarStyle_backgroundColor, 0 + ) + if (backgroundColor != 0) { + toolbar.setBackgroundColor(backgroundColor) + } + typedArray.recycle() + } + + private fun setTitle( + context: Context, + toolbar: Toolbar, + navigationBar: NavigationBar, + textAppearance: Int = 0, + isCenterTitle: Boolean = false + ) { + removePreviousToolbarTitle(toolbar) + if (isCenterTitle) { + setCenterTitle(context, toolbar, navigationBar, textAppearance) + } else { + setDefaultTitle(context, toolbar, navigationBar, textAppearance) + } + } + + private fun setCenterTitle( + context: Context, + toolbar: Toolbar, + navigationBar: NavigationBar, + textAppearance: Int = 0, + ) { + val titleTextView = toolbarTextManager.generateTitle(context, navigationBar, textAppearance) + toolbar.addView(titleTextView) + toolbarTextManager.centerTitle(toolbar, titleTextView) + toolbar.contentInsetStartWithNavigation = CONTENT_INSET_ZERO + toolbar.setContentInsetsAbsolute(CONTENT_INSET_LEFT_ZERO, CONTENT_INSET_RIGHT_ZERO) + } + + private fun setDefaultTitle( + context: Context, + toolbar: Toolbar, + navigationBar: NavigationBar, + textAppearance: Int = 0, + ) { + toolbar.title = navigationBar.title + if (textAppearance != 0) { + toolbar.setTitleTextAppearance(context, textAppearance) } } diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/utils/ToolbarManagerTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/utils/ToolbarManagerTest.kt index 9b89c47b50..734e76d5ac 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/utils/ToolbarManagerTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/utils/ToolbarManagerTest.kt @@ -253,8 +253,8 @@ internal class ToolbarManagerTest : BaseTest() { inner class ConfigureToolbar { @Test - @DisplayName("Then should check the title settings") - fun verifyCallTheGenerateTitle() { + @DisplayName("Then should check the center title settings") + fun verifyCallTheGenerateCenterTitle() { // GIVEN every { toolbar.findViewById(any()) } returns textView every { navigationBar.title } returns title @@ -282,5 +282,63 @@ internal class ToolbarManagerTest : BaseTest() { toolbarTextManagerMock.centerTitle(toolbar, textViewMock) } } + + @Test + @DisplayName("Then you should check if the title and the style have been applied") + fun shouldToolbarWithTitleAndStyleWhenCallConfigureToolbarThenTheToolbarMustHaveATitleAndStyle() { + // GIVEN + toolbar = mockk() + val beagleActivityMock = mockk(relaxed = true) + every { (rootView.getContext() as BeagleActivity) } returns beagleActivityMock + val slotStyle = slot() + every { beagleActivityMock.obtainStyledAttributes(capture(slotStyle),R.styleable.BeagleToolbarStyle) } returns mockk(relaxed = true) + every { toolbar.title } returns title + every { beagleActivityMock.getToolbar() } returns toolbar + every { navigationBar.styleId } returns style + every { beagleSdk.designSystem } returns designSystemMock + every { designSystemMock.toolbarStyle(style) } returns styleInt + every { toolbar.visibility = View.VISIBLE } just runs + every { toolbar.menu } returns menu + every { toolbar.navigationIcon = null } just runs + every { toolbar.findViewById(any()) } returns textView + every { toolbar.removeView(textView) } just runs + every { toolbar.title = title } just runs + every { toolbar.setTitleTextAppearance(beagleActivityMock, styleInt) } just runs + every { navigationBar.title } returns title + every { + typedArray.getResourceId(R.styleable.BeagleToolbarStyle_titleTextAppearance, 0) + } returns textAppearanceMock + + // WHEN + toolbarManager.configureToolbar(rootView, navigationBar, beagleFlexView, screenComponent) + + // THEN + assertEquals(title, toolbar.title.toString()) + assertEquals(styleInt, slotStyle.captured) + } + + @Test + @DisplayName("Then you should check if the title has been applied") + fun shouldToolbarWithTitleWhenCallConfigureToolbarThenTheToolbarMustHaveATitle() { + // GIVEN + toolbar = mockk() + val beagleActivityMock = mockk(relaxed = true) + every { (rootView.getContext() as BeagleActivity) } returns beagleActivityMock + every { toolbar.title } returns title + every { beagleActivityMock.getToolbar() } returns toolbar + every { toolbar.visibility = View.VISIBLE } just runs + every { toolbar.menu } returns menu + every { toolbar.navigationIcon = null } just runs + every { toolbar.findViewById(any()) } returns textView + every { toolbar.removeView(textView) } just runs + every { toolbar.title = title } just runs + every { navigationBar.title } returns title + + // WHEN + toolbarManager.configureToolbar(rootView, navigationBar, beagleFlexView, screenComponent) + + // THEN + assertEquals(title, toolbar.title.toString()) + } } } \ No newline at end of file