diff options
author | Allan Wang <me@allanwang.ca> | 2017-07-04 17:22:36 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-04 17:22:36 -0400 |
commit | a67f99fcf210792da5e028570efbad61407aeab2 (patch) | |
tree | 3a2d617e9d9649828f13e581d8491c0371c67223 /app/src/main/kotlin/com/pitchedapps/frost/web | |
parent | 4cf368be0cd2f179485b05a3d2cd9b7bc236ba8e (diff) | |
download | frost-a67f99fcf210792da5e028570efbad61407aeab2.tar.gz frost-a67f99fcf210792da5e028570efbad61407aeab2.tar.bz2 frost-a67f99fcf210792da5e028570efbad61407aeab2.zip |
Dev (#21) - fix search and file chooser
* Fix up search and add file chooser
* Fix repeating file chooser
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/web')
3 files changed, 32 insertions, 21 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClient.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClient.kt index 00e72cc7..363bf795 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClient.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClient.kt @@ -2,10 +2,13 @@ package com.pitchedapps.frost.web import android.net.Uri import android.webkit.* +import ca.allanwang.kau.utils.snackbar +import com.pitchedapps.frost.contracts.ActivityWebContract import com.pitchedapps.frost.utils.L import io.reactivex.subjects.BehaviorSubject import io.reactivex.subjects.Subject + /** * Created by Allan Wang on 2017-05-31. */ @@ -13,6 +16,7 @@ class FrostChromeClient(webCore: FrostWebViewCore) : WebChromeClient() { val progressObservable: Subject<Int> = webCore.progressObservable val titleObservable: BehaviorSubject<String> = webCore.titleObservable + val activityContract = (webCore.context as? ActivityWebContract) override fun onConsoleMessage(consoleMessage: ConsoleMessage): Boolean { L.i("Chrome Console ${consoleMessage.lineNumber()}: ${consoleMessage.message()}") @@ -30,15 +34,9 @@ class FrostChromeClient(webCore: FrostWebViewCore) : WebChromeClient() { progressObservable.onNext(newProgress) } - override fun onShowFileChooser(webView: WebView, filePathCallback: ValueCallback<Array<Uri>>?, fileChooserParams: FileChooserParams?): Boolean { - L.d("On show file chooser") - fileChooserParams?.apply { - L.d(filenameHint ?: "hi") - L.d("$mode") - L.d(acceptTypes.contentToString()) - } - - return super.onShowFileChooser(webView, filePathCallback, fileChooserParams) + override fun onShowFileChooser(webView: WebView, filePathCallback: ValueCallback<Array<Uri>>, fileChooserParams: FileChooserParams): Boolean { + activityContract?.openFileChooser(filePathCallback, fileChooserParams) ?: webView.snackbar("File chooser not found") + return activityContract != null } override fun onGeolocationPermissionsShowPrompt(origin: String, callback: GeolocationPermissions.Callback) { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt index 709ab7ac..62c28527 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt @@ -11,10 +11,7 @@ import com.pitchedapps.frost.SelectorActivity import com.pitchedapps.frost.facebook.FACEBOOK_COM import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.injectors.* -import com.pitchedapps.frost.utils.L -import com.pitchedapps.frost.utils.Prefs -import com.pitchedapps.frost.utils.cookies -import com.pitchedapps.frost.utils.launchNewTask +import com.pitchedapps.frost.utils.* import io.reactivex.subjects.Subject /** @@ -76,8 +73,20 @@ open class FrostWebViewClient(val webCore: FrostWebViewCore) : WebViewClient() { L.d("Emit $flag") } - override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest?): Boolean { - L.i("Url Loading ${request?.url?.path}") + /** + * Helper to format the request and launch it + * returns true to override the url + */ + private fun launchRequest(request: WebResourceRequest): Boolean { + L.d("Launching ${request.url}") + webCore.context.launchWebOverlay(request.url.toString()) + return true + } + + override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { + L.i("Url Loading ${request.url}") + val path = request.url.path + if (path.startsWith("/composer/")) return launchRequest(request) return super.shouldOverrideUrlLoading(view, request) } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewSearch.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewSearch.kt index 83dccb9a..fb2e1851 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewSearch.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewSearch.kt @@ -16,6 +16,7 @@ import com.pitchedapps.frost.injectors.jsInject import com.pitchedapps.frost.utils.L import io.reactivex.schedulers.Schedulers import io.reactivex.subjects.PublishSubject +import org.jetbrains.anko.runOnUiThread import org.jsoup.Jsoup import java.util.concurrent.TimeUnit @@ -61,8 +62,11 @@ class FrostWebViewSearch(context: Context, val contract: SearchContract) : WebVi Jsoup.parse(it).select("a:not([rel*='keywords(']):not([href=#])[rel]").map { element -> //split text into separate items + L.i("Search element ${element.attr("href")}") val texts = element.select("div").map { (it.ownText()) }.filter { it.isNotBlank() } - Pair(texts, element.attr("href")) + val pair = Pair(texts, element.attr("href")) + L.i("Search element potential $pair") + pair }.filter { it.first.isNotEmpty() } } .filter { content -> Pair(content.lastOrNull()?.second, content.size) != previousResult } @@ -72,7 +76,6 @@ class FrostWebViewSearch(context: Context, val contract: SearchContract) : WebVi L.d("Search element count ${content.size}") contract.emitSearchResponse(content.map { (texts, href) -> - L.i("Search element $texts $href") SearchItem(href, texts[0], texts.getOrNull(1)) }) } @@ -86,7 +89,7 @@ class FrostWebViewSearch(context: Context, val contract: SearchContract) : WebVi var pauseLoad: Boolean get() = settings.blockNetworkLoads set(value) { - settings.blockNetworkLoads = value + context.runOnUiThread { settings.blockNetworkLoads = value } } override fun reload() { @@ -97,8 +100,9 @@ class FrostWebViewSearch(context: Context, val contract: SearchContract) : WebVi * Sets the input to have our given text, then dispatches the input event so the webpage recognizes it */ fun query(input: String) { + pauseLoad = false L.d("Searching attempt", input) - JsBuilder().js("var e=document.getElementById('main-search-input');if(e){e.value='$input';var n=new Event('input',{bubbles:!0,cancelable:!0});e.dispatchEvent(n)}else console.log('Input field not found')").build().inject(this) + JsBuilder().js("var e=document.getElementById('main-search-input');if(e){e.value='$input';var n=new Event('input',{bubbles:!0,cancelable:!0});e.dispatchEvent(n),e.dispatchEvent(new Event('focus'))}else console.log('Input field not found');").build().inject(this) } /** @@ -118,7 +122,8 @@ class FrostWebViewSearch(context: Context, val contract: SearchContract) : WebVi inner class SearchJSI { @JavascriptInterface fun handleHtml(html: String) { - L.d("Search received response") + L.d("Search received response ${contract.isSearchOpened}") + if (!contract.isSearchOpened) pauseLoad = true searchSubject.onNext(html) } @@ -127,7 +132,6 @@ class FrostWebViewSearch(context: Context, val contract: SearchContract) : WebVi when (flag) { 0 -> { L.d("Search loaded successfully") - if (!contract.isSearchOpened) pauseLoad = true } 1 -> { //something is not found in the search view; this is effectively useless L.eThrow("Search subject error; reverting to full overlay") |