Skip to content

Commit

Permalink
Refine refresh igneous
Browse files Browse the repository at this point in the history
  • Loading branch information
xb2016 committed Sep 28, 2024
1 parent 7fee990 commit 65a9c50
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 216 deletions.
118 changes: 63 additions & 55 deletions app/src/main/java/com/hippo/ehviewer/preference/AccountPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,97 +19,105 @@ import android.content.Context
import android.content.DialogInterface
import android.text.Html
import android.util.AttributeSet
import android.view.View
import android.view.WindowManager
import android.widget.Button
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope
import com.hippo.ehviewer.R
import com.hippo.ehviewer.client.EhCookieStore
import com.hippo.ehviewer.client.EhEngine
import com.hippo.ehviewer.client.EhUrl
import com.hippo.ehviewer.client.EhUtils
import com.hippo.ehviewer.ui.SettingsActivity
import com.hippo.ehviewer.ui.scene.BaseScene
import com.hippo.preference.MessagePreference
import com.hippo.preference.DialogPreference
import com.hippo.util.ReadableTime
import com.hippo.util.addTextToClipboard
import com.hippo.util.launchIO
import com.hippo.util.withUIContext
import okhttp3.Cookie
import okhttp3.HttpUrl.Companion.toHttpUrl
import java.util.LinkedList

class AccountPreference(
context: Context,
attrs: AttributeSet? = null,
) : MessagePreference(context, attrs) {
) : DialogPreference(context, attrs), View.OnClickListener {
private val mActivity = context as SettingsActivity
private var message: String? = null

init {
updateMessage()
}
private var mCookie: String? = null
private var mMessage: String? = context.getString(R.string.settings_eh_account_name_tourist)
private lateinit var mDialog: AlertDialog
private lateinit var refreshButton: Button

private fun updateMessage() {
val eCookies = EhCookieStore.getCookies(EhUrl.HOST_E.toHttpUrl())
val exCookies = EhCookieStore.getCookies(EhUrl.HOST_EX.toHttpUrl())
val cookies: MutableList<Cookie> = LinkedList(eCookies)
cookies.addAll(exCookies)
var ipbMemberId: String? = null
var ipbPassHash: String? = null
var igneous: String? = null
cookies.forEach {
when (it.name) {
EhCookieStore.KEY_IPB_MEMBER_ID -> ipbMemberId = it.value
EhCookieStore.KEY_IPB_PASS_HASH -> ipbPassHash = it.value
EhCookieStore.KEY_IGNEOUS -> igneous = it.value
if (EhCookieStore.hasSignedIn()) {
var ipbMemberId: String? = null
var ipbPassHash: String? = null
var igneous: String? = null
var igneousExpire = 0L

EhCookieStore.getCookies(EhUrl.HOST_EX.toHttpUrl()).forEach {
when (it.name) {
EhCookieStore.KEY_IPB_MEMBER_ID -> ipbMemberId = it.value
EhCookieStore.KEY_IPB_PASS_HASH -> ipbPassHash = it.value
EhCookieStore.KEY_IGNEOUS -> {
igneous = it.value
igneousExpire = it.expiresAt
}
}
}
}
if (ipbMemberId != null && ipbPassHash != null) {
message = (
EhCookieStore.KEY_IPB_MEMBER_ID + ": " + ipbMemberId + "<br>" +
EhCookieStore.KEY_IPB_PASS_HASH + ": " + ipbPassHash + "<br>" +
EhCookieStore.KEY_IGNEOUS + ": " + igneous
)
setDialogMessage(
Html.fromHtml(
context.getString(
R.string.settings_eh_account_identity_cookies,
message,
),
Html.FROM_HTML_MODE_LEGACY,
),
)
message = message!!.replace("<br>", "\n")
} else {
setDialogMessage(context.getString(R.string.settings_eh_account_name_tourist))
mCookie = EhCookieStore.KEY_IPB_MEMBER_ID + ": " + ipbMemberId +
"<br>" + EhCookieStore.KEY_IPB_PASS_HASH + ": " + ipbPassHash
igneous?.let { mCookie += "<br>" + EhCookieStore.KEY_IGNEOUS + ": " + it }
mMessage = Html.fromHtml(
context.getString(R.string.settings_eh_account_identity_cookies, mCookie),
Html.FROM_HTML_MODE_LEGACY,
).toString()
if (igneousExpire > 0 && igneousExpire != ReadableTime.MAX_VALUE_MILLIS) {
mMessage += "\n" + context.getString(R.string.settings_eh_account_igneous_expire) +
ReadableTime.getShortTime(igneousExpire)
}
mCookie = mCookie!!.replace("<br>", "\n")
mDialog.setMessage(mMessage)
}
}

override fun onPrepareDialogBuilder(builder: AlertDialog.Builder) {
super.onPrepareDialogBuilder(builder)
if (message != null) {
if (EhCookieStore.hasSignedIn()) {
builder.setNeutralButton(R.string.settings_eh_account_identity_cookies_copy) { dialog: DialogInterface, which: Int ->
mActivity.addTextToClipboard(message, true)
mActivity.addTextToClipboard(mCookie, true)
this@AccountPreference.onClick(dialog, which)
}
builder.setNegativeButton(R.string.settings_eh_account_clear_igneous) { _: DialogInterface?, _: Int ->
mActivity.lifecycleScope.launchIO {
EhCookieStore.deleteCookie(
EhUrl.HOST_EX.toHttpUrl(),
EhCookieStore.KEY_IGNEOUS,
)
withUIContext {
updateMessage()
}
}
}
builder.setNegativeButton(R.string.settings_eh_account_refresh_igneous, null)
}
builder.setPositiveButton(R.string.settings_eh_account_sign_out) { _: DialogInterface?, _: Int ->
builder.setPositiveButton(R.string.settings_eh_account_sign_out) { _: DialogInterface, _: Int ->
EhUtils.signOut()
mActivity.showTip(R.string.settings_eh_account_sign_out_tip, BaseScene.LENGTH_SHORT)
}
builder.setMessage(mMessage)
}

override fun onDialogCreated(dialog: AlertDialog) {
super.onDialogCreated(dialog)
dialog.window!!.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
mDialog = dialog
refreshButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE)
refreshButton.setOnClickListener(this)
mDialog.window!!.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
updateMessage()
}

override fun onClick(v: View) {
refreshButton.isEnabled = false
mActivity.lifecycleScope.launchIO {
EhCookieStore.deleteCookie(
EhUrl.HOST_EX.toHttpUrl(),
EhCookieStore.KEY_IGNEOUS,
)
runCatching { EhEngine.getUConfig(EhUrl.URL_UCONFIG_EX) }
withUIContext {
updateMessage()
refreshButton.isEnabled = true
}
}
}
}
71 changes: 0 additions & 71 deletions app/src/main/java/com/hippo/preference/MessagePreference.kt

This file was deleted.

Loading

0 comments on commit 65a9c50

Please sign in to comment.