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

Commit

Permalink
Refactor and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sblatz committed Mar 26, 2019
1 parent ff6bdda commit e8227b1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,39 +93,63 @@ class ToolbarUIView(
}

override fun updateView() = Consumer<SearchState> {
with(view.context) {
if (it.isEditing) {
val defaultEngineIcon = components.search.searchEngineManager.defaultSearchEngine?.icon
val searchIcon = it.engine?.icon ?: defaultEngineIcon
val draw = BitmapDrawable(searchIcon)
val iconSize =
containerView?.context!!.resources.getDimension(R.dimen.preference_icon_drawable_size).toInt()
draw.setBounds(0, 0, iconSize, iconSize)
engineIconView?.backgroundDrawable = draw
}
if (shouldUpdateEngineIcon(it)) {
updateEngineIcon(it)
}

if (shouldClearSearchURL(it)) {
view.url = ""
view.editMode()
clearSearchURL()
}

if (it.engine == state?.engine) {
if (it.isEditing) {
view.url = it.query
view.editMode()
} else {
view.displayMode()
}
if (shouldUpdateEditingState(it)) {
updateEditingState(it)
}

state = it
}

private fun shouldUpdateEngineIcon(newState: SearchState): Boolean {
return (newState.isEditing && engineDidChange(newState))
}

private fun updateEngineIcon(newState: SearchState) {
with(view.context) {
val defaultEngineIcon = components.search.searchEngineManager.defaultSearchEngine?.icon
val searchIcon = newState.engine?.icon ?: defaultEngineIcon
val draw = BitmapDrawable(searchIcon)
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 {
return newState.engine != state?.engine && view.url == newState.query
}

private fun clearSearchURL() {
view.url = ""
view.editMode()
}

private fun shouldUpdateEditingState(newState: SearchState): Boolean {
return !engineDidChange(newState)
}

private fun updateEditingState(newState: SearchState) {
if (newState.isEditing) {
view.url = newState.query
view.editMode()
} else {
view.displayMode()
}
}

private fun engineDidChange(newState: SearchState): Boolean {
return newState.engine != state?.engine
}

companion object {
const val browserActionMarginDp = 8
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class ShortcutsSuggestionProvider(
},
title = context.getString(R.string.search_shortcuts_engine_settings),
onSuggestionClicked = {
// Open search engine settings
selectShortcutEngineSettings()
})
)
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/res/layout/fragment_search.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
android:id="@+id/search_layout"
android:background="?attr/searchBackground">



<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/toolbar_wrapper"
android:layout_width="0dp"
Expand Down

0 comments on commit e8227b1

Please sign in to comment.