Use Voyager for WebView in non-reader places
(cherry picked from commit 6efcb8ccfae0a19ae1c3c866620c8dd5222eb4b3) # Conflicts: # app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreen.kt
This commit is contained in:
parent
aea9f36eff
commit
c9bc4646da
@ -31,7 +31,7 @@ import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.util.system.setDefaultSettings
|
||||
|
||||
@Composable
|
||||
fun WebViewScreen(
|
||||
fun WebViewScreenContent(
|
||||
onNavigateUp: () -> Unit,
|
||||
initialTitle: String?,
|
||||
url: String,
|
@ -25,7 +25,7 @@ import eu.kanade.tachiyomi.source.online.HttpSource
|
||||
import eu.kanade.tachiyomi.ui.browse.migration.advanced.process.MigrationListScreen
|
||||
import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourceScreenModel
|
||||
import eu.kanade.tachiyomi.ui.manga.MangaScreen
|
||||
import eu.kanade.tachiyomi.ui.webview.WebViewActivity
|
||||
import eu.kanade.tachiyomi.ui.webview.WebViewScreen
|
||||
import eu.kanade.tachiyomi.util.Constants
|
||||
|
||||
data class SourceSearchScreen(
|
||||
@ -87,8 +87,13 @@ data class SourceSearchScreen(
|
||||
contentPadding = paddingValues,
|
||||
onWebViewClick = {
|
||||
val source = screenModel.source as? HttpSource ?: return@BrowseSourceContent
|
||||
val intent = WebViewActivity.newIntent(context, source.baseUrl, source.id, source.name)
|
||||
context.startActivity(intent)
|
||||
navigator.push(
|
||||
WebViewScreen(
|
||||
url = source.baseUrl,
|
||||
initialTitle = source.name,
|
||||
sourceId = source.id,
|
||||
),
|
||||
)
|
||||
},
|
||||
onHelpClick = { uriHandler.openUri(Constants.URL_HELP) },
|
||||
onLocalSourceHelpClick = { uriHandler.openUri(LocalSource.HELP_URL) },
|
||||
|
@ -58,7 +58,7 @@ import eu.kanade.tachiyomi.ui.browse.source.SourcesScreen
|
||||
import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourceScreenModel.Listing
|
||||
import eu.kanade.tachiyomi.ui.category.CategoryScreen
|
||||
import eu.kanade.tachiyomi.ui.manga.MangaScreen
|
||||
import eu.kanade.tachiyomi.ui.webview.WebViewActivity
|
||||
import eu.kanade.tachiyomi.ui.webview.WebViewScreen
|
||||
import eu.kanade.tachiyomi.util.Constants
|
||||
import eu.kanade.tachiyomi.util.lang.launchIO
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
@ -114,8 +114,13 @@ data class BrowseSourceScreen(
|
||||
|
||||
val onWebViewClick = f@{
|
||||
val source = screenModel.source as? HttpSource ?: return@f
|
||||
val intent = WebViewActivity.newIntent(context, source.baseUrl, source.id, source.name)
|
||||
context.startActivity(intent)
|
||||
navigator.push(
|
||||
WebViewScreen(
|
||||
url = source.baseUrl,
|
||||
initialTitle = source.name,
|
||||
sourceId = source.id,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
LaunchedEffect(screenModel.source) {
|
||||
|
@ -58,7 +58,7 @@ import eu.kanade.tachiyomi.ui.home.HomeScreen
|
||||
import eu.kanade.tachiyomi.ui.manga.merged.EditMergedSettingsDialog
|
||||
import eu.kanade.tachiyomi.ui.manga.track.TrackInfoDialogHomeScreen
|
||||
import eu.kanade.tachiyomi.ui.reader.ReaderActivity
|
||||
import eu.kanade.tachiyomi.ui.webview.WebViewActivity
|
||||
import eu.kanade.tachiyomi.ui.webview.WebViewScreen
|
||||
import eu.kanade.tachiyomi.util.lang.launchUI
|
||||
import eu.kanade.tachiyomi.util.lang.withIOContext
|
||||
import eu.kanade.tachiyomi.util.lang.withNonCancellableContext
|
||||
@ -151,9 +151,9 @@ class MangaScreen(
|
||||
// SY -->
|
||||
onWebViewClicked = {
|
||||
if (successState.mergedData == null) {
|
||||
openMangaInWebView(context, screenModel.manga, screenModel.source)
|
||||
openMangaInWebView(navigator, screenModel.manga, screenModel.source)
|
||||
} else {
|
||||
openMergedMangaWebview(context, successState.mergedData)
|
||||
openMergedMangaWebview(context, navigator, successState.mergedData)
|
||||
}
|
||||
}.takeIf { isHttpSource },
|
||||
// SY <--
|
||||
@ -331,10 +331,15 @@ class MangaScreen(
|
||||
}
|
||||
}
|
||||
|
||||
private fun openMangaInWebView(context: Context, manga_: Manga?, source_: Source?) {
|
||||
private fun openMangaInWebView(navigator: Navigator, manga_: Manga?, source_: Source?) {
|
||||
getMangaUrl(manga_, source_)?.let { url ->
|
||||
val intent = WebViewActivity.newIntent(context, url, source_?.id, manga_?.title)
|
||||
context.startActivity(intent)
|
||||
navigator.push(
|
||||
WebViewScreen(
|
||||
url = url,
|
||||
initialTitle = manga_?.title,
|
||||
sourceId = source_?.id,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -434,7 +439,7 @@ class MangaScreen(
|
||||
navigator.push(MetadataViewScreen(manga.id, manga.source))
|
||||
}
|
||||
|
||||
private fun openMergedMangaWebview(context: Context, mergedMangaData: MergedMangaData) {
|
||||
private fun openMergedMangaWebview(context: Context, navigator: Navigator, mergedMangaData: MergedMangaData) {
|
||||
val sourceManager: SourceManager = Injekt.get()
|
||||
val mergedManga = mergedMangaData.manga.values.filterNot { it.source == MERGED_SOURCE_ID }
|
||||
val sources = mergedManga.map { sourceManager.getOrStub(it.source) }
|
||||
@ -445,7 +450,7 @@ class MangaScreen(
|
||||
-1,
|
||||
) { dialog, index ->
|
||||
dialog.dismiss()
|
||||
openMangaInWebView(context, mergedManga[index], sources[index] as? HttpSource)
|
||||
openMangaInWebView(navigator, mergedManga[index], sources[index] as? HttpSource)
|
||||
}
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
.show()
|
||||
|
@ -6,7 +6,7 @@ import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.widget.Toast
|
||||
import androidx.core.net.toUri
|
||||
import eu.kanade.presentation.webview.WebViewScreen
|
||||
import eu.kanade.presentation.webview.WebViewScreenContent
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.network.NetworkHelper
|
||||
import eu.kanade.tachiyomi.source.SourceManager
|
||||
@ -45,13 +45,13 @@ class WebViewActivity : BaseActivity() {
|
||||
val url = intent.extras?.getString(URL_KEY) ?: return
|
||||
assistUrl = url
|
||||
|
||||
var headers = mutableMapOf<String, String>()
|
||||
var headers = emptyMap<String, String>()
|
||||
(sourceManager.get(intent.extras!!.getLong(SOURCE_KEY)) as? HttpSource)?.let { source ->
|
||||
headers = source.headers.toMultimap().mapValues { it.value.getOrNull(0) ?: "" }.toMutableMap()
|
||||
headers = source.headers.toMultimap().mapValues { it.value.getOrNull(0) ?: "" }
|
||||
}
|
||||
|
||||
setComposeContent {
|
||||
WebViewScreen(
|
||||
WebViewScreenContent(
|
||||
onNavigateUp = { finish() },
|
||||
initialTitle = intent.extras?.getString(TITLE_KEY),
|
||||
url = url,
|
||||
|
@ -0,0 +1,42 @@
|
||||
package eu.kanade.tachiyomi.ui.webview
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import cafe.adriel.voyager.core.model.rememberScreenModel
|
||||
import cafe.adriel.voyager.core.screen.Screen
|
||||
import cafe.adriel.voyager.core.screen.uniqueScreenKey
|
||||
import cafe.adriel.voyager.navigator.LocalNavigator
|
||||
import cafe.adriel.voyager.navigator.currentOrThrow
|
||||
import eu.kanade.presentation.util.AssistContentScreen
|
||||
import eu.kanade.presentation.webview.WebViewScreenContent
|
||||
|
||||
class WebViewScreen(
|
||||
private val url: String,
|
||||
private val initialTitle: String? = null,
|
||||
private val sourceId: Long? = null,
|
||||
) : Screen, AssistContentScreen {
|
||||
|
||||
private var assistUrl: String? = null
|
||||
|
||||
override val key = uniqueScreenKey
|
||||
|
||||
override fun onProvideAssistUrl() = assistUrl
|
||||
|
||||
@Composable
|
||||
override fun Content() {
|
||||
val navigator = LocalNavigator.currentOrThrow
|
||||
val context = LocalContext.current
|
||||
val screenModel = rememberScreenModel { WebViewScreenModel(sourceId) }
|
||||
|
||||
WebViewScreenContent(
|
||||
onNavigateUp = { navigator.pop() },
|
||||
initialTitle = initialTitle,
|
||||
url = url,
|
||||
headers = screenModel.headers,
|
||||
onUrlChange = { assistUrl = it },
|
||||
onShare = { screenModel.shareWebpage(context, it) },
|
||||
onOpenInBrowser = { screenModel.openInBrowser(context, it) },
|
||||
onClearCookies = screenModel::clearCookies,
|
||||
)
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package eu.kanade.tachiyomi.ui.webview
|
||||
|
||||
import android.content.Context
|
||||
import androidx.core.net.toUri
|
||||
import cafe.adriel.voyager.core.model.StateScreenModel
|
||||
import eu.kanade.presentation.more.stats.StatsScreenState
|
||||
import eu.kanade.tachiyomi.network.NetworkHelper
|
||||
import eu.kanade.tachiyomi.source.SourceManager
|
||||
import eu.kanade.tachiyomi.source.online.HttpSource
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import eu.kanade.tachiyomi.util.system.openInBrowser
|
||||
import eu.kanade.tachiyomi.util.system.toShareIntent
|
||||
import eu.kanade.tachiyomi.util.system.toast
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import uy.kohesive.injekt.Injekt
|
||||
import uy.kohesive.injekt.api.get
|
||||
|
||||
class WebViewScreenModel(
|
||||
val sourceId: Long?,
|
||||
private val sourceManager: SourceManager = Injekt.get(),
|
||||
private val network: NetworkHelper = Injekt.get(),
|
||||
) : StateScreenModel<StatsScreenState>(StatsScreenState.Loading) {
|
||||
|
||||
var headers = emptyMap<String, String>()
|
||||
|
||||
init {
|
||||
sourceId?.let { sourceManager.get(it) as? HttpSource }?.let { source ->
|
||||
headers = source.headers.toMultimap().mapValues { it.value.getOrNull(0) ?: "" }
|
||||
}
|
||||
}
|
||||
|
||||
fun shareWebpage(context: Context, url: String) {
|
||||
try {
|
||||
context.startActivity(url.toUri().toShareIntent(context, type = "text/plain"))
|
||||
} catch (e: Exception) {
|
||||
context.toast(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
fun openInBrowser(context: Context, url: String) {
|
||||
context.openInBrowser(url, forceDefaultBrowser = true)
|
||||
}
|
||||
|
||||
fun clearCookies(url: String) {
|
||||
val cleared = network.cookieManager.remove(url.toHttpUrl())
|
||||
logcat { "Cleared $cleared cookies for: $url" }
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user