Webview enhancements

- Pull to refresh
- Loading progress
- Share page
This commit is contained in:
arkon 2020-01-16 21:43:10 -05:00
parent 0d7f84857c
commit 73fbc81067
3 changed files with 79 additions and 15 deletions

View File

@ -7,19 +7,17 @@ import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.webkit.WebChromeClient
import android.webkit.WebView import android.webkit.WebView
import androidx.core.graphics.ColorUtils import androidx.core.graphics.ColorUtils
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
import eu.kanade.tachiyomi.util.WebViewClientCompat import eu.kanade.tachiyomi.util.*
import eu.kanade.tachiyomi.util.getResourceColor import kotlinx.android.synthetic.main.webview_activity.*
import kotlinx.android.synthetic.main.webview_activity.toolbar
import kotlinx.android.synthetic.main.webview_activity.webview
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
class WebViewActivity : BaseActivity() { class WebViewActivity : BaseActivity() {
private val sourceManager by injectLazy<SourceManager>() private val sourceManager by injectLazy<SourceManager>()
@ -41,11 +39,27 @@ class WebViewActivity : BaseActivity() {
super.onBackPressed() super.onBackPressed()
} }
swipe_refresh.isEnabled = false
swipe_refresh.setOnRefreshListener {
refreshPage()
}
if (bundle == null) { if (bundle == null) {
val source = sourceManager.get(intent.extras!!.getLong(SOURCE_KEY)) as? HttpSource ?: return val source = sourceManager.get(intent.extras!!.getLong(SOURCE_KEY)) as? HttpSource ?: return
val url = intent.extras!!.getString(URL_KEY) ?: return val url = intent.extras!!.getString(URL_KEY) ?: return
val headers = source.headers.toMultimap().mapValues { it.value.getOrNull(0) ?: "" } val headers = source.headers.toMultimap().mapValues { it.value.getOrNull(0) ?: "" }
webview.webChromeClient = object : WebChromeClient() {
override fun onProgressChanged(view: WebView?, newProgress: Int) {
progress_bar.visible()
progress_bar.progress = newProgress
if (newProgress == 100) {
progress_bar.invisible()
}
super.onProgressChanged(view, newProgress)
}
}
webview.webViewClient = object : WebViewClientCompat() { webview.webViewClient = object : WebViewClientCompat() {
override fun shouldOverrideUrlCompat(view: WebView, url: String): Boolean { override fun shouldOverrideUrlCompat(view: WebView, url: String): Boolean {
view.loadUrl(url) view.loadUrl(url)
@ -56,12 +70,21 @@ class WebViewActivity : BaseActivity() {
super.onPageFinished(view, url) super.onPageFinished(view, url)
invalidateOptionsMenu() invalidateOptionsMenu()
title = view?.title title = view?.title
swipe_refresh.isEnabled = true
swipe_refresh?.isRefreshing = false
} }
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon) super.onPageStarted(view, url, favicon)
invalidateOptionsMenu() invalidateOptionsMenu()
} }
override fun onPageCommitVisible(view: WebView?, url: String?) {
super.onPageCommitVisible(view, url)
// Reset to top when page refreshes
nested_view.scrollTo(0, 0)
}
} }
webview.settings.javaScriptEnabled = true webview.settings.javaScriptEnabled = true
webview.settings.userAgentString = source.headers["User-Agent"] webview.settings.userAgentString = source.headers["User-Agent"]
@ -98,11 +121,29 @@ class WebViewActivity : BaseActivity() {
when (item.itemId) { when (item.itemId) {
R.id.action_web_back -> webview.goBack() R.id.action_web_back -> webview.goBack()
R.id.action_web_forward -> webview.goForward() R.id.action_web_forward -> webview.goForward()
R.id.action_web_refresh -> webview.reload() R.id.action_web_refresh -> refreshPage()
R.id.action_web_share -> shareWebpage()
} }
return super.onOptionsItemSelected(item) return super.onOptionsItemSelected(item)
} }
private fun refreshPage() {
swipe_refresh.isRefreshing = true
webview.reload()
}
private fun shareWebpage() {
try {
val intent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, webview.url)
}
startActivity(Intent.createChooser(intent, getString(R.string.action_share)))
} catch (e: Exception) {
toast(e.message)
}
}
companion object { companion object {
private const val SOURCE_KEY = "source_key" private const val SOURCE_KEY = "source_key"
private const val URL_KEY = "url_key" private const val URL_KEY = "url_key"

View File

@ -17,21 +17,40 @@
android:layout_height="?attr/actionBarSize" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme" android:theme="?attr/actionBarTheme"
app:navigationIcon="@drawable/ic_close_white_24dp" app:layout_scrollFlags="scroll|enterAlways|snap"
app:layout_scrollFlags="scroll|enterAlways|snap" /> app:navigationIcon="@drawable/ic_close_white_24dp" />
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@id/swipe_refresh"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"> app:layout_behavior="@string/appbar_scrolling_view_behavior">
<WebView <androidx.core.widget.NestedScrollView
android:id="@+id/webview" android:id="@+id/nested_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</androidx.core.widget.NestedScrollView> <WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="2dp"
android:progressBackgroundTint="@color/colorPrimary"
android:progressTint="@color/colorPrimary" />
</WebView>
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -16,8 +16,12 @@
<item <item
android:id="@+id/action_web_refresh" android:id="@+id/action_web_refresh"
android:icon="@drawable/ic_refresh_white_24dp"
android:title="@string/action_webview_refresh" android:title="@string/action_webview_refresh"
app:showAsAction="ifRoom" /> app:showAsAction="never" />
<item
android:id="@+id/action_web_share"
android:title="@string/action_share"
app:showAsAction="never" />
</menu> </menu>