Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Adds shortcut logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sblatz committed Mar 7, 2019
1 parent 7fce0d8 commit 2876cdb
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class ToolbarComponent(
override val reducer: Reducer<SearchState, SearchChange> = { state, change ->
when (change) {
is SearchChange.QueryChanged -> state.copy(query = change.query)
is SearchChange.SearchShortcutEngineSelected ->
state.copy(engine = change.engine)
}
}

Expand All @@ -60,7 +62,9 @@ class ToolbarComponent(
}
}

data class SearchState(val query: String, val isEditing: Boolean) : ViewState
data class SearchState(val query: String,
val isEditing: Boolean,
val engine: String? = null) : ViewState

sealed class SearchAction : Action {
data class UrlCommitted(val url: String, val session: String?) : SearchAction()
Expand All @@ -71,4 +75,5 @@ sealed class SearchAction : Action {

sealed class SearchChange : Change {
data class QueryChanged(val query: String) : SearchChange()
data class SearchShortcutEngineSelected(val engine: String) : SearchChange()
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class ToolbarUIView(
} else {
view.displayMode()
}

it.engine?.let { engine -> view.setSearchShortcut("@ " + engine.toLowerCase()) }
}

companion object {
Expand Down
17 changes: 11 additions & 6 deletions app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import mozilla.components.support.ktx.kotlin.isUrl
import mozilla.components.support.ktx.kotlin.toNormalizedUrl
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.components.toolbar.SearchAction
import org.mozilla.fenix.components.toolbar.SearchState
import org.mozilla.fenix.components.toolbar.ToolbarComponent
import org.mozilla.fenix.components.toolbar.ToolbarUIView
import org.mozilla.fenix.components.toolbar.*
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.mvi.ActionBusFactory
Expand Down Expand Up @@ -79,8 +76,8 @@ class SearchFragment : Fragment() {

search_shortcuts_button.setOnClickListener {
getManagedEmitter<AwesomeBarChange>().onNext(AwesomeBarChange
.SearchShortcutSelected(!(
(awesomeBarComponent.uiView as AwesomeBarUIView).state?.searchShortcutSelected ?: true)))
.SearchShortcutEnginePicker(!(
(awesomeBarComponent.uiView as AwesomeBarUIView).state?.showShortcutEnginePicker ?: true)))
}
}

Expand Down Expand Up @@ -117,6 +114,14 @@ class SearchFragment : Fragment() {
getSearchUseCase(requireContext(), sessionId == null).invoke(it.searchTerms)
transitionToBrowser()
}
is AwesomeBarAction.SearchEngineSelected -> {
getManagedEmitter<AwesomeBarChange>()
.onNext(AwesomeBarChange.SearchEngineSelected(it.engine))
getManagedEmitter<AwesomeBarChange>()
.onNext(AwesomeBarChange.SearchShortcutEnginePicker(false))
getManagedEmitter<SearchChange>()
.onNext(SearchChange.SearchShortcutEngineSelected(it.engine))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ import org.mozilla.fenix.mvi.Reducer
import org.mozilla.fenix.mvi.UIComponent
import org.mozilla.fenix.mvi.ViewState

data class AwesomeBarState(val query: String, val searchShortcutSelected: Boolean) : ViewState
data class AwesomeBarState(
val query: String,
val showShortcutEnginePicker: Boolean,
val suggestionEngine: String? = null
) : ViewState

sealed class AwesomeBarAction : Action {
data class URLTapped(val url: String) : AwesomeBarAction()
data class SearchTermsTapped(val searchTerms: String) : AwesomeBarAction()
data class SearchEngineSelected(val engine: String) : AwesomeBarAction()
}

sealed class AwesomeBarChange : Change {
data class SearchShortcutSelected(val selected: Boolean) : AwesomeBarChange()
data class SearchEngineSelected(val engine: String) : AwesomeBarChange()
data class SearchShortcutEnginePicker(val show: Boolean) : AwesomeBarChange()
data class UpdateQuery(val query: String) : AwesomeBarChange()
}

Expand All @@ -33,8 +39,10 @@ class AwesomeBarComponent(
) {
override val reducer: Reducer<AwesomeBarState, AwesomeBarChange> = { state, change ->
when (change) {
is AwesomeBarChange.SearchShortcutSelected ->
state.copy(searchShortcutSelected = change.selected)
is AwesomeBarChange.SearchEngineSelected ->
state.copy(suggestionEngine = change.engine)
is AwesomeBarChange.SearchShortcutEnginePicker ->
state.copy(showShortcutEnginePicker = change.show)
is AwesomeBarChange.UpdateQuery -> state.copy(query = change.query)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.mozilla.fenix.search.awesomebar
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.content.SearchRecentSuggestionsProvider
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
Expand Down Expand Up @@ -36,25 +37,37 @@ class AwesomeBarUIView(
var state: AwesomeBarState? = null
private set

var clipboardSuggestionProvider: ClipboardSuggestionProvider? = null
var sessionProvider: SessionSuggestionProvider? = null
var historyStorageProvider: HistoryStorageSuggestionProvider? = null
var searchSuggestionProvider: SearchSuggestionProvider? = null
var shortcutsSuggestionProvider: ShortcutsSuggestionProvider? = null
private var clipboardSuggestionProvider: ClipboardSuggestionProvider? = null
private var sessionProvider: SessionSuggestionProvider? = null
private var historyStorageProvider: HistoryStorageSuggestionProvider? = null
private var shortcutsEnginePickerProvider: ShortcutsSuggestionProvider? = null

init {
val loadUrlUseCase = object : SessionUseCases.LoadUrlUseCase {
override fun invoke(url: String) {
actionEmitter.onNext(AwesomeBarAction.URLTapped(url))
}
private val searchSuggestionProvider: SearchSuggestionProvider?
get() = searchSuggestionFromShortcutProvider ?: defaultSearchSuggestionProvider!!

private var defaultSearchSuggestionProvider: SearchSuggestionProvider? = null
private var searchSuggestionFromShortcutProvider: SearchSuggestionProvider? = null

private val loadUrlUseCase = object : SessionUseCases.LoadUrlUseCase {
override fun invoke(url: String) {
actionEmitter.onNext(AwesomeBarAction.URLTapped(url))
}
}

val searchUseCase = object : SearchUseCases.SearchUseCase {
override fun invoke(searchTerms: String) {
actionEmitter.onNext(AwesomeBarAction.SearchTermsTapped(searchTerms))
}
private val searchUseCase = object : SearchUseCases.SearchUseCase {
override fun invoke(searchTerms: String) {
actionEmitter.onNext(AwesomeBarAction.SearchTermsTapped(searchTerms))
}
}

private val shortcutSearchCase = object : SearchUseCases.SearchUseCase {
override fun invoke(searchTerms: String) {
// TODO set "engine = engineName" here
actionEmitter.onNext(AwesomeBarAction.SearchTermsTapped(searchTerms))
}
}

init {
with(container.context) {
clipboardSuggestionProvider = ClipboardSuggestionProvider(
this,
Expand All @@ -75,46 +88,73 @@ class AwesomeBarUIView(
loadUrlUseCase
)

searchSuggestionProvider =
defaultSearchSuggestionProvider =
SearchSuggestionProvider(
components.search.searchEngineManager.getDefaultSearchEngine(this),
searchUseCase,
components.core.client,
SearchSuggestionProvider.Mode.MULTIPLE_SUGGESTIONS
)
shortcutsSuggestionProvider =
ShortcutsSuggestionProvider(components.search.searchEngineManager, this)

shortcutsEnginePickerProvider =
ShortcutsSuggestionProvider(components.search.searchEngineManager, this, ::selectShortcutEngine)
}
}

private fun updateShortcutVisibility(selected: Boolean) {
private fun showShortcutEnginePicker() {
with(container.context) {
if (selected) {
search_shortcuts_button.background = getDrawable(R.drawable.search_pill_background)
search_shortcuts_button.background = getDrawable(R.drawable.search_pill_background)
view.removeAllProviders()
view.addProviders(shortcutsEnginePickerProvider!!)
}
}

view.removeAllProviders()
private fun hideShortcutEnginePicker() {
with(container.context) {
search_shortcuts_button.setBackgroundColor(ContextCompat.getColor(this,
DefaultThemeManager.resolveAttribute(R.attr.pillWrapperBackground, this)))
view.removeProviders(shortcutsEnginePickerProvider!!)
}
}

view.addProviders(shortcutsSuggestionProvider!!)
} else {
search_shortcuts_button.setBackgroundColor(ContextCompat.getColor(this,
DefaultThemeManager.resolveAttribute(R.attr.pillWrapperBackground, this)))
private fun showSuggestionProviders() {
view.addProviders(
clipboardSuggestionProvider!!,
sessionProvider!!,
historyStorageProvider!!,
searchSuggestionProvider!!
)
}

view.removeProviders(shortcutsSuggestionProvider!!)
private fun selectShortcutEngine(engineName: String) {
actionEmitter.onNext(AwesomeBarAction.SearchEngineSelected(engineName))
}

view.addProviders(
clipboardSuggestionProvider!!,
sessionProvider!!,
historyStorageProvider!!,
searchSuggestionProvider!!
)
}
private fun setShortcutEngine(engineName: String) {
with(container.context) {
searchSuggestionFromShortcutProvider = SearchSuggestionProvider(
components.search.searchEngineManager.getDefaultSearchEngine(this, engineName),
shortcutSearchCase,
components.core.client,
SearchSuggestionProvider.Mode.MULTIPLE_SUGGESTIONS
)
}
}

override fun updateView() = Consumer<AwesomeBarState> {
if (state?.searchShortcutSelected != it.searchShortcutSelected) {
updateShortcutVisibility(it.searchShortcutSelected)
it.suggestionEngine?.let { engine ->
if (state?.suggestionEngine != engine) {
setShortcutEngine(engine)
}
}

if (it.showShortcutEnginePicker) {
showShortcutEnginePicker()
} else {
hideShortcutEnginePicker()
showSuggestionProviders()
}

view.onInputChanged(it.query)

state = it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import java.util.UUID
*/
class ShortcutsSuggestionProvider(
private val searchEngineManager: SearchEngineManager,
private val context: Context
private val context: Context,
private val selectShortcutEngine: (engineName: String) -> Unit
) : AwesomeBar.SuggestionProvider {
override val id: String = UUID.randomUUID().toString()

Expand All @@ -31,7 +32,7 @@ class ShortcutsSuggestionProvider(
title = it.name,
description = getDescription(it.name),
onSuggestionClicked = {
// TODO pass this data to the awesomeBar
selectShortcutEngine(it.name)
})
)
}
Expand Down

0 comments on commit 2876cdb

Please sign in to comment.