diff options
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt | 59 |
1 files changed, 30 insertions, 29 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt b/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt index 04b392ed..b7bf3421 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt @@ -1,6 +1,5 @@ package com.pitchedapps.frost.fragments -import android.annotation.SuppressLint import android.content.Context import android.os.Bundle import android.support.v4.app.Fragment @@ -25,29 +24,23 @@ class WebFragment : Fragment() { companion object { private const val ARG_URL = "arg_url" private const val ARG_URL_ENUM = "arg_url_enum" - operator fun invoke(url: String) = WebFragment().withBundle { - putString(ARG_URL, url) - } + private const val ARG_POSITION = "arg_position" - operator fun invoke(data: FbTab) = WebFragment().withBundle { + operator fun invoke(data: FbTab, position: Int) = WebFragment().withBundle { putString(ARG_URL, data.url) + putInt(ARG_POSITION, position) putSerializable(ARG_URL_ENUM, data) } } // val refresh: SwipeRefreshLayout by lazy { frostWebView.refresh } val web: FrostWebViewCore by lazy { frostWebView.web } - lateinit var url: String - var urlEnum: FbTab? = null + val url: String by lazy { arguments.getString(ARG_URL) } + val urlEnum: FbTab by lazy { arguments.getSerializable(ARG_URL_ENUM) as FbTab } + val position: Int by lazy { arguments.getInt(ARG_POSITION) } lateinit private var frostWebView: FrostWebView private var firstLoad = true - private var refreshDisposable: Disposable? = null - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - url = arguments.getString(ARG_URL) - urlEnum = arguments.getSerializable(ARG_URL_ENUM) as? FbTab - } + private var activityDisposable: Disposable? = null override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { super.onCreateView(inflater, container, savedInstanceState) @@ -75,37 +68,45 @@ class WebFragment : Fragment() { override fun onAttach(context: Context) { super.onAttach(context) - refreshDisposable?.dispose() - if (context is MainActivity) - refreshDisposable = context.refreshObservable.observeOn(AndroidSchedulers.mainThread()).subscribe { - clearHistory -> - if (clearHistory) web.clearHistory() - web.loadBaseUrl() + activityDisposable?.dispose() + if (context is MainActivity) { + activityDisposable = context.webFragmentObservable.observeOn(AndroidSchedulers.mainThread()).subscribe { + /** + * Execute actions based on flags + * Flags between -10 and 10 are reserved for viewpager events + */ + when (it) { + MainActivity.FRAGMENT_REFRESH -> { + web.clearHistory() + web.loadBaseUrl(true) + } + position -> { + context.toolbar.setTitle(urlEnum.titleId) + pauseLoad = false + } + -(position + 1) -> { //we are moving away from this fragment + pauseLoad = true + } + } } + } } override fun onDetach() { - refreshDisposable?.dispose() + activityDisposable?.dispose() super.onDetach() } - @SuppressLint("SetJavaScriptEnabled") override fun onResume() { super.onResume() pauseLoad = false firstLoad() } - override fun onPause() { - pauseLoad = true - super.onPause() - } - var pauseLoad: Boolean get() = web.settings.blockNetworkLoads set(value) { - if (urlEnum != FbTab.MENU) - web.settings.blockNetworkLoads = value + web.settings.blockNetworkLoads = value } |