aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/web
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-05-31 17:11:46 -0700
committerAllan Wang <me@allanwang.ca>2017-05-31 17:11:46 -0700
commit8618670b82641d5fbaec9c333f1290bab429ce27 (patch)
tree737c9a04f108ea68547eef2db1ae6e96caa64df6 /app/src/main/kotlin/com/pitchedapps/frost/web
parent9a41937a33539dbfaae4d072361caaec79865c29 (diff)
downloadfrost-8618670b82641d5fbaec9c333f1290bab429ce27.tar.gz
frost-8618670b82641d5fbaec9c333f1290bab429ce27.tar.bz2
frost-8618670b82641d5fbaec9c333f1290bab429ce27.zip
add more cookie handling
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/web')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebView.kt139
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt48
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/web/SwipeRefreshBase.kt30
3 files changed, 217 insertions, 0 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebView.kt
new file mode 100644
index 00000000..27312763
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebView.kt
@@ -0,0 +1,139 @@
+package com.pitchedapps.frost.web
+
+import android.annotation.SuppressLint
+import android.content.Context
+import android.support.v4.view.MotionEventCompat
+import android.support.v4.view.NestedScrollingChild
+import android.support.v4.view.NestedScrollingChildHelper
+import android.support.v4.view.ViewCompat
+import android.util.AttributeSet
+import android.view.MotionEvent
+import android.view.View
+import android.webkit.WebView
+import com.pitchedapps.frost.events.WebEvent
+import com.pitchedapps.frost.utils.ObservableContainer
+import io.reactivex.subjects.BehaviorSubject
+import io.reactivex.subjects.Subject
+import org.greenrobot.eventbus.EventBus
+import org.greenrobot.eventbus.Subscribe
+
+enum class WebStatus {
+ LOADING, LOADED, ERROR
+}
+
+/**
+ * Created by Allan Wang on 2017-05-29.
+ *
+ * Courtesy of takahirom
+ *
+ * https://github.com/takahirom/webview-in-coordinatorlayout/blob/master/app/src/main/java/com/github/takahirom/webview_in_coodinator_layout/NestedWebView.java
+ */
+class FrostWebView @JvmOverloads constructor(
+ context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
+) : WebView(context, attrs, defStyleAttr), NestedScrollingChild, ObservableContainer<WebStatus> {
+
+ private val childHelper = NestedScrollingChildHelper(this)
+ private var lastY: Int = 0
+ private val scrollOffset = IntArray(2)
+ private val scrollConsumed = IntArray(2)
+ private var nestedOffsetY: Int = 0
+ override val observable: Subject<WebStatus> //TODO see if we need this
+ var baseUrl: String? = null
+
+ init {
+ isNestedScrollingEnabled = true
+ observable = BehaviorSubject.create<WebStatus>()
+ setupWebview()
+ }
+
+ @SuppressLint("SetJavaScriptEnabled")
+ fun setupWebview() {
+ settings.javaScriptEnabled = true
+ setLayerType(View.LAYER_TYPE_HARDWARE, null)
+ setWebViewClient(FrostWebViewClient(observable))
+ }
+
+ override fun loadUrl(url: String?) {
+ if (url != null)
+ super.loadUrl(url)
+ }
+
+ override fun onTouchEvent(ev: MotionEvent): Boolean {
+ val event = MotionEvent.obtain(ev)
+ val action = MotionEventCompat.getActionMasked(event)
+ if (action == MotionEvent.ACTION_DOWN)
+ nestedOffsetY = 0
+ val eventY = event.y.toInt()
+ event.offsetLocation(0f, nestedOffsetY.toFloat())
+ val returnValue: Boolean
+ when (action) {
+ MotionEvent.ACTION_MOVE -> {
+ var deltaY = lastY - eventY
+ // NestedPreScroll
+ if (dispatchNestedPreScroll(0, deltaY, scrollConsumed, scrollOffset)) {
+ deltaY -= scrollConsumed[1]
+ event.offsetLocation(0f, -scrollOffset[1].toFloat())
+ nestedOffsetY += scrollOffset[1]
+ }
+ lastY = eventY - scrollOffset[1]
+ returnValue = super.onTouchEvent(event)
+ // NestedScroll
+ if (dispatchNestedScroll(0, scrollOffset[1], 0, deltaY, scrollOffset)) {
+ event.offsetLocation(0f, scrollOffset[1].toFloat())
+ nestedOffsetY += scrollOffset[1]
+ lastY -= scrollOffset[1]
+ }
+ }
+ MotionEvent.ACTION_DOWN -> {
+ returnValue = super.onTouchEvent(event)
+ lastY = eventY
+ startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL)
+ }
+ MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
+ returnValue = super.onTouchEvent(event)
+ stopNestedScroll()
+ }
+ else -> return false
+ }
+ return returnValue
+ }
+
+ override fun onAttachedToWindow() {
+ super.onAttachedToWindow()
+ EventBus.getDefault().register(this);
+ }
+
+ override fun onDetachedFromWindow() {
+ EventBus.getDefault().unregister(this)
+ super.onDetachedFromWindow()
+ }
+
+ @Subscribe
+ fun webEvent(event: WebEvent) = event.execute(this)
+
+ // Nested Scroll implements
+ override fun setNestedScrollingEnabled(enabled: Boolean) {
+ childHelper.isNestedScrollingEnabled = enabled
+ }
+
+ override fun isNestedScrollingEnabled() = childHelper.isNestedScrollingEnabled
+
+ override fun startNestedScroll(axes: Int) = childHelper.startNestedScroll(axes)
+
+ override fun stopNestedScroll() = childHelper.stopNestedScroll()
+
+ override fun hasNestedScrollingParent() = childHelper.hasNestedScrollingParent()
+
+ override fun dispatchNestedScroll(dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, offsetInWindow: IntArray?)
+ = childHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow)
+
+ override fun dispatchNestedPreScroll(dx: Int, dy: Int, consumed: IntArray?, offsetInWindow: IntArray?)
+ = childHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow)
+
+ override fun dispatchNestedFling(velocityX: Float, velocityY: Float, consumed: Boolean)
+ = childHelper.dispatchNestedFling(velocityX, velocityY, consumed)
+
+ override fun dispatchNestedPreFling(velocityX: Float, velocityY: Float)
+ = childHelper.dispatchNestedPreFling(velocityX, velocityY)
+
+} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt
new file mode 100644
index 00000000..14fcc22a
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClient.kt
@@ -0,0 +1,48 @@
+package com.pitchedapps.frost.web
+
+import android.graphics.Bitmap
+import android.webkit.*
+import com.pitchedapps.frost.facebook.FbCookie
+import com.pitchedapps.frost.utils.L
+import io.reactivex.subjects.Subject
+
+/**
+ * Created by Allan Wang on 2017-05-31.
+ */
+class FrostWebViewClient(val observable: Subject<WebStatus>) : WebViewClient() {
+
+ private var injectionCount: Int = 0
+
+ companion object {
+ //Collections of jewels mapped with url match -> id
+ val jewelMap: Map<String, String> = mapOf("a" to "b")
+ fun test() {
+
+ }
+ }
+
+ override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
+ super.onReceivedError(view, request, error)
+ observable.onNext(WebStatus.ERROR)
+ L.e("FWV Error ${request}")
+ }
+
+ override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
+ super.onPageStarted(view, url, favicon)
+ injectionCount = 0
+ observable.onNext(WebStatus.LOADING)
+ L.d("FWV Loading $url")
+ if (url.contains("logout.php")) FbCookie.logout()
+ }
+
+ override fun onPageFinished(view: WebView, url: String) {
+ super.onPageFinished(view, url)
+ observable.onNext(WebStatus.LOADED)
+ FbCookie.checkUserId(url, CookieManager.getInstance().getCookie(url))
+ }
+
+ fun logout() {
+
+ }
+
+} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/SwipeRefreshBase.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/SwipeRefreshBase.kt
new file mode 100644
index 00000000..0e303e49
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/web/SwipeRefreshBase.kt
@@ -0,0 +1,30 @@
+package com.pitchedapps.frost.web
+
+import android.content.Context
+import android.support.v4.widget.SwipeRefreshLayout
+import android.util.AttributeSet
+import android.view.MotionEvent
+import android.webkit.WebView
+import com.pitchedapps.frost.utils.Utils
+
+
+/**
+ * Created by Allan Wang on 2017-05-28.
+ */
+class SwipeRefreshBase @JvmOverloads constructor(
+ context: Context?, attrs: AttributeSet? = null
+) : SwipeRefreshLayout(context, attrs) {
+
+ lateinit var shouldSwipe: (ev: MotionEvent) -> Boolean
+
+ companion object {
+ private val SCROLL_BUFFER by lazy { Utils.dpToPx(5) }
+ fun shouldScroll(webview: WebView) = webview.scrollY <= SCROLL_BUFFER
+ }
+
+// override fun onInterceptTouchEvent(ev: MotionEvent):Boolean {
+// val b = shouldSwipe.invoke(ev) && super.onInterceptTouchEvent(ev)
+// L.e("Should swipe $b")
+// return b
+// }
+} \ No newline at end of file