* Closes #875: Adds shortcuts * Refactor and clean up * Remove TODO * Removes local * Fix nits * Refactors to add ShortcutEngineManagernightly-build-test
parent
4f67b7a26c
commit
36af5107c6
@ -0,0 +1,100 @@
|
|||||||
|
package org.mozilla.fenix.search.awesomebar
|
||||||
|
|
||||||
|
/* 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/. */
|
||||||
|
|
||||||
|
import android.view.View
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.navigation.Navigation
|
||||||
|
import io.reactivex.Observer
|
||||||
|
import kotlinx.android.synthetic.main.fragment_search.*
|
||||||
|
import mozilla.components.browser.search.SearchEngine
|
||||||
|
import org.jetbrains.anko.textColor
|
||||||
|
import org.mozilla.fenix.DefaultThemeManager
|
||||||
|
import org.mozilla.fenix.R
|
||||||
|
import org.mozilla.fenix.search.SearchFragmentDirections
|
||||||
|
|
||||||
|
class ShortcutEngineManager(
|
||||||
|
private val awesomeBarUIView: AwesomeBarUIView,
|
||||||
|
private val actionEmitter: Observer<AwesomeBarAction>,
|
||||||
|
private val setShortcutEngine: (newEngine: SearchEngine) -> Unit,
|
||||||
|
private val showSuggestionProviders: () -> Unit,
|
||||||
|
private val showSearchSuggestionProvider: () -> Unit
|
||||||
|
) {
|
||||||
|
|
||||||
|
var shortcutsEnginePickerProvider: ShortcutsSuggestionProvider? = null
|
||||||
|
val context = awesomeBarUIView.containerView?.context!!
|
||||||
|
|
||||||
|
fun updateSelectedEngineIfNecessary(newState: AwesomeBarState) {
|
||||||
|
if (engineDidChange(newState)) {
|
||||||
|
newState.suggestionEngine?.let { newEngine ->
|
||||||
|
setShortcutEngine(newEngine)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateEnginePickerVisibilityIfNecessary(newState: AwesomeBarState) {
|
||||||
|
if (shouldUpdateShortcutEnginePickerVisibility(newState)) {
|
||||||
|
if (newState.showShortcutEnginePicker) {
|
||||||
|
showShortcutEnginePicker()
|
||||||
|
updateSearchWithVisibility(true)
|
||||||
|
} else {
|
||||||
|
hideShortcutEnginePicker()
|
||||||
|
updateSearchWithVisibility(false)
|
||||||
|
newState.suggestionEngine?.also { showSearchSuggestionProvider() } ?: showSuggestionProviders()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun selectShortcutEngine(engine: SearchEngine) {
|
||||||
|
actionEmitter.onNext(AwesomeBarAction.SearchShortcutEngineSelected(engine))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun selectShortcutEngineSettings() {
|
||||||
|
val directions = SearchFragmentDirections.actionSearchFragmentToSearchEngineFragment()
|
||||||
|
Navigation.findNavController(awesomeBarUIView.view).navigate(directions)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun engineDidChange(newState: AwesomeBarState): Boolean {
|
||||||
|
return awesomeBarUIView.state?.suggestionEngine != newState.suggestionEngine
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun shouldUpdateShortcutEnginePickerVisibility(newState: AwesomeBarState): Boolean {
|
||||||
|
return awesomeBarUIView.state?.showShortcutEnginePicker != newState.showShortcutEnginePicker
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun showShortcutEnginePicker() {
|
||||||
|
with(context) {
|
||||||
|
awesomeBarUIView.search_shortcuts_button.background = getDrawable(R.drawable.search_pill_background)
|
||||||
|
|
||||||
|
awesomeBarUIView.search_shortcuts_button.compoundDrawables[0].setTint(ContextCompat.getColor(this,
|
||||||
|
DefaultThemeManager.resolveAttribute(R.attr.pillWrapperBackground, this)))
|
||||||
|
|
||||||
|
awesomeBarUIView.search_shortcuts_button.textColor = ContextCompat.getColor(this,
|
||||||
|
DefaultThemeManager.resolveAttribute(R.attr.pillWrapperBackground, this))
|
||||||
|
|
||||||
|
awesomeBarUIView.view.removeAllProviders()
|
||||||
|
awesomeBarUIView.view.addProviders(shortcutsEnginePickerProvider!!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun hideShortcutEnginePicker() {
|
||||||
|
with(context) {
|
||||||
|
awesomeBarUIView.search_shortcuts_button.setBackgroundColor(ContextCompat.getColor(this,
|
||||||
|
DefaultThemeManager.resolveAttribute(R.attr.pillWrapperBackground, this)))
|
||||||
|
|
||||||
|
awesomeBarUIView.search_shortcuts_button.compoundDrawables[0].setTint(ContextCompat.getColor(this,
|
||||||
|
DefaultThemeManager.resolveAttribute(R.attr.searchShortcutsTextColor, this)))
|
||||||
|
|
||||||
|
awesomeBarUIView.search_shortcuts_button.textColor = ContextCompat.getColor(this,
|
||||||
|
DefaultThemeManager.resolveAttribute(R.attr.searchShortcutsTextColor, this))
|
||||||
|
|
||||||
|
awesomeBarUIView.view.removeProviders(shortcutsEnginePickerProvider!!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateSearchWithVisibility(visible: Boolean) {
|
||||||
|
awesomeBarUIView.search_with_shortcuts.visibility = if (visible) View.VISIBLE else View.GONE
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
package org.mozilla.fenix.search.awesomebar
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import mozilla.components.browser.search.SearchEngine
|
||||||
|
import mozilla.components.browser.search.SearchEngineManager
|
||||||
|
import mozilla.components.concept.awesomebar.AwesomeBar
|
||||||
|
import mozilla.components.support.ktx.android.graphics.drawable.toBitmap
|
||||||
|
import org.mozilla.fenix.R
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A [AwesomeBar.SuggestionProvider] implementation that provides search engine suggestions.
|
||||||
|
*/
|
||||||
|
class ShortcutsSuggestionProvider(
|
||||||
|
private val searchEngineManager: SearchEngineManager,
|
||||||
|
private val context: Context,
|
||||||
|
private val selectShortcutEngine: (engine: SearchEngine) -> Unit,
|
||||||
|
private val selectShortcutEngineSettings: () -> Unit
|
||||||
|
) : AwesomeBar.SuggestionProvider {
|
||||||
|
override val id: String = UUID.randomUUID().toString()
|
||||||
|
|
||||||
|
override val shouldClearSuggestions: Boolean
|
||||||
|
get() = false
|
||||||
|
|
||||||
|
override suspend fun onInputChanged(text: String): List<AwesomeBar.Suggestion> {
|
||||||
|
val suggestions = mutableListOf<AwesomeBar.Suggestion>()
|
||||||
|
|
||||||
|
searchEngineManager.getSearchEngines(context).forEach {
|
||||||
|
suggestions.add(
|
||||||
|
AwesomeBar.Suggestion(
|
||||||
|
provider = this,
|
||||||
|
id = id,
|
||||||
|
icon = { _, _ ->
|
||||||
|
it.icon
|
||||||
|
},
|
||||||
|
title = it.name,
|
||||||
|
onSuggestionClicked = {
|
||||||
|
selectShortcutEngine(it)
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
suggestions.add(
|
||||||
|
AwesomeBar.Suggestion(
|
||||||
|
provider = this,
|
||||||
|
id = id,
|
||||||
|
icon = { _, _ ->
|
||||||
|
context.getDrawable(R.drawable.ic_settings)?.toBitmap()
|
||||||
|
},
|
||||||
|
title = context.getString(R.string.search_shortcuts_engine_settings),
|
||||||
|
onSuggestionClicked = {
|
||||||
|
selectShortcutEngineSettings()
|
||||||
|
})
|
||||||
|
)
|
||||||
|
return suggestions
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue