Use requireNotNull to raise exception

Rename find to textToFind and clean up code
This commit is contained in:
Abdul Wadood 2020-10-22 15:15:32 +05:30
parent 8fa8f40c83
commit 7770d6a6ae

View File

@ -82,12 +82,9 @@ class CompatFindActionModeCallback internal constructor(context: Context) :
// Set the WebView to search. Must be non null, and set before calling startActionMode. // Set the WebView to search. Must be non null, and set before calling startActionMode.
fun setWebView(webView: WebView?) { fun setWebView(webView: WebView?) {
if (null == webView) { this.webView = requireNotNull(webView) {
throw AssertionError(
"WebView supplied to CompatFindActionModeCallback cannot be null" "WebView supplied to CompatFindActionModeCallback cannot be null"
)
} }
this.webView = webView
findResultsTextView.visibility = View.VISIBLE findResultsTextView.visibility = View.VISIBLE
this.webView?.setFindListener { activeMatchOrdinal: Int, numberOfMatches: Int, _: Boolean -> this.webView?.setFindListener { activeMatchOrdinal: Int, numberOfMatches: Int, _: Boolean ->
val result: String = when { val result: String = when {
@ -109,23 +106,19 @@ class CompatFindActionModeCallback internal constructor(context: Context) :
// If true, find the next match further down in the document. // If true, find the next match further down in the document.
// If false, find the previous match, up in the document. // If false, find the previous match, up in the document.
private fun findNext(next: Boolean) { private fun findNext(next: Boolean) {
if (webView == null) { requireNotNull(webView) {
throw AssertionError("No WebView for CompatFindActionModeCallback::findNext") "No WebView for CompatFindActionModeCallback::findNext"
} }.findNext(next)
webView?.findNext(next)
} }
// Highlight all the instances of the string from mEditText in mWebView. // Highlight all the instances of the string from mEditText in mWebView.
fun findAll() { fun findAll() {
if (webView == null) { requireNotNull(webView) {
throw AssertionError("No WebView for CompatFindActionModeCallback::findAll") "No WebView for CompatFindActionModeCallback::findAll"
} }
val find: CharSequence? = editText.text val textToFind: CharSequence? = editText.text
if (find == null || find.isEmpty()) { if (textToFind?.isNotEmpty() == true) {
webView?.clearMatches() webView?.findAllAsync("$textToFind")
webView?.findAllAsync("")
} else {
webView?.findAllAsync("$find")
// Enable word highlighting with reflection // Enable word highlighting with reflection
try { try {
@ -139,6 +132,9 @@ class CompatFindActionModeCallback internal constructor(context: Context) :
} catch (exception: Exception) { } catch (exception: Exception) {
Log.e(tag, "Exception in findAll", exception) Log.e(tag, "Exception in findAll", exception)
} }
} else {
webView?.clearMatches()
webView?.findAllAsync("")
} }
} }
@ -176,10 +172,8 @@ class CompatFindActionModeCallback internal constructor(context: Context) :
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean = false override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean = false
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
if (webView == null) { requireNotNull(webView) {
throw AssertionError(
"No WebView for CompatFindActionModeCallback::onActionItemClicked" "No WebView for CompatFindActionModeCallback::onActionItemClicked"
)
} }
input.hideSoftInputFromWindow(webView?.windowToken, 0) input.hideSoftInputFromWindow(webView?.windowToken, 0)
when (item.itemId) { when (item.itemId) {