Refactor CustomTabsIntent creation

This commit is contained in:
arkon 2020-12-13 22:30:27 -05:00
parent c87ba6231d
commit 2cc2a90941
2 changed files with 24 additions and 20 deletions

View File

@ -2,7 +2,6 @@ package eu.kanade.tachiyomi.ui.setting
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import androidx.browser.customtabs.CustomTabsIntent
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.TrackManager
@ -20,7 +19,7 @@ import eu.kanade.tachiyomi.util.preference.onClick
import eu.kanade.tachiyomi.util.preference.preferenceCategory import eu.kanade.tachiyomi.util.preference.preferenceCategory
import eu.kanade.tachiyomi.util.preference.switchPreference import eu.kanade.tachiyomi.util.preference.switchPreference
import eu.kanade.tachiyomi.util.preference.titleRes import eu.kanade.tachiyomi.util.preference.titleRes
import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.openInBrowser
import eu.kanade.tachiyomi.widget.preference.LoginPreference import eu.kanade.tachiyomi.widget.preference.LoginPreference
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
@ -47,11 +46,9 @@ class SettingsTrackingController :
startActivity(MyAnimeListLoginActivity.newIntent(activity!!)) startActivity(MyAnimeListLoginActivity.newIntent(activity!!))
} }
trackPreference(trackManager.aniList) { trackPreference(trackManager.aniList) {
val tabsIntent = CustomTabsIntent.Builder() activity?.openInBrowser(AnilistApi.authUrl(), trackManager.aniList.getLogoColor()) {
.setToolbarColor(context.getResourceColor(R.attr.colorPrimary)) intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
.build() }
tabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
tabsIntent.launchUrl(activity!!, AnilistApi.authUrl())
} }
trackPreference(trackManager.kitsu) { trackPreference(trackManager.kitsu) {
val dialog = TrackLoginDialog(trackManager.kitsu, R.string.email) val dialog = TrackLoginDialog(trackManager.kitsu, R.string.email)
@ -59,18 +56,14 @@ class SettingsTrackingController :
dialog.showDialog(router) dialog.showDialog(router)
} }
trackPreference(trackManager.shikimori) { trackPreference(trackManager.shikimori) {
val tabsIntent = CustomTabsIntent.Builder() activity?.openInBrowser(ShikimoriApi.authUrl(), trackManager.shikimori.getLogoColor()) {
.setToolbarColor(context.getResourceColor(R.attr.colorPrimary)) intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
.build() }
tabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
tabsIntent.launchUrl(activity!!, ShikimoriApi.authUrl())
} }
trackPreference(trackManager.bangumi) { trackPreference(trackManager.bangumi) {
val tabsIntent = CustomTabsIntent.Builder() activity?.openInBrowser(BangumiApi.authUrl(), trackManager.bangumi.getLogoColor()) {
.setToolbarColor(context.getResourceColor(R.attr.colorPrimary)) intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
.build() }
tabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
tabsIntent.launchUrl(activity!!, BangumiApi.authUrl())
} }
} }
preferenceCategory { preferenceCategory {

View File

@ -13,12 +13,14 @@ import android.content.pm.PackageManager
import android.content.res.Resources import android.content.res.Resources
import android.graphics.Color import android.graphics.Color
import android.net.ConnectivityManager import android.net.ConnectivityManager
import android.net.Uri
import android.os.PowerManager import android.os.PowerManager
import android.view.View import android.view.View
import android.widget.Toast import android.widget.Toast
import androidx.annotation.AttrRes import androidx.annotation.AttrRes
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.browser.customtabs.CustomTabColorSchemeParams
import androidx.browser.customtabs.CustomTabsIntent import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
@ -224,12 +226,21 @@ fun Context.isServiceRunning(serviceClass: Class<*>): Boolean {
/** /**
* Opens a URL in a custom tab. * Opens a URL in a custom tab.
*/ */
fun Context.openInBrowser(url: String) { fun Context.openInBrowser(url: String, @ColorInt toolbarColor: Int? = null, block: CustomTabsIntent.() -> Unit = {}) {
this.openInBrowser(url.toUri(), toolbarColor, block)
}
fun Context.openInBrowser(uri: Uri, @ColorInt toolbarColor: Int? = null, block: CustomTabsIntent.() -> Unit = {}) {
try { try {
val intent = CustomTabsIntent.Builder() val intent = CustomTabsIntent.Builder()
.setToolbarColor(getResourceColor(R.attr.colorPrimary)) .setDefaultColorSchemeParams(
CustomTabColorSchemeParams.Builder()
.setToolbarColor(toolbarColor ?: getResourceColor(R.attr.colorPrimary))
.build()
)
.build() .build()
intent.launchUrl(this, url.toUri()) block(intent)
intent.launchUrl(this, uri)
} catch (e: Exception) { } catch (e: Exception) {
toast(e.message) toast(e.message)
} }