aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragments.kt
blob: 3cac92afc2b7c94adce74a37c9f9001be5f63c57 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright 2018 Allan Wang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.fragments

import android.webkit.WebView
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import com.pitchedapps.frost.R
import com.pitchedapps.frost.contracts.MainFabContract
import com.pitchedapps.frost.facebook.FbItem
import com.pitchedapps.frost.injectors.JsActions
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.views.FrostWebView
import com.pitchedapps.frost.web.FrostWebViewClient
import com.pitchedapps.frost.web.FrostWebViewClientMenu
import com.pitchedapps.frost.web.FrostWebViewClientMessenger

/**
 * Created by Allan Wang on 27/12/17.
 *
 * Basic webfragment
 * Do not extend as this is always a fallback
 */
class WebFragment : BaseFragment() {

    override val layoutRes: Int = R.layout.view_content_web

    /**
     * Given a webview, output a client
     */
    fun client(web: FrostWebView) = when (baseEnum) {
        FbItem.MESSENGER -> FrostWebViewClientMessenger(web)
        FbItem.MENU -> FrostWebViewClientMenu(web)
        else -> FrostWebViewClient(web)
    }

    override fun updateFab(contract: MainFabContract) {
        val web = core as? WebView
        if (web == null) {
            L.e { "Webview not found in fragment $baseEnum" }
            return super.updateFab(contract)
        }
        if (baseEnum.isFeed && prefs.showCreateFab) {
            contract.showFab(GoogleMaterial.Icon.gmd_edit) {
                JsActions.CREATE_POST.inject(web, prefs)
            }
            return
        }
        super.updateFab(contract)
    }

    override fun onBackPressed(): Boolean {
        if (baseEnum == FbItem.MENU) {
            val core = core
            val web = core as? WebView
            if (web != null && web.canGoBack() && !web.canGoBackOrForward(-2)) {
                // If menu item + we are at the second last entry, reload the base
                // To properly inflate the menu
                // Related to https://github.com/AllanWang/Frost-for-Facebook/issues/1593
                core.clearHistory()
                core.reloadBase(true)
                return true
            }
        }
        return super.onBackPressed()
    }
}