From 68aca55e6f8e2b506a2b5000c32a8bfd9aed75e4 Mon Sep 17 00:00:00 2001 From: Eugene Date: Tue, 16 Apr 2019 11:34:52 -0400 Subject: [PATCH] Add options to open catalogue in browser/webview (#1979) --- .../browse/BrowseCatalogueController.kt | 25 +++++++++++++++++++ .../ui/manga/info/MangaInfoController.kt | 11 ++------ .../tachiyomi/util/ContextExtensions.kt | 18 +++++++++++++ app/src/main/res/menu/catalogue_list.xml | 11 ++++++++ 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt index 80f284204f..97cfd76e16 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt @@ -17,11 +17,13 @@ import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.model.FilterList +import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.ui.base.controller.NucleusController import eu.kanade.tachiyomi.ui.base.controller.SecondaryDrawerController import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction import eu.kanade.tachiyomi.ui.library.ChangeMangaCategoriesDialog import eu.kanade.tachiyomi.ui.manga.MangaController +import eu.kanade.tachiyomi.ui.manga.info.MangaWebViewController import eu.kanade.tachiyomi.util.* import eu.kanade.tachiyomi.widget.AutofitRecyclerView import kotlinx.android.synthetic.main.catalogue_controller.* @@ -259,15 +261,38 @@ open class BrowseCatalogueController(bundle: Bundle) : } } + override fun onPrepareOptionsMenu(menu: Menu) { + super.onPrepareOptionsMenu(menu) + + val isHttpSource = presenter.source is HttpSource + menu.findItem(R.id.action_open_in_browser).isVisible = isHttpSource + menu.findItem(R.id.action_open_in_web_view).isVisible = isHttpSource + } + override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.action_display_mode -> swapDisplayMode() R.id.action_set_filter -> navView?.let { activity?.drawer?.openDrawer(Gravity.END) } + R.id.action_open_in_browser -> openInBrowser() + R.id.action_open_in_web_view -> openInWebView() else -> return super.onOptionsItemSelected(item) } return true } + private fun openInBrowser() { + val source = presenter.source as? HttpSource ?: return + + activity?.openInBrowser(source.baseUrl) + } + + private fun openInWebView() { + val source = presenter.source as? HttpSource ?: return + + router.pushController(MangaWebViewController(source.id, source.baseUrl) + .withFadeTransaction()) + } + /** * Restarts the request with a new query. * 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 f2a0f412ef..b3bb48289a 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 @@ -42,6 +42,7 @@ import eu.kanade.tachiyomi.ui.library.ChangeMangaCategoriesDialog import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.util.getResourceColor +import eu.kanade.tachiyomi.util.openInBrowser import eu.kanade.tachiyomi.util.snack import eu.kanade.tachiyomi.util.toast import eu.kanade.tachiyomi.util.truncateCenter @@ -287,15 +288,7 @@ class MangaInfoController : NucleusController(), val context = view?.context ?: return val source = presenter.source as? HttpSource ?: return - try { - val url = Uri.parse(source.mangaDetailsRequest(presenter.manga).url().toString()) - val intent = CustomTabsIntent.Builder() - .setToolbarColor(context.getResourceColor(R.attr.colorPrimary)) - .build() - intent.launchUrl(activity, url) - } catch (e: Exception) { - context.toast(e.message) - } + context.openInBrowser(source.mangaDetailsRequest(presenter.manga).url().toString()) } private fun openInWebView() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/ContextExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/ContextExtensions.kt index d89b3ce5a4..2c54b236c5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/ContextExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/ContextExtensions.kt @@ -10,14 +10,17 @@ import android.content.IntentFilter import android.content.pm.PackageManager import android.content.res.Resources import android.net.ConnectivityManager +import android.net.Uri import android.os.PowerManager import android.support.annotation.AttrRes import android.support.annotation.StringRes +import android.support.customtabs.CustomTabsIntent import android.support.v4.app.NotificationCompat import android.support.v4.content.ContextCompat import android.support.v4.content.LocalBroadcastManager import android.widget.Toast import com.nononsenseapps.filepicker.FilePickerActivity +import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.widget.CustomLayoutPickerActivity /** @@ -163,3 +166,18 @@ fun Context.isServiceRunning(serviceClass: Class<*>): Boolean { return manager.getRunningServices(Integer.MAX_VALUE) .any { className == it.service.className } } + +/** + * Opens a URL in a custom tab. + */ +fun Context.openInBrowser(url: String) { + try { + val url = Uri.parse(url) + val intent = CustomTabsIntent.Builder() + .setToolbarColor(getResourceColor(R.attr.colorPrimary)) + .build() + intent.launchUrl(this, url) + } catch (e: Exception) { + toast(e.message) + } +} diff --git a/app/src/main/res/menu/catalogue_list.xml b/app/src/main/res/menu/catalogue_list.xml index ff4373df40..1b75d4ae75 100644 --- a/app/src/main/res/menu/catalogue_list.xml +++ b/app/src/main/res/menu/catalogue_list.xml @@ -19,4 +19,15 @@ android:id="@+id/action_display_mode" android:title="@string/action_display_mode" app:showAsAction="ifRoom"/> + + + + +