From 5b395f9999bfa8d358d82b36935e02b292b838a0 Mon Sep 17 00:00:00 2001 From: Tiger Oakes Date: Tue, 8 Sep 2020 13:55:10 -0700 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/14376: Reverse items with bottom toolbar (https://github.com/mozilla-mobile/fenix/pull/14451) --- .../components/toolbar/TabCounterMenu.kt | 37 +++++++++++++++---- .../toolbar/TabCounterToolbarButton.kt | 3 +- .../components/toolbar/TabCounterMenuTest.kt | 4 +- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounterMenu.kt b/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounterMenu.kt index 6076dbc54e..6d6bebe5a7 100644 --- a/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounterMenu.kt +++ b/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounterMenu.kt @@ -81,20 +81,41 @@ class TabCounterMenu( } @VisibleForTesting - internal fun menuItems(showOnly: BrowsingMode?): List { + internal fun menuItems(showOnly: BrowsingMode): List { return when (showOnly) { BrowsingMode.Normal -> listOf(newTabItem) BrowsingMode.Private -> listOf(newPrivateTabItem) - null -> listOf( - newTabItem, - newPrivateTabItem, - DividerMenuCandidate(), - closeTabItem - ) } } - fun updateMenu(showOnly: BrowsingMode? = null) { + @VisibleForTesting + internal fun menuItems(toolbarPosition: ToolbarPosition): List { + val items = listOf( + newTabItem, + newPrivateTabItem, + DividerMenuCandidate(), + closeTabItem + ) + + return when (toolbarPosition) { + ToolbarPosition.BOTTOM -> items.reversed() + ToolbarPosition.TOP -> items + } + } + + /** + * Update the displayed menu items. + * @param showOnly Show only the new tab item corresponding to the given [BrowsingMode]. + */ + fun updateMenu(showOnly: BrowsingMode) { menuController.submitList(menuItems(showOnly)) } + + /** + * Update the displayed menu items. + * @param toolbarPosition Return a list that is ordered based on the given [ToolbarPosition]. + */ + fun updateMenu(toolbarPosition: ToolbarPosition) { + menuController.submitList(menuItems(toolbarPosition)) + } } diff --git a/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounterToolbarButton.kt b/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounterToolbarButton.kt index 0b743e744d..118c062ff6 100644 --- a/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounterToolbarButton.kt +++ b/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounterToolbarButton.kt @@ -34,6 +34,7 @@ class TabCounterToolbarButton( override fun createView(parent: ViewGroup): View { val store = parent.context.components.core.store val metrics = parent.context.components.analytics.metrics + val settings = parent.context.components.settings store.flowScoped(lifecycleOwner) { flow -> flow.map { state -> state.getNormalOrPrivateTabs(isPrivate).size } @@ -42,7 +43,7 @@ class TabCounterToolbarButton( } val menu = TabCounterMenu(parent.context, metrics, onItemTapped) - menu.updateMenu() + menu.updateMenu(settings.toolbarPosition) val view = TabCounter(parent.context).apply { reference = WeakReference(this) diff --git a/app/src/test/java/org/mozilla/fenix/components/toolbar/TabCounterMenuTest.kt b/app/src/test/java/org/mozilla/fenix/components/toolbar/TabCounterMenuTest.kt index ae69260903..a85c533b22 100644 --- a/app/src/test/java/org/mozilla/fenix/components/toolbar/TabCounterMenuTest.kt +++ b/app/src/test/java/org/mozilla/fenix/components/toolbar/TabCounterMenuTest.kt @@ -40,7 +40,7 @@ class TabCounterMenuTest { @Test fun `all items use primary text color styling`() { - val items = menu.menuItems(showOnly = null) + val items = menu.menuItems(ToolbarPosition.BOTTOM) assertEquals(4, items.size) val textItems = items.mapNotNull { it as? TextMenuCandidate } @@ -85,7 +85,7 @@ class TabCounterMenuTest { @Test fun `return two new tab items and a close button`() { - val (newTab, newPrivateTab, divider, closeTab) = menu.menuItems(showOnly = null) + val (newTab, newPrivateTab, divider, closeTab) = menu.menuItems(ToolbarPosition.TOP) assertEquals("New tab", (newTab as TextMenuCandidate).text) assertEquals("New private tab", (newPrivateTab as TextMenuCandidate).text)