Skip to content

Commit

Permalink
For mozilla-mobile#6907 - Adds Media.State to concept-tabstray
Browse files Browse the repository at this point in the history
- Maps MediaState from BrowserState when creating the Tabs list
  • Loading branch information
boek committed May 11, 2020
1 parent 8c98a4f commit f7f34be
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 16 deletions.
1 change: 1 addition & 0 deletions components/browser/tabstray/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ android {

dependencies {
api project(':concept-tabstray')

implementation project(':ui-icons')
implementation project(':ui-colors')
implementation project(':support-base')
Expand Down
3 changes: 2 additions & 1 deletion components/concept/tabstray/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ android {
}

dependencies {
implementation project(':support-base')
api project(':concept-engine')

implementation project(':support-base')
implementation Dependencies.kotlin_stdlib
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package mozilla.components.concept.tabstray

import android.graphics.Bitmap
import mozilla.components.concept.engine.media.Media

/**
* Data class representing a tab to be displayed in a [TabsTray].
Expand All @@ -14,11 +15,13 @@ import android.graphics.Bitmap
* @property title Current title of the tab (or an empty [String]]).
* @property icon Current icon of the tab (or null)
* @property thumbnail Current thumbnail of the tab (or null)
* @property mediaState Current media state for the tab (or null)
*/
data class Tab(
val id: String,
val url: String,
val title: String = "",
val icon: Bitmap? = null,
val thumbnail: Bitmap? = null
val thumbnail: Bitmap? = null,
val mediaState: Media.State? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@
package mozilla.components.feature.tabs.ext

import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.state.MediaState
import mozilla.components.browser.state.state.TabSessionState
import mozilla.components.concept.tabstray.Tabs

private fun BrowserState.mediaStateForTab(tab: TabSessionState): MediaState.State =
if (media.aggregate.activeTabId == tab.id) {
media.aggregate.state
} else {
MediaState.State.NONE
}

internal fun BrowserState.toTabs(
tabsFilter: (TabSessionState) -> Boolean = { true }
) = Tabs(
list = tabs
.filter(tabsFilter)
.map { it.toTab() },
.map { it.toTab(mediaStateForTab(it)) },
selectedIndex = tabs.indexOfFirst { it.id == selectedTabId }
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@

package mozilla.components.feature.tabs.ext

import mozilla.components.browser.state.state.MediaState
import mozilla.components.browser.state.state.TabSessionState
import mozilla.components.concept.engine.media.Media
import mozilla.components.concept.tabstray.Tab

internal fun TabSessionState.toTab() = Tab(
internal fun TabSessionState.toTab(mediaState: MediaState.State) = Tab(
id,
content.url,
content.title,
content.icon,
content.thumbnail
content.thumbnail,
when (mediaState) {
MediaState.State.PLAYING -> Media.State.PLAYING
MediaState.State.PAUSED -> Media.State.PAUSED
else -> null
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,21 @@ class TabsTrayPresenter(
private val closeTabsTray: () -> Unit
) {
private var tabs: Tabs? = null
private var mediaScope: CoroutineScope? = null
private var tabScope: CoroutineScope? = null

fun start() {
tabScope = store.flowScoped { flow -> collect(flow) }
mediaScope = store.flowScoped {
it
.ifChanged { state -> state.media.aggregate }
.collect { this.tabs?.also(tabsTray::updateTabs) }
}
}

fun stop() {
tabScope?.cancel()
mediaScope?.cancel()
}

private suspend fun collect(flow: Flow<BrowserState>) {
flow.map { state -> state.toTabs(tabsFilter) }
flow.map { state ->
val tabs = state.toTabs(tabsFilter)
tabs
}
.ifChanged()
.collect { tabs ->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.tabstray.Tabs
import mozilla.components.concept.tabstray.TabsTray
import mozilla.components.feature.tabs.ext.toTabs
import mozilla.components.support.test.any
import mozilla.components.support.test.ext.joinBlocking
import mozilla.components.support.test.mock
import org.junit.After
Expand Down Expand Up @@ -239,10 +240,15 @@ class TabsTrayPresenterTest {
presenter.start()
testDispatcher.advanceUntilIdle()

store.dispatch(MediaAction.UpdateMediaAggregateAction(store.state.media.aggregate.copy(state = MediaState.State.PLAYING))).joinBlocking()
store.dispatch(
MediaAction.UpdateMediaAggregateAction(
store.state.media.aggregate.copy(activeTabId = "a", state = MediaState.State.PLAYING)
)
).joinBlocking()

testDispatcher.advanceUntilIdle()

verify(tabsTray, times(3)).updateTabs(store.state.toTabs())
verify(tabsTray, times(2)).updateTabs(any())
}

@Test
Expand Down
5 changes: 4 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ permalink: /changelog/
* ⚠️ **This is a breaking change**: Added optional `crashReporting` param to [PictureInPictureFeature] so we can record caught exceptions.

* **feature-tabs**
* Fixed issue [#6907](https://github.com/mozilla-mobile/android-components/issues/6907). Dispatches tabsTray update when MediaState changes.
* Fixed issue [#6907](https://github.com/mozilla-mobile/android-components/issues/6907). Uses MediaState when mapping BrowserState to tabs.

* **concept-tabstray**
* For issue [#6907](https://github.com/mozilla-mobile/android-components/issues/6907). Adds `Media.State` to `Tab`

* **feature-downloads**
* Fixed issue [#6881](https://github.com/mozilla-mobile/android-components/issues/6881).
Expand Down

0 comments on commit f7f34be

Please sign in to comment.