1
0
Fork 0
fenix/app/src/main/java/org/mozilla/fenix/components/toolbar/ToolbarIntegration.kt

110 lines
4.2 KiB
Kotlin
Raw Normal View History

2019-01-24 22:07:52 +01:00
/* 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.components.toolbar
import android.content.Context
import android.graphics.PorterDuff
import androidx.core.content.ContextCompat
import androidx.navigation.Navigation
import mozilla.components.browser.domains.autocomplete.DomainAutocompleteProvider
import mozilla.components.browser.session.SessionManager
import mozilla.components.browser.session.runWithSession
import mozilla.components.browser.toolbar.BrowserToolbar
import mozilla.components.concept.storage.HistoryStorage
import mozilla.components.feature.toolbar.ToolbarAutocompleteFeature
2019-04-01 17:47:25 +02:00
import mozilla.components.feature.toolbar.ToolbarFeature
import mozilla.components.feature.toolbar.ToolbarPresenter
2019-04-01 17:47:25 +02:00
import mozilla.components.lib.publicsuffixlist.PublicSuffixList
import mozilla.components.support.base.feature.LifecycleAwareFeature
2019-04-01 17:47:25 +02:00
import org.mozilla.fenix.DefaultThemeManager
import org.mozilla.fenix.R
import org.mozilla.fenix.browser.BrowserFragmentDirections
import org.mozilla.fenix.ext.components
class ToolbarIntegration(
context: Context,
toolbar: BrowserToolbar,
toolbarMenu: ToolbarMenu,
domainAutocompleteProvider: DomainAutocompleteProvider,
historyStorage: HistoryStorage,
sessionManager: SessionManager,
sessionId: String? = null,
isPrivate: Boolean
) : LifecycleAwareFeature {
2019-04-01 17:47:25 +02:00
private var renderStyle: ToolbarFeature.RenderStyle = ToolbarFeature.RenderStyle.UncoloredUrl
init {
toolbar.setMenuBuilder(toolbarMenu.menuBuilder)
toolbar.private = isPrivate
2019-04-05 23:33:02 +02:00
run {
sessionManager.runWithSession(sessionId) {
it.isCustomTabSession()
}.also { isCustomTab ->
2019-04-01 17:47:25 +02:00
if (isCustomTab) {
renderStyle = ToolbarFeature.RenderStyle.RegistrableDomain
return@run
}
if (isPrivate) {
val deleteIcon = context.getDrawable(R.drawable.ic_delete)
deleteIcon?.setColorFilter(
ContextCompat.getColor(
context,
DefaultThemeManager.resolveAttribute(R.attr.primaryText, context)
), PorterDuff.Mode.SRC_IN
)
deleteIcon?.let {
val deleteSessions = BrowserToolbar.Button(
deleteIcon,
context.getString(R.string.private_browsing_delete_session),
listener = {
context.components.useCases.tabsUseCases.removeAllTabsOfType.invoke(
private = true
)
Navigation.findNavController(toolbar)
.navigate(BrowserFragmentDirections.actionBrowserFragmentToHomeFragment())
}
)
toolbar.addNavigationAction(deleteSessions)
}
}
2019-04-05 23:33:02 +02:00
val tabsAction = TabCounterToolbarButton(
sessionManager,
{
2019-04-05 23:33:02 +02:00
Navigation.findNavController(toolbar)
.navigate(BrowserFragmentDirections.actionBrowserFragmentToHomeFragment())
},
isPrivate
2019-04-05 23:33:02 +02:00
)
toolbar.addBrowserAction(tabsAction)
}
}
ToolbarAutocompleteFeature(toolbar).apply {
addDomainProvider(domainAutocompleteProvider)
addHistoryStorageProvider(historyStorage)
}
}
private val toolbarPresenter: ToolbarPresenter = ToolbarPresenter(
toolbar,
context.components.core.sessionManager,
2019-04-01 17:47:25 +02:00
sessionId,
ToolbarFeature.UrlRenderConfiguration(PublicSuffixList(context),
DefaultThemeManager.resolveAttribute(R.attr.primaryText, context), renderStyle = renderStyle)
)
override fun start() {
toolbarPresenter.start()
}
override fun stop() {
toolbarPresenter.stop()
}
}