For #6018 - Added back shortcuts button in awesomebar
Refactored logic for adding providers, since shortcut providers should be alone in the list, while all others can be as list
This commit is contained in:
parent
4abe22922a
commit
2be4d08228
|
@ -32,6 +32,7 @@ interface SearchController {
|
|||
fun handleSearchShortcutEngineSelected(searchEngine: SearchEngine)
|
||||
fun handleClickSearchEngineSettings()
|
||||
fun handleExistingSessionSelected(session: Session)
|
||||
fun handleSearchShortcutsButtonClicked()
|
||||
}
|
||||
|
||||
class DefaultSearchController(
|
||||
|
@ -98,6 +99,11 @@ class DefaultSearchController(
|
|||
context.metrics.track(Event.SearchShortcutSelected(searchEngine.name))
|
||||
}
|
||||
|
||||
override fun handleSearchShortcutsButtonClicked() {
|
||||
val isOpen = store.state.showSearchShortcuts
|
||||
store.dispatch(SearchFragmentAction.ShowSearchShortcutEnginePicker(!isOpen))
|
||||
}
|
||||
|
||||
override fun handleClickSearchEngineSettings() {
|
||||
val directions = SearchFragmentDirections.actionSearchFragmentToSearchEngineFragment()
|
||||
navController.navigate(directions)
|
||||
|
|
|
@ -41,6 +41,10 @@ class SearchInteractor(
|
|||
searchController.handleSearchShortcutEngineSelected(searchEngine)
|
||||
}
|
||||
|
||||
override fun onSearchShortcutsButtonClicked() {
|
||||
searchController.handleSearchShortcutsButtonClicked()
|
||||
}
|
||||
|
||||
override fun onClickSearchEngineSettings() {
|
||||
searchController.handleClickSearchEngineSettings()
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import android.view.View
|
|||
import android.view.ViewGroup
|
||||
import androidx.core.graphics.drawable.toBitmap
|
||||
import kotlinx.android.extensions.LayoutContainer
|
||||
import kotlinx.android.synthetic.main.fragment_search.*
|
||||
import mozilla.components.browser.awesomebar.BrowserAwesomeBar
|
||||
import mozilla.components.browser.search.SearchEngine
|
||||
import mozilla.components.browser.session.Session
|
||||
|
@ -64,6 +65,11 @@ interface AwesomeBarInteractor {
|
|||
* Called whenever an existing session is selected from the sessionSuggestionProvider
|
||||
*/
|
||||
fun onExistingSessionSelected(session: Session)
|
||||
|
||||
/**
|
||||
* Called whenever the Shortcuts button is clicked
|
||||
*/
|
||||
fun onSearchShortcutsButtonClicked()
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -164,10 +170,16 @@ class AwesomeBarView(
|
|||
interactor::onClickSearchEngineSettings
|
||||
)
|
||||
}
|
||||
|
||||
searchShortcutsButton.setOnClickListener {
|
||||
interactor.onSearchShortcutsButtonClicked()
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("ComplexMethod")
|
||||
fun update(state: SearchFragmentState) {
|
||||
updateSearchShortcutsIcon(state)
|
||||
|
||||
// Do not make suggestions based on user's current URL
|
||||
if (state.query == state.session?.url) {
|
||||
return
|
||||
|
@ -178,17 +190,29 @@ class AwesomeBarView(
|
|||
view.onInputChanged(state.query)
|
||||
}
|
||||
|
||||
private fun updateSearchShortcutsIcon(searchState: SearchFragmentState) {
|
||||
with(container.context) {
|
||||
val showShortcuts = searchState.showSearchShortcuts
|
||||
searchShortcutsButton?.isChecked = showShortcuts
|
||||
|
||||
val color = if (showShortcuts) R.attr.contrastText else R.attr.primaryText
|
||||
|
||||
searchShortcutsButton.compoundDrawables[0]?.setTint(getColorFromAttr(color))
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("ComplexMethod")
|
||||
private fun updateSuggestionProvidersVisibility(state: SearchFragmentState) {
|
||||
val providersToAdd = mutableSetOf<AwesomeBar.SuggestionProvider>()
|
||||
val providersToRemove = mutableSetOf<AwesomeBar.SuggestionProvider>()
|
||||
|
||||
if (state.showSearchShortcuts) {
|
||||
providersToAdd.add(shortcutsEnginePickerProvider)
|
||||
} else {
|
||||
providersToRemove.add(shortcutsEnginePickerProvider)
|
||||
handleDisplayShortcutsProviders()
|
||||
return
|
||||
}
|
||||
|
||||
providersToRemove.add(shortcutsEnginePickerProvider)
|
||||
|
||||
if (state.showHistorySuggestions) {
|
||||
providersToAdd.add(historyStorageProvider)
|
||||
} else {
|
||||
|
@ -211,12 +235,14 @@ class AwesomeBarView(
|
|||
}
|
||||
)
|
||||
} else {
|
||||
providersToRemove.add(when (state.searchEngineSource) {
|
||||
providersToRemove.add(
|
||||
when (state.searchEngineSource) {
|
||||
is SearchEngineSource.Default -> defaultSearchSuggestionProvider
|
||||
is SearchEngineSource.Shortcut -> createSuggestionProviderForEngine(
|
||||
state.searchEngineSource.searchEngine
|
||||
)
|
||||
})
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
if ((container.context.asActivity() as? HomeActivity)?.browsingModeManager?.mode?.isPrivate == false) {
|
||||
|
@ -240,6 +266,13 @@ class AwesomeBarView(
|
|||
}
|
||||
}
|
||||
|
||||
private fun handleDisplayShortcutsProviders() {
|
||||
view.removeAllProviders()
|
||||
providersInUse.clear()
|
||||
providersInUse.add(shortcutsEnginePickerProvider)
|
||||
view.addProviders(shortcutsEnginePickerProvider)
|
||||
}
|
||||
|
||||
private fun createSuggestionProviderForEngine(engine: SearchEngine): SearchSuggestionProvider {
|
||||
return with(container.context) {
|
||||
val draw = getDrawable(R.drawable.ic_search)
|
||||
|
|
|
@ -156,5 +156,12 @@
|
|||
android:drawableStart="@drawable/ic_qr"
|
||||
android:textOff="@string/search_scan_button"
|
||||
android:textOn="@string/search_scan_button" />
|
||||
|
||||
<ToggleButton
|
||||
android:id="@+id/searchShortcutsButton"
|
||||
style="@style/search_pill"
|
||||
android:drawableStart="@drawable/ic_search"
|
||||
android:textOff="@string/search_shortcuts_button"
|
||||
android:textOn="@string/search_shortcuts_button" />
|
||||
</LinearLayout>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
@ -95,6 +95,8 @@
|
|||
<!-- Search Fragment -->
|
||||
<!-- Button in the search view that lets a user search by scanning a QR code -->
|
||||
<string name="search_scan_button">Scan</string>
|
||||
<!-- Button in the search view that lets a user search by using a shortcut -->
|
||||
<string name="search_shortcuts_button">Shortcuts</string>
|
||||
<!-- Button in the search view when shortcuts are displayed that takes a user to the search engine settings -->
|
||||
<string name="search_shortcuts_engine_settings">Search engine settings</string>
|
||||
<!-- Header displayed when selecting a shortcut search engine -->
|
||||
|
|
Loading…
Reference in New Issue
Block a user