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

Commit

Permalink
Closes #875: Adds shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
sblatz committed Mar 13, 2019
1 parent 33b83fb commit d4e9707
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 50 deletions.
16 changes: 11 additions & 5 deletions app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.appcompat.widget.Toolbar
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.NavigationUI
import mozilla.components.browser.search.SearchEngine
import mozilla.components.browser.session.Session
import mozilla.components.concept.engine.EngineView
import mozilla.components.feature.intent.IntentProcessor
Expand Down Expand Up @@ -115,9 +116,14 @@ open class HomeActivity : AppCompatActivity() {
openToBrowser(SafeIntent(intent).getStringExtra(IntentProcessor.ACTIVE_SESSION_ID), BrowserDirection.FromGlobal)
}

fun openToBrowserAndLoad(text: String, sessionId: String? = null, from: BrowserDirection) {
fun openToBrowserAndLoad(
text: String,
sessionId: String? = null,
engine: SearchEngine? = null,
from: BrowserDirection
) {
openToBrowser(sessionId, from)
load(text, sessionId)
load(text, sessionId, engine)
}

fun openToBrowser(sessionId: String?, from: BrowserDirection) {
Expand All @@ -138,7 +144,7 @@ open class HomeActivity : AppCompatActivity() {
host.navController.navigate(directions)
}

private fun load(text: String, sessionId: String?) {
private fun load(text: String, sessionId: String?, engine: SearchEngine?) {
val isPrivate = this.browsingModeManager.isPrivate

val loadUrlUseCase = if (sessionId == null) {
Expand All @@ -152,8 +158,8 @@ open class HomeActivity : AppCompatActivity() {
val searchUseCase: (String) -> Unit = { searchTerms ->
if (sessionId == null) {
components.useCases.searchUseCases.newTabSearch
.invoke(searchTerms, Session.Source.USER_ENTERED, true, isPrivate)
} else components.useCases.searchUseCases.defaultSearch.invoke(searchTerms)
.invoke(searchTerms, Session.Source.USER_ENTERED, true, isPrivate, searchEngine = engine)
} else components.useCases.searchUseCases.defaultSearch.invoke(searchTerms, engine)
}

if (text.isUrl()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package org.mozilla.fenix.components.toolbar
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.component_search.*
import mozilla.components.browser.search.SearchEngine
import mozilla.components.browser.toolbar.BrowserToolbar
import org.mozilla.fenix.DefaultThemeManager
import org.mozilla.fenix.R
Expand Down Expand Up @@ -34,6 +35,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,15 +63,20 @@ class ToolbarComponent(
}
}

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

sealed class SearchAction : Action {
data class UrlCommitted(val url: String, val session: String?) : SearchAction()
data class UrlCommitted(val url: String, val session: String?, val engine: SearchEngine? = null) : SearchAction()
data class TextChanged(val query: String) : SearchAction()
object ToolbarTapped : SearchAction()
data class ToolbarMenuItemTapped(val item: ToolbarMenu.Item) : SearchAction()
}

sealed class SearchChange : Change {
data class QueryChanged(val query: String) : SearchChange()
data class SearchShortcutEngineSelected(val engine: SearchEngine) : SearchChange()
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class ToolbarUIView(
UIView<SearchState, SearchAction, SearchChange>(container, actionEmitter, changesObservable) {

val toolbarIntegration: ToolbarIntegration
var state: SearchState? = null
private set

override val view: BrowserToolbar = LayoutInflater.from(container.context)
.inflate(R.layout.component_search, container, true)
Expand All @@ -38,7 +40,7 @@ class ToolbarUIView(
init {
view.apply {
setOnUrlCommitListener {
actionEmitter.onNext(SearchAction.UrlCommitted(it, sessionId))
actionEmitter.onNext(SearchAction.UrlCommitted(it, sessionId, state?.engine))
false
}
onUrlClicked = {
Expand Down Expand Up @@ -92,6 +94,10 @@ class ToolbarUIView(
} else {
view.displayMode()
}

state = it

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

companion object {
Expand Down
18 changes: 17 additions & 1 deletion app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.fragment_search.*
import kotlinx.android.synthetic.main.fragment_search.view.*
import mozilla.components.feature.search.SearchUseCases
import mozilla.components.feature.session.SessionUseCases
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.components.toolbar.SearchAction
import org.mozilla.fenix.components.toolbar.SearchChange
import org.mozilla.fenix.components.toolbar.SearchState
import org.mozilla.fenix.components.toolbar.ToolbarComponent
import org.mozilla.fenix.components.toolbar.ToolbarUIView
Expand All @@ -29,6 +31,7 @@ import org.mozilla.fenix.mvi.getManagedEmitter
import org.mozilla.fenix.search.awesomebar.AwesomeBarAction
import org.mozilla.fenix.search.awesomebar.AwesomeBarChange
import org.mozilla.fenix.search.awesomebar.AwesomeBarComponent
import org.mozilla.fenix.search.awesomebar.AwesomeBarUIView

class SearchFragment : Fragment() {
private lateinit var toolbarComponent: ToolbarComponent
Expand Down Expand Up @@ -57,6 +60,7 @@ class SearchFragment : Fragment() {
isPrivate,
SearchState(url, isEditing = true)
)

awesomeBarComponent = AwesomeBarComponent(view.search_layout, ActionBusFactory.get(this))
ActionBusFactory.get(this).logMergedObservables()
return view
Expand All @@ -70,6 +74,12 @@ class SearchFragment : Fragment() {
lifecycle.addObserver((toolbarComponent.uiView as ToolbarUIView).toolbarIntegration)

view.toolbar_wrapper.clipToOutline = false

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

override fun onResume() {
Expand All @@ -84,7 +94,7 @@ class SearchFragment : Fragment() {
when (it) {
is SearchAction.UrlCommitted -> {
if (it.url.isNotBlank()) {
(activity as HomeActivity).openToBrowserAndLoad(it.url, it.session,
(activity as HomeActivity).openToBrowserAndLoad(it.url, it.session, it.engine,
BrowserDirection.FromSearch)
}
}
Expand All @@ -106,6 +116,12 @@ class SearchFragment : Fragment() {
.invoke(it.searchTerms, it.engine)
(activity as HomeActivity).openToBrowser(sessionId, BrowserDirection.FromSearch)
}
is AwesomeBarAction.SearchShorcutEngineSelected -> {
getManagedEmitter<AwesomeBarChange>()
.onNext(AwesomeBarChange.SearchShorcutEngineSelected(it.engine))
getManagedEmitter<SearchChange>()
.onNext(SearchChange.SearchShortcutEngineSelected(it.engine))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,38 @@ import org.mozilla.fenix.mvi.Reducer
import org.mozilla.fenix.mvi.UIComponent
import org.mozilla.fenix.mvi.ViewState

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

sealed class AwesomeBarAction : Action {
data class URLTapped(val url: String) : AwesomeBarAction()
data class SearchTermsTapped(val searchTerms: String, val engine: SearchEngine?) : AwesomeBarAction()
data class SearchTermsTapped(val searchTerms: String, val engine: SearchEngine? = null) : AwesomeBarAction()
data class SearchShorcutEngineSelected(val engine: SearchEngine) : AwesomeBarAction()
}

sealed class AwesomeBarChange : Change {
data class SearchShorcutEngineSelected(val engine: SearchEngine) : AwesomeBarChange()
data class SearchShortcutEnginePicker(val show: Boolean) : AwesomeBarChange()
data class UpdateQuery(val query: String) : AwesomeBarChange()
}

class AwesomeBarComponent(
private val container: ViewGroup,
bus: ActionBusFactory,
override var initialState: AwesomeBarState = AwesomeBarState("")
override var initialState: AwesomeBarState = AwesomeBarState("", false)
) : UIComponent<AwesomeBarState, AwesomeBarAction, AwesomeBarChange>(
bus.getManagedEmitter(AwesomeBarAction::class.java),
bus.getSafeManagedObservable(AwesomeBarChange::class.java)
) {
override val reducer: Reducer<AwesomeBarState, AwesomeBarChange> = { state, change ->
when (change) {
is AwesomeBarChange.SearchShorcutEngineSelected ->
state.copy(suggestionEngine = change.engine, showShortcutEnginePicker = false)
is AwesomeBarChange.SearchShortcutEnginePicker ->
state.copy(showShortcutEnginePicker = change.show)
is AwesomeBarChange.UpdateQuery -> state.copy(query = change.query)
}
}
Expand Down
Loading

0 comments on commit d4e9707

Please sign in to comment.