aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClientMenu.kt
blob: 0f08bcf348285f4dae90a4bd68f448599d893107 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.pitchedapps.frost.web

import android.graphics.Bitmap
import android.webkit.WebView
import com.pitchedapps.frost.facebook.FB_URL_BASE
import com.pitchedapps.frost.injectors.JsAssets
import com.pitchedapps.frost.injectors.jsInject
import com.pitchedapps.frost.utils.L
import io.reactivex.subjects.Subject

/**
 * Created by Allan Wang on 2017-05-31.
 */
class FrostWebViewClientMenu(webCore: FrostWebViewCore) : FrostWebViewClient(webCore) {

    var content: String? = null
    val progressObservable: Subject<Int> = webCore.progressObservable
    private val contentBaseUrl = "${FB_URL_BASE}notifications"

    override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
        super.onPageStarted(view, url, favicon)
        if (content != null) {
            when (url.removePrefix(FB_URL_BASE)) {
                "settings",
                "settings#",
                "settings#!/settings?soft=bookmarks" -> {
                    L.d("Load from stored $url")
                    view.stopLoading()
                    view.loadDataWithBaseURL(contentBaseUrl, content, "text/html", "utf-8", "https://google.ca/test")
                }
            }
        }
    }

    override fun onPageFinished(view: WebView, url: String) {
        super.onPageFinished(view, url)
        if (url == webCore.baseUrl && content == null) {
            jsInject(JsAssets.MENU)
        }
    }

    override fun emit(flag: Int) {
        super.emit(flag)
        super.injectAndFinish()
    }

    override fun onPageFinishedActions(url: String?) {
        when (url?.removePrefix(FB_URL_BASE)) {
            "settings",
            "settings#",
            "settings#!/settings?soft=bookmarks" -> {
                //do nothing; we will further inject before revealing
            }
            else -> injectAndFinish()
        }
    }

    override fun handleHtml(html: String) {
        super.handleHtml(html)
        content = html //we will not save this locally in case things change
    }
}