Use Voyager for WebView in non-reader places
This commit is contained in:
@@ -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" }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user