Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
fix: toolbar set title in beagle android (#1513)
Browse files Browse the repository at this point in the history
* fix: set title toolbar beagle android

* fix: rename mtests method
  • Loading branch information
luisoliveirazup authored Apr 23, 2021
1 parent c33371e commit 7bcb689
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextView>(any()) } returns textView
every { navigationBar.title } returns title
Expand Down Expand Up @@ -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<BeagleActivity>(relaxed = true)
every { (rootView.getContext() as BeagleActivity) } returns beagleActivityMock
val slotStyle = slot<Int>()
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<TextView>(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<BeagleActivity>(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<TextView>(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())
}
}
}

0 comments on commit 7bcb689

Please sign in to comment.