Skip to content

Commit

Permalink
Add WebViewSearchBar for your web view searching needs
Browse files Browse the repository at this point in the history
Just a basic widget for replacing the broken implementation in the
WebView itself. As a bonus it's themed to match the toolbar now!

I've added the counter that shows how many results you have/which one is
currently highlighted, and tried to style it like Chrome does it (on the
desktop anyway) - so basically the count area can expand and push back on
the typey bit, but the longer the part you're typing the less likely you
are to get a lot of results, so it shouldn't be an issue

Also when I came to use the carets for the prev/next buttons, I
noticed the down one was bigger - different viewport settings for
whatever reason, and I couldn't change those on the up caret without
getting a weird offset. So I've switched out references to that big caret
for the smaller one, and adjusted the scaling on those image views to
compensate

That's the subforum expander and the down button to show the original
PM message - the former should look about the same, I can't expand the PM
button image without the button growing with it, so I left it but it looks
fine. I've also deleted an XML thing that wasn't being used (pretty sure
it was abandoned in favour of the spinny expander arrows)
  • Loading branch information
baka-kaba committed Feb 24, 2020
1 parent b4836a3 commit 54a5177
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
import com.ferg.awfulapp.webview.WebViewJsInterface;
import com.ferg.awfulapp.widget.PageBar;
import com.ferg.awfulapp.widget.PagePicker;
import com.ferg.awfulapp.widget.WebViewSearchBar;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;

Expand Down Expand Up @@ -549,9 +550,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
copyThreadURL(null);
break;
case R.id.find:
if (mThreadView != null) {
this.mThreadView.showFindDialog(null, true);
}
((WebViewSearchBar) item.getActionView()).setWebView(mThreadView);
break;
case R.id.keep_screen_on:
this.toggleScreenOn();
Expand Down
6 changes: 5 additions & 1 deletion Awful.apk/src/main/java/com/ferg/awfulapp/util/ViewExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.ferg.awfulapp.util
import android.app.Activity
import android.support.v4.app.Fragment
import android.view.View
import android.view.ViewGroup


fun View.isVisible() = (this.visibility == View.VISIBLE)
Expand All @@ -28,4 +29,7 @@ fun <T : View?> Fragment.bind(resId: Int): Lazy<T> =
lazy(LazyThreadSafetyMode.NONE) { view!!.findViewById<T>(resId) }

fun <T : View?> Activity.bind(resId: Int): Lazy<T> =
lazy(LazyThreadSafetyMode.NONE) { findViewById<T>(resId) }
lazy(LazyThreadSafetyMode.NONE) { findViewById<T>(resId) }

fun <T : View?> ViewGroup.bind(resId: Int): Lazy<T> =
lazy(LazyThreadSafetyMode.NONE) { findViewById<T>(resId) }
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.ferg.awfulapp.widget

import android.content.Context
import android.support.v7.view.CollapsibleActionView
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.inputmethod.InputMethodManager
import android.webkit.WebView
import android.widget.EditText
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.TextView
import com.ferg.awfulapp.R
import com.ferg.awfulapp.util.bind
import kotlin.properties.Delegates


/**
* Created by baka kaba on 24/02/2020.
*
* A simple widget for searching a provided [webView].
*
* Replaces the in-built find toolbar in the Webview class which is fully broken at this point.
* Set [webView] and text entered into the search bar will be forwarded to its search system. This
* implements [CollapsibleActionView] so it can be used as an expanding menu item that appears in
* the toolbar, and cleans up after itself once closed.
*
* Implemented as a LinearLayout because ConstraintLayout wasn't playing nice with the textbox filling
* the available area.
*/

class WebViewSearchBar @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr), WebView.FindListener, CollapsibleActionView {

private val nextButton: ImageButton by bind(R.id.web_view_search_next_result)
private val prevButton: ImageButton by bind(R.id.web_view_search_prev_result)
private val searchBox: EditText by bind(R.id.web_view_search_text)
private val resultCount: TextView by bind(R.id.webview_search_result_count)

/** The webview this widget will attempt to search in */
var webView by Delegates.observable<WebView?>(null) { _, _, new -> new?.setFindListener(this) }

init {
LayoutInflater.from(context)
.inflate(R.layout.webview_search_bar, this, true)
searchBox.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
onTextChanged()
}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}

})
nextButton.setOnClickListener { webView?.findNext(true) }
prevButton.setOnClickListener { webView?.findNext(false) }
}

/** Called when the text in the search box changes */
private fun onTextChanged() {
// don't pass null to #findAllAsync no matter what it claims, those NPEs are why I had to write this
webView?.findAllAsync(searchBox.text?.toString() ?: "")
}

override fun onFindResultReceived(activeMatchOrdinal: Int, numberOfMatches: Int, isDoneCounting: Boolean) {
resultCount.text = when {
searchBox.text.isEmpty() -> ""
numberOfMatches == 0 -> "0/0"
else -> "${activeMatchOrdinal + 1}/$numberOfMatches"
}
}

override fun onActionViewExpanded() {
searchBox.requestFocus()
// need to delay this call for... reasons? this makes the keyboard pop up reliably though
searchBox.postDelayed({
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
imm?.showSoftInput(searchBox, InputMethodManager.SHOW_IMPLICIT)
}, 100)
}

override fun onActionViewCollapsed() {
// clean up because we're only getting hidden
searchBox.text.clear()
searchBox.clearFocus()
webView?.clearMatches()
}
}
7 changes: 0 additions & 7 deletions Awful.apk/src/main/res/drawable/expander_group.xml

This file was deleted.

10 changes: 5 additions & 5 deletions Awful.apk/src/main/res/drawable/ic_expand_more.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="18.0"
android:viewportHeight="18.0">
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="?attr/iconColor"
android:pathData="M13.59,5.59L9,10.17 4.41,5.59 3,7l6,6 6,-6z"/>
android:pathData="M16.59,8.59L12,13.17 7.41,8.59 6,10l6,6 6,-6z" />
</vector>
5 changes: 4 additions & 1 deletion Awful.apk/src/main/res/layout/forum_index_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@
android:layout_height="20dp"
android:layout_gravity="center"
android:contentDescription="@string/show_and_hide_subforums"
android:scaleType="fitCenter"
android:scaleX="1.3"
android:scaleY="1.3"
android:tint="?primaryPostFontColor"
android:visibility="visible"
app:srcCompat="@drawable/ic_expand_more"
tools:ignore="MissingPrefix"/>
tools:ignore="MissingPrefix" />

</LinearLayout>

Expand Down
5 changes: 3 additions & 2 deletions Awful.apk/src/main/res/layout/private_message_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@
android:id="@+id/hide_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:scaleType="center"
app:srcCompat="@drawable/ic_expand_more"
tools:ignore="MissingPrefix"/>
tools:ignore="MissingPrefix" />
</RelativeLayout>

<com.ferg.awfulapp.webview.AwfulWebView
Expand Down
60 changes: 60 additions & 0 deletions Awful.apk/src/main/res/layout/webview_search_bar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:layout_height="?android:attr/actionBarSize">

<EditText
android:id="@+id/web_view_search_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:ems="10"
android:background="@android:color/transparent"
android:hint="@string/web_view_search_box_hint"
android:inputType="textPersonName"
android:singleLine="true" />

<TextView
android:id="@+id/webview_search_result_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="8dp"
android:paddingEnd="16dp"
android:textAlignment="textEnd"
android:textAllCaps="false"
android:textAppearance="@style/TextAppearance.MaterialComponents.Caption"
android:textColor="?attr/actionBarFontColor"
android:textSize="12sp"
android:typeface="normal"
tools:text="1000/1000" />

<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="?attr/actionBarDivider" />

<ImageButton
android:id="@+id/web_view_search_prev_result"
style="@style/MaterialImageButton.Borderless"
android:layout_width="@dimen/material_touch_target_size"
android:layout_height="@dimen/material_touch_target_size"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_expand_less"
android:scaleType="centerInside"
android:contentDescription="@string/web_view_previous_result_description" />

<ImageButton
android:id="@+id/web_view_search_next_result"
style="@style/MaterialImageButton.Borderless"
android:layout_width="@dimen/material_touch_target_size"
android:layout_height="@dimen/material_touch_target_size"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_expand_more"
android:scaleType="centerInside"
android:contentDescription="@string/web_view_next_result_description" />

</LinearLayout>
3 changes: 2 additions & 1 deletion Awful.apk/src/main/res/menu/post_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
android:id="@+id/find"
android:icon="@drawable/ic_find_in_page_dark"
android:title="@string/find"
app:showAsAction="never"
app:showAsAction="never|collapseActionView"
app:actionViewClass="com.ferg.awfulapp.widget.WebViewSearchBar"
/>
<item
android:id="@+id/keep_screen_on"
Expand Down
5 changes: 5 additions & 0 deletions Awful.apk/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,11 @@
<string name="send_username_in_report_summary">Attach username to crash reports</string>


<string name="web_view_previous_result_description">Previous result</string>
<string name="web_view_next_result_description">Next result</string>
<string name="web_view_search_box_hint">Search in page</string>


<!--
End of Settings menu strings
-->
Expand Down

0 comments on commit 54a5177

Please sign in to comment.