2019-01-29 20:20:29 +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/. */
|
|
|
|
|
2019-02-13 00:51:30 +01:00
|
|
|
package org.mozilla.fenix.components.toolbar
|
2019-01-29 20:20:29 +01:00
|
|
|
|
2019-03-29 21:49:50 +01:00
|
|
|
import android.graphics.drawable.BitmapDrawable
|
2019-01-29 20:20:29 +01:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.ViewGroup
|
2019-03-29 21:49:50 +01:00
|
|
|
import android.widget.ImageView
|
2019-04-01 17:47:25 +02:00
|
|
|
import androidx.core.content.ContextCompat
|
2019-01-31 07:49:41 +01:00
|
|
|
import io.reactivex.Observable
|
|
|
|
import io.reactivex.Observer
|
2019-01-29 20:20:29 +01:00
|
|
|
import io.reactivex.functions.Consumer
|
2019-01-31 00:51:49 +01:00
|
|
|
import mozilla.components.browser.domains.autocomplete.ShippedDomainsProvider
|
2019-01-29 20:20:29 +01:00
|
|
|
import mozilla.components.browser.toolbar.BrowserToolbar
|
|
|
|
import mozilla.components.support.ktx.android.content.res.pxToDp
|
2019-03-29 21:49:50 +01:00
|
|
|
import org.jetbrains.anko.backgroundDrawable
|
2019-01-29 20:20:29 +01:00
|
|
|
import org.mozilla.fenix.R
|
2019-04-01 17:47:25 +02:00
|
|
|
import org.mozilla.fenix.customtabs.CustomTabToolbarMenu
|
2019-01-29 20:20:29 +01:00
|
|
|
import org.mozilla.fenix.ext.components
|
|
|
|
import org.mozilla.fenix.mvi.UIView
|
|
|
|
|
2019-01-31 07:49:41 +01:00
|
|
|
class ToolbarUIView(
|
2019-02-08 19:35:48 +01:00
|
|
|
sessionId: String?,
|
2019-02-15 18:31:03 +01:00
|
|
|
isPrivate: Boolean,
|
2019-01-31 07:49:41 +01:00
|
|
|
container: ViewGroup,
|
|
|
|
actionEmitter: Observer<SearchAction>,
|
2019-03-29 21:49:50 +01:00
|
|
|
changesObservable: Observable<SearchChange>,
|
|
|
|
private val engineIconView: ImageView? = null
|
2019-01-31 07:49:41 +01:00
|
|
|
) :
|
|
|
|
UIView<SearchState, SearchAction, SearchChange>(container, actionEmitter, changesObservable) {
|
2019-01-29 20:20:29 +01:00
|
|
|
|
2019-01-31 00:51:49 +01:00
|
|
|
val toolbarIntegration: ToolbarIntegration
|
2019-03-29 21:49:50 +01:00
|
|
|
var state: SearchState? = null
|
|
|
|
private set
|
2019-01-31 00:51:49 +01:00
|
|
|
|
2019-01-29 20:20:29 +01:00
|
|
|
override val view: BrowserToolbar = LayoutInflater.from(container.context)
|
|
|
|
.inflate(R.layout.component_search, container, true)
|
|
|
|
.findViewById(R.id.toolbar)
|
|
|
|
|
|
|
|
private val urlBackground = LayoutInflater.from(container.context)
|
2019-03-19 17:13:48 +01:00
|
|
|
.inflate(R.layout.layout_url_background, container, false)
|
2019-01-29 20:20:29 +01:00
|
|
|
|
|
|
|
init {
|
2019-05-02 21:32:04 +02:00
|
|
|
val sessionManager = view.context.components.core.sessionManager
|
|
|
|
val session = sessionId?.let { sessionManager.findSessionById(it) }
|
2019-05-10 01:06:12 +02:00
|
|
|
?: sessionManager.selectedSession
|
2019-04-01 17:47:25 +02:00
|
|
|
|
2019-01-29 20:20:29 +01:00
|
|
|
view.apply {
|
2019-05-02 22:08:49 +02:00
|
|
|
elevation = resources.pxToDp(TOOLBAR_ELEVATION).toFloat()
|
|
|
|
|
2019-02-17 00:28:38 +01:00
|
|
|
setOnUrlCommitListener {
|
2019-03-29 21:49:50 +01:00
|
|
|
actionEmitter.onNext(SearchAction.UrlCommitted(it, sessionId, state?.engine))
|
2019-05-02 22:08:49 +02:00
|
|
|
false
|
2019-02-17 00:28:38 +01:00
|
|
|
}
|
2019-02-05 00:41:26 +01:00
|
|
|
onUrlClicked = {
|
2019-04-09 23:15:18 +02:00
|
|
|
actionEmitter.onNext(SearchAction.ToolbarClicked)
|
2019-02-05 00:41:26 +01:00
|
|
|
false
|
|
|
|
}
|
2019-01-29 20:20:29 +01:00
|
|
|
|
|
|
|
browserActionMargin = resources.pxToDp(browserActionMarginDp)
|
2019-04-01 17:47:25 +02:00
|
|
|
|
|
|
|
val isCustomTabSession = (session?.isCustomTabSession() == true)
|
|
|
|
|
2019-05-02 22:08:49 +02:00
|
|
|
urlBoxView = if (isCustomTabSession) {
|
|
|
|
null
|
|
|
|
} else urlBackground
|
|
|
|
progressBarGravity = if (isCustomTabSession) {
|
|
|
|
PROGRESS_BOTTOM
|
|
|
|
} else PROGRESS_TOP
|
2019-04-01 17:47:25 +02:00
|
|
|
|
|
|
|
textColor = ContextCompat.getColor(context, R.color.photonGrey30)
|
2019-01-29 20:20:29 +01:00
|
|
|
|
|
|
|
hint = context.getString(R.string.search_hint)
|
2019-01-31 00:51:49 +01:00
|
|
|
|
|
|
|
setOnEditListener(object : mozilla.components.concept.toolbar.Toolbar.OnEditListener {
|
2019-03-13 17:23:55 +01:00
|
|
|
override fun onCancelEditing(): Boolean {
|
2019-03-13 19:12:58 +01:00
|
|
|
actionEmitter.onNext(SearchAction.EditingCanceled)
|
|
|
|
return false
|
2019-03-13 17:23:55 +01:00
|
|
|
}
|
2019-05-02 22:08:49 +02:00
|
|
|
|
2019-01-31 00:51:49 +01:00
|
|
|
override fun onTextChanged(text: String) {
|
2019-02-08 19:35:48 +01:00
|
|
|
url = text
|
2019-01-31 07:49:41 +01:00
|
|
|
actionEmitter.onNext(SearchAction.TextChanged(text))
|
2019-01-31 00:51:49 +01:00
|
|
|
}
|
|
|
|
})
|
2019-04-09 23:15:18 +02:00
|
|
|
|
|
|
|
setOnUrlLongClickListener {
|
|
|
|
actionEmitter.onNext(SearchAction.ToolbarLongClicked)
|
|
|
|
true
|
|
|
|
}
|
2019-01-29 20:20:29 +01:00
|
|
|
}
|
|
|
|
|
2019-01-31 00:51:49 +01:00
|
|
|
with(view.context) {
|
2019-04-01 17:47:25 +02:00
|
|
|
val isCustom = session?.isCustomTabSession() ?: false
|
2019-02-17 20:10:29 +01:00
|
|
|
|
2019-04-01 17:47:25 +02:00
|
|
|
val menuToolbar = if (isCustom) {
|
2019-05-02 21:32:04 +02:00
|
|
|
CustomTabToolbarMenu(
|
|
|
|
this,
|
|
|
|
sessionManager,
|
|
|
|
sessionId,
|
2019-04-01 17:47:25 +02:00
|
|
|
onItemTapped = { actionEmitter.onNext(SearchAction.ToolbarMenuItemTapped(it)) }
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
DefaultToolbarMenu(this,
|
2019-03-13 17:47:23 +01:00
|
|
|
sessionId = sessionId,
|
2019-02-17 20:10:29 +01:00
|
|
|
requestDesktopStateProvider = { session?.desktopMode ?: false },
|
2019-02-08 00:26:18 +01:00
|
|
|
onItemTapped = { actionEmitter.onNext(SearchAction.ToolbarMenuItemTapped(it)) }
|
2019-04-01 17:47:25 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
toolbarIntegration = ToolbarIntegration(
|
|
|
|
this,
|
|
|
|
view,
|
|
|
|
menuToolbar,
|
2019-01-31 00:51:49 +01:00
|
|
|
ShippedDomainsProvider().also { it.initialize(this) },
|
2019-01-31 18:58:52 +01:00
|
|
|
components.core.historyStorage,
|
2019-02-06 23:15:38 +01:00
|
|
|
components.core.sessionManager,
|
2019-02-15 18:31:03 +01:00
|
|
|
sessionId,
|
|
|
|
isPrivate
|
2019-01-31 00:51:49 +01:00
|
|
|
)
|
2019-01-29 20:20:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun updateView() = Consumer<SearchState> {
|
2019-04-25 21:31:17 +02:00
|
|
|
var newState = it
|
|
|
|
if (shouldUpdateEngineIcon(newState)) {
|
|
|
|
updateEngineIcon(newState)
|
2019-03-29 21:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldClearSearchURL(it)) {
|
2019-04-25 21:31:17 +02:00
|
|
|
newState = SearchState("", "", it.isEditing, it.engine, it.focused)
|
2019-03-29 21:49:50 +01:00
|
|
|
}
|
|
|
|
|
2019-04-25 21:31:17 +02:00
|
|
|
// Need to set edit mode if the url value was cleared
|
|
|
|
if (newState.focused || shouldClearSearchURL(it) || shouldUpdateEditingState(newState)) {
|
|
|
|
updateEditingState(newState)
|
2019-03-29 21:49:50 +01:00
|
|
|
}
|
|
|
|
|
2019-04-25 21:31:17 +02:00
|
|
|
if (!newState.focused) {
|
|
|
|
view.clearFocus()
|
|
|
|
}
|
|
|
|
|
|
|
|
state = newState
|
2019-03-29 21:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun shouldUpdateEngineIcon(newState: SearchState): Boolean {
|
|
|
|
return newState.isEditing && (engineDidChange(newState) || state == null)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateEngineIcon(newState: SearchState) {
|
|
|
|
with(view.context) {
|
|
|
|
val defaultEngineIcon = components.search.searchEngineManager.defaultSearchEngine?.icon
|
|
|
|
val searchIcon = newState.engine?.icon ?: defaultEngineIcon
|
2019-05-06 23:32:08 +02:00
|
|
|
val draw = BitmapDrawable(resources, searchIcon)
|
2019-03-29 21:49:50 +01:00
|
|
|
val iconSize =
|
|
|
|
containerView?.context!!.resources.getDimension(R.dimen.preference_icon_drawable_size).toInt()
|
|
|
|
draw.setBounds(0, 0, iconSize, iconSize)
|
|
|
|
engineIconView?.backgroundDrawable = draw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun shouldClearSearchURL(newState: SearchState): Boolean {
|
2019-04-25 21:31:17 +02:00
|
|
|
with(view.context) {
|
|
|
|
val defaultEngine = this
|
|
|
|
.components
|
|
|
|
.search
|
|
|
|
.searchEngineManager
|
|
|
|
.defaultSearchEngine
|
|
|
|
|
|
|
|
return (newState.engine != null && newState.engine != defaultEngine) ||
|
|
|
|
(state?.engine != null && state?.engine != defaultEngine)
|
|
|
|
}
|
2019-03-29 21:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun shouldUpdateEditingState(newState: SearchState): Boolean {
|
2019-04-25 21:31:17 +02:00
|
|
|
return !engineDidChange(newState) && (state?.isEditing != newState.isEditing)
|
2019-03-29 21:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateEditingState(newState: SearchState) {
|
|
|
|
if (newState.isEditing) {
|
2019-04-24 20:28:31 +02:00
|
|
|
view.setSearchTerms(newState.searchTerm)
|
2019-03-29 21:49:50 +01:00
|
|
|
view.url = newState.query
|
2019-02-04 23:23:15 +01:00
|
|
|
view.editMode()
|
|
|
|
} else {
|
|
|
|
view.displayMode()
|
|
|
|
}
|
2019-01-29 20:20:29 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 21:49:50 +01:00
|
|
|
private fun engineDidChange(newState: SearchState): Boolean {
|
|
|
|
return newState.engine != state?.engine
|
|
|
|
}
|
|
|
|
|
2019-01-29 20:20:29 +01:00
|
|
|
companion object {
|
2019-05-02 22:08:49 +02:00
|
|
|
private const val TOOLBAR_ELEVATION = 16
|
2019-04-16 18:34:44 +02:00
|
|
|
private const val PROGRESS_BOTTOM = 0
|
|
|
|
private const val PROGRESS_TOP = 1
|
2019-01-29 20:20:29 +01:00
|
|
|
const val browserActionMarginDp = 8
|
|
|
|
}
|
|
|
|
}
|