Improve Igneous cookie handling

This commit is contained in:
Jobobby04 2022-12-01 14:18:52 -05:00
parent 6402258c83
commit 51c5f29b25
3 changed files with 98 additions and 31 deletions

View File

@ -9,6 +9,7 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
@ -22,6 +23,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@ -105,7 +107,7 @@ fun EhLoginWebViewScreen(
}
}
}
var showAdvancedOptions by remember {
var showAdvancedOptions by rememberSaveable {
mutableStateOf(false)
}
@ -114,7 +116,9 @@ fun EhLoginWebViewScreen(
WebView(
state = state,
navigator = navigator,
modifier = Modifier.padding(bottom = 48.dp),
modifier = Modifier
.fillMaxSize()
.padding(bottom = 48.dp),
onCreated = { webView ->
webView.setDefaultSettings()
@ -143,7 +147,11 @@ fun EhLoginWebViewScreen(
}
}
if (showAdvancedOptions) {
Box(Modifier.background(Color(0xb5000000))) {
Box(
Modifier
.fillMaxSize()
.background(Color(0xb5000000))
) {
Dialog(onDismissRequest = { showAdvancedOptions = false }) {
fun loadUrl(url: String) {
state.content = WebContent.Url(url)

View File

@ -0,0 +1,61 @@
package eu.kanade.presentation.webview.components
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.window.DialogProperties
import eu.kanade.tachiyomi.R
@Composable
fun IgneousDialog(
onDismissRequest: () -> Unit,
onIgneousSet: (String) -> Unit,
) {
var textFieldValue by rememberSaveable(stateSaver = TextFieldValue.Saver) {
mutableStateOf(TextFieldValue(""))
}
AlertDialog(
onDismissRequest = onDismissRequest,
title = { Text(text = stringResource(R.string.custom_igneous_cookie)) },
text = {
Column {
Text(text = stringResource(R.string.custom_igneous_cookie_message))
OutlinedTextField(
value = textFieldValue,
onValueChange = { textFieldValue = it },
singleLine = true,
modifier = Modifier.fillMaxWidth(),
)
}
},
properties = DialogProperties(
usePlatformDefaultWidth = true,
),
confirmButton = {
TextButton(
onClick = {
onIgneousSet(textFieldValue.text.trim())
onDismissRequest()
},
) {
Text(text = stringResource(android.R.string.ok))
}
},
dismissButton = {
TextButton(onClick = onDismissRequest) {
Text(text = stringResource(R.string.action_cancel))
}
},
)
}

View File

@ -7,15 +7,18 @@ import android.os.Bundle
import android.webkit.CookieManager
import android.webkit.WebView
import android.widget.Toast
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import eu.kanade.domain.UnsortedPreferences
import eu.kanade.presentation.webview.EhLoginWebViewScreen
import eu.kanade.presentation.webview.components.IgneousDialog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
import eu.kanade.tachiyomi.util.system.WebViewUtil
import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.util.view.setComposeContent
import eu.kanade.tachiyomi.widget.materialdialogs.setTextInput
import exh.log.xLogD
import uy.kohesive.injekt.injectLazy
import java.net.HttpCookie
@ -27,8 +30,6 @@ import java.util.Locale
class EhLoginActivity : BaseActivity() {
private val preferenceManager: UnsortedPreferences by injectLazy()
private var igneous: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -39,14 +40,28 @@ class EhLoginActivity : BaseActivity() {
}
setComposeContent {
var igneous by rememberSaveable {
mutableStateOf<String?>(null)
}
var igneousDialogOpen by rememberSaveable {
mutableStateOf(false)
}
EhLoginWebViewScreen(
onUp = { finish() },
onPageFinished = ::onPageFinished,
onPageFinished = { view, url -> onPageFinished(view, url, igneous) },
onClickRecheckLoginStatus = ::recheckLoginStatus,
onClickAlternateLoginPage = ::alternateLoginPage,
onClickSkipPageRestyling = ::skipPageRestyling,
onClickCustomIgneousCookie = ::openIgneousDialog,
onClickCustomIgneousCookie = { igneousDialogOpen = true },
)
if (igneousDialogOpen) {
IgneousDialog(
onDismissRequest = { igneousDialogOpen = false },
onIgneousSet = { igneous = it },
)
}
}
}
@ -62,24 +77,7 @@ class EhLoginActivity : BaseActivity() {
loadUrl("https://forums.e-hentai.org/index.php?act=Login&$PARAM_SKIP_INJECT=true")
}
private fun openIgneousDialog() {
var igneous: CharSequence? = null
MaterialAlertDialogBuilder(this)
.setTitle(R.string.custom_igneous_cookie)
.setMessage(R.string.custom_igneous_cookie_message)
.setTextInput { igneousText ->
igneous = igneousText
}
.setPositiveButton(android.R.string.ok) { _, _ ->
if (!igneous.isNullOrBlank()) {
this.igneous = igneous?.toString()?.trim()
}
}
.setNegativeButton(android.R.string.cancel, null)
.show()
}
private fun onPageFinished(view: WebView, url: String) {
private fun onPageFinished(view: WebView, url: String, customIgneous: String?) {
xLogD(url)
val parsedUrl = Uri.parse(url)
if (parsedUrl.host.equals("forums.e-hentai.org", ignoreCase = true)) {
@ -94,7 +92,7 @@ class EhLoginActivity : BaseActivity() {
}
} else if (parsedUrl.host.equals("exhentai.org", ignoreCase = true)) {
// At ExHentai, check that everything worked out...
if (applyExHentaiCookies(url)) {
if (applyExHentaiCookies(url, customIgneous)) {
preferenceManager.enableExhentai().set(true)
setResult(RESULT_OK)
finish()
@ -121,18 +119,18 @@ class EhLoginActivity : BaseActivity() {
/**
* Parse cookies at ExHentai
*/
private fun applyExHentaiCookies(url: String): Boolean {
private fun applyExHentaiCookies(url: String, customIgneous: String?): Boolean {
getCookies(url)?.let { parsed ->
var memberId: String? = null
var passHash: String? = null
var igneous: String? = null
var igneous: String? = customIgneous
parsed.forEach {
when (it.name.lowercase(Locale.getDefault())) {
MEMBER_ID_COOKIE -> memberId = it.value
PASS_HASH_COOKIE -> passHash = it.value
IGNEOUS_COOKIE -> igneous = this.igneous ?: it.value
IGNEOUS_COOKIE -> igneous = customIgneous ?: it.value
}
}