aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-06-03 00:22:10 -0700
committerAllan Wang <me@allanwang.ca>2017-06-03 00:22:10 -0700
commit5796566137995c8d244720f87ba85bce0e0d2f00 (patch)
tree1736e12182c7cf2769ad953029dc6a645b803802 /app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt
parentbc197ad5769ee792d930ac5b634e1e9000230689 (diff)
downloadfrost-5796566137995c8d244720f87ba85bce0e0d2f00.tar.gz
frost-5796566137995c8d244720f87ba85bce0e0d2f00.tar.bz2
frost-5796566137995c8d244720f87ba85bce0e0d2f00.zip
css updates and beginning login migration
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt101
1 files changed, 101 insertions, 0 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt
new file mode 100644
index 00000000..6e0cf201
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt
@@ -0,0 +1,101 @@
+package com.pitchedapps.frost.web
+
+import android.annotation.SuppressLint
+import android.content.Context
+import android.util.AttributeSet
+import android.view.View
+import android.webkit.*
+import com.pitchedapps.frost.dbflow.CookieModel
+import com.pitchedapps.frost.facebook.FACEBOOK_COM
+import com.pitchedapps.frost.facebook.FbCookie
+import com.pitchedapps.frost.injectors.CssAssets
+import com.pitchedapps.frost.utils.L
+import com.pitchedapps.frost.views.circularReveal
+import com.pitchedapps.frost.views.snackbar
+import io.reactivex.subjects.PublishSubject
+import io.reactivex.subjects.SingleSubject
+import io.reactivex.subjects.Subject
+
+/**
+ * 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 LoginWebView @JvmOverloads constructor(
+ context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
+) : WebView(context, attrs, defStyleAttr) {
+
+ companion object {
+ const val LOGIN_URL = "https://touch.facebook.com/login"
+ private val userMatcher: Regex by lazy { Regex("c_user=([0-9]*);") }
+ }
+
+ val cookieObservable = PublishSubject.create<Pair<String, String?>>()
+ lateinit var loginObservable: SingleSubject<CookieModel>
+ lateinit var progressObservable: Subject<Int>
+
+ init {
+ cookieObservable.filter { (_, cookie) -> cookie?.contains(userMatcher) ?: false }
+ .subscribe {
+ (url, cookie) ->
+ L.d("Checking cookie for $url\n\t$cookie")
+ val id = userMatcher.find(cookie!!)?.groups?.get(1)?.value
+ if (id != null) {
+ try {
+ FbCookie.save(id.toLong(), -1)
+ //TODO proceed to next view
+ cookieObservable.onComplete()
+ } catch (e: NumberFormatException) {
+ //todo send report that id has changed
+ }
+ }
+ }
+ setupWebview()
+ }
+
+ @SuppressLint("SetJavaScriptEnabled")
+ fun setupWebview() {
+ settings.javaScriptEnabled = true
+ setLayerType(View.LAYER_TYPE_HARDWARE, null)
+ setWebViewClient(LoginClient())
+ setWebChromeClient(LoginChromeClient())
+ }
+
+ fun loadLogin() {
+ loadUrl(LOGIN_URL)
+ }
+
+
+ inner class LoginClient : WebViewClient() {
+
+ override fun onPageFinished(view: WebView, url: String) {
+ super.onPageFinished(view, url)
+ if (!url.contains(FACEBOOK_COM)) {
+ view.snackbar("No longer under facebook; refreshing...")
+ loadLogin()
+ return
+ }
+ CssAssets.LOGIN.inject(view, {
+ if (view.visibility == View.INVISIBLE)
+ view.circularReveal(offset = 150L)
+ })
+ cookieObservable.onNext(Pair(url, CookieManager.getInstance().getCookie(url)))
+ }
+ }
+
+ inner class LoginChromeClient : WebChromeClient() {
+ override fun onConsoleMessage(consoleMessage: ConsoleMessage): Boolean {
+ L.d("Login Console ${consoleMessage.lineNumber()}: ${consoleMessage.message()}")
+ return super.onConsoleMessage(consoleMessage)
+ }
+
+ override fun onProgressChanged(view: WebView, newProgress: Int) {
+ super.onProgressChanged(view, newProgress)
+ progressObservable.onNext(newProgress)
+ }
+ }
+} \ No newline at end of file