Session.kt 1.66 KB
Newer Older
1
2
3
4
5
6
7
8
/* 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 org.mozilla.fenix.ext

import android.content.Context
import mozilla.components.browser.session.Session
9
import mozilla.components.browser.state.selector.findTab
10
11
import mozilla.components.browser.state.state.MediaState
import mozilla.components.browser.state.store.BrowserStore
12
import mozilla.components.lib.publicsuffixlist.PublicSuffixList
13
import org.mozilla.fenix.home.Tab
14

15
16
17
18
19
20
fun Session.toTab(context: Context, selected: Boolean? = null): Tab =
    this.toTab(
        context.components.core.store,
        context.components.publicSuffixList,
        selected
    )
21

22
fun Session.toTab(store: BrowserStore, publicSuffixList: PublicSuffixList, selected: Boolean? = null): Tab {
23
    val url = store.state.findTab(this.id)?.readerState?.activeUrl ?: this.url
24
    return Tab(
Severin Rudie's avatar
Severin Rudie committed
25
        sessionId = this.id,
26
27
        url = url,
        hostname = url.toShortUrl(publicSuffixList),
Severin Rudie's avatar
Severin Rudie committed
28
29
        title = this.title,
        selected = selected,
30
31
        icon = this.icon,
        mediaState = getMediaStateForSession(store, this)
Sawyer Blatz's avatar
Sawyer Blatz committed
32
    )
33
}
34
35
36
37
38
39
40
41
42
43

private fun getMediaStateForSession(store: BrowserStore, session: Session): MediaState.State {
    // For now we are looking up the media state for this session in the BrowserStore. Eventually
    // we will migrate away from Session(Manager) and can use BrowserStore and BrowserState directly.
    return if (store.state.media.aggregate.activeTabId == session.id) {
        store.state.media.aggregate.state
    } else {
        MediaState.State.NONE
    }
}