Unverified Commit e589d307 authored by Jonathan Almeida's avatar Jonathan Almeida
Browse files

Close #8115: Add onTabsUpdated to TabsTray.Observer

parent c117eb29
......@@ -54,6 +54,8 @@ open class TabsAdapter(
override fun updateTabs(tabs: Tabs) {
this.tabs = tabs
notifyObservers { onTabsUpdated() }
}
override fun onTabsInserted(position: Int, count: Int) = notifyItemRangeInserted(position, count)
......
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package mozilla.components.browser.tabstray.ext
import mozilla.components.browser.tabstray.TabsAdapter
import mozilla.components.concept.tabstray.Tab
import mozilla.components.concept.tabstray.TabsTray
/**
* Performs the given action when the [TabsTray.Observer.onTabsUpdated] is invoked.
*
* The action will only be invoked once and then removed.
*/
inline fun TabsAdapter.doOnTabsUpdated(crossinline action: () -> Unit) {
register(object : TabsTray.Observer {
override fun onTabsUpdated() {
unregister(this)
action()
}
override fun onTabSelected(tab: Tab) = Unit
override fun onTabClosed(tab: Tab) = Unit
})
}
......@@ -114,4 +114,25 @@ class TabsAdapterTest {
adapter.onTabsChanged(42, 78)
verify(adapter).notifyItemRangeChanged(42, 78)
}
@Test
fun `tabs updated notifies observers`() {
val adapter = TabsAdapter()
val observer: TabsTray.Observer = mock()
adapter.register(observer)
adapter.updateTabs(
Tabs(
list = listOf(
Tab("A", "https://www.mozilla.org"),
Tab("B", "https://www.firefox.com"),
Tab("C", "https://getpocket.com")
),
selectedIndex = 0
)
)
verify(observer).onTabsUpdated()
}
}
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package mozilla.components.browser.tabstray.ext
import mozilla.components.browser.tabstray.TabsAdapter
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Test
class TabsAdapterKtTest {
@Test
fun `doOnTabsUpdated is invoked once`() {
val adapter = TabsAdapter()
var invokedCount = 0
adapter.doOnTabsUpdated {
invokedCount++
}
assertEquals(0, invokedCount)
adapter.notifyObservers { onTabsUpdated() }
assertEquals(1, invokedCount)
adapter.notifyObservers { onTabsUpdated() }
assertEquals(1, invokedCount)
assertFalse(adapter.isObserved())
}
}
......@@ -15,6 +15,11 @@ interface TabsTray : Observable<TabsTray.Observer> {
* Interface to be implemented by classes that want to observe a tabs tray.
*/
interface Observer {
/**
* One or many tabs have been added or removed.
*/
fun onTabsUpdated() = Unit
/**
* A new tab has been selected.
*/
......
......@@ -12,6 +12,11 @@ permalink: /changelog/
* [Gecko](https://github.com/mozilla-mobile/android-components/blob/master/buildSrc/src/main/java/Gecko.kt)
* [Configuration](https://github.com/mozilla-mobile/android-components/blob/master/buildSrc/src/main/java/Config.kt)
* **concept-tabstray**
* Added `onTabsUpdated` to `TabsTray.Observer` for notifying observers when one or more tabs have been added/removed.
* **browser-tabstray**
* Added the convenience function `TabsAdapter.doOnTabsUpdated` for performing actions only once when the tabs are updated.
# 55.0.0
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment