From e7606e6dca48f7a20c998398b976332a904106db Mon Sep 17 00:00:00 2001 From: inorichi Date: Mon, 8 Apr 2019 02:08:40 +0200 Subject: [PATCH] Add option to open manga details in a WebView --- .../ui/manga/info/MangaInfoController.kt | 21 +++++--- .../ui/manga/info/MangaWebViewController.kt | 51 +++++++++++++++++++ .../res/layout/manga_info_web_controller.xml | 7 +++ app/src/main/res/menu/manga_info.xml | 6 ++- app/src/main/res/values/strings.xml | 1 + 5 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaWebViewController.kt create mode 100644 app/src/main/res/layout/manga_info_web_controller.xml diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt index cb9b091faf..f2a0f412ef 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt @@ -15,12 +15,7 @@ import android.support.customtabs.CustomTabsIntent import android.support.v4.content.pm.ShortcutInfoCompat import android.support.v4.content.pm.ShortcutManagerCompat import android.support.v4.graphics.drawable.IconCompat -import android.view.LayoutInflater -import android.view.Menu -import android.view.MenuInflater -import android.view.MenuItem -import android.view.View -import android.view.ViewGroup +import android.view.* import android.widget.Toast import com.afollestad.materialdialogs.MaterialDialog import com.bumptech.glide.load.engine.DiskCacheStrategy @@ -138,6 +133,7 @@ class MangaInfoController : NucleusController(), override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.action_open_in_browser -> openInBrowser() + R.id.action_open_in_web_view -> openInWebView() R.id.action_share -> shareManga() R.id.action_add_to_home_screen -> addToHomeScreen() else -> return super.onOptionsItemSelected(item) @@ -302,6 +298,19 @@ class MangaInfoController : NucleusController(), } } + private fun openInWebView() { + val source = presenter.source as? HttpSource ?: return + + val url = try { + source.mangaDetailsRequest(presenter.manga).url().toString() + } catch (e: Exception) { + return + } + + parentController?.router?.pushController(MangaWebViewController(source.id, url) + .withFadeTransaction()) + } + /** * Called to run Intent with [Intent.ACTION_SEND], which show share dialog. */ diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaWebViewController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaWebViewController.kt new file mode 100644 index 0000000000..15a85a1080 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaWebViewController.kt @@ -0,0 +1,51 @@ +package eu.kanade.tachiyomi.ui.manga.info + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.webkit.WebView +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.source.SourceManager +import eu.kanade.tachiyomi.source.online.HttpSource +import eu.kanade.tachiyomi.ui.base.controller.BaseController +import uy.kohesive.injekt.injectLazy + +class MangaWebViewController(bundle: Bundle? = null) : BaseController(bundle) { + + private val sourceManager by injectLazy() + + constructor(sourceId: Long, url: String) : this(Bundle().apply { + putLong(SOURCE_KEY, sourceId) + putString(URL_KEY, url) + }) + + override fun inflateView(inflater: LayoutInflater, container: ViewGroup): View { + return inflater.inflate(R.layout.manga_info_web_controller, container, false) + } + + override fun onViewCreated(view: View) { + super.onViewCreated(view) + val source = sourceManager.get(args.getLong(SOURCE_KEY)) as? HttpSource ?: return + val url = args.getString(URL_KEY) ?: return + val headers = source.headers.toMultimap().mapValues { it.value.getOrNull(0) ?: "" } + + val web = view as WebView + web.settings.javaScriptEnabled = true + web.settings.userAgentString = source.headers["User-Agent"] + web.loadUrl(url, headers) + } + + override fun onDestroyView(view: View) { + val web = view as WebView + web.stopLoading() + web.destroy() + super.onDestroyView(view) + } + + private companion object { + const val SOURCE_KEY = "source_key" + const val URL_KEY = "url_key" + } + +} diff --git a/app/src/main/res/layout/manga_info_web_controller.xml b/app/src/main/res/layout/manga_info_web_controller.xml new file mode 100644 index 0000000000..6d52f5e22a --- /dev/null +++ b/app/src/main/res/layout/manga_info_web_controller.xml @@ -0,0 +1,7 @@ + + + + diff --git a/app/src/main/res/menu/manga_info.xml b/app/src/main/res/menu/manga_info.xml index 76f105d1da..6a5edad194 100644 --- a/app/src/main/res/menu/manga_info.xml +++ b/app/src/main/res/menu/manga_info.xml @@ -12,8 +12,12 @@ android:title="@string/action_open_in_browser" app:showAsAction="never"/> + + - \ No newline at end of file + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f19427f625..2388bdc1f8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -73,6 +73,7 @@ Resume Move Open in browser + Open in web view Add to home screen Change display mode Display