diff --git a/app/src/main/java/com/hippo/ehviewer/ui/compose/CookieSignInScreen.kt b/app/src/main/java/com/hippo/ehviewer/ui/compose/CookieSignInScreen.kt index fa56d58a0..a8bb52d9f 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/compose/CookieSignInScreen.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/compose/CookieSignInScreen.kt @@ -55,7 +55,6 @@ import com.hippo.ehviewer.client.EhCookieStore import com.hippo.ehviewer.client.EhUrl import com.hippo.ehviewer.client.EhUtils import com.hippo.ehviewer.ui.LoginActivity.Companion.SELECT_SITE_ROUTE_NAME -import com.hippo.ehviewer.ui.scene.getProfile import com.hippo.util.ExceptionUtils import com.hippo.util.getClipboardManager import com.hippo.util.getTextFromClipboard diff --git a/app/src/main/java/com/hippo/ehviewer/ui/compose/GetProfile.kt b/app/src/main/java/com/hippo/ehviewer/ui/compose/GetProfile.kt new file mode 100644 index 000000000..7c2908ae0 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/ui/compose/GetProfile.kt @@ -0,0 +1,14 @@ +package com.hippo.ehviewer.ui.compose + +import com.hippo.ehviewer.Settings +import com.hippo.ehviewer.client.EhClient +import com.hippo.ehviewer.client.parser.ProfileParser + +suspend fun getProfile() { + runCatching { + (EhClient.execute(EhClient.METHOD_GET_PROFILE) as ProfileParser.Result).run { + Settings.putDisplayName(displayName) + Settings.putAvatar(avatar) + } + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/ui/compose/SelectSiteScreen.kt b/app/src/main/java/com/hippo/ehviewer/ui/compose/SelectSiteScreen.kt index adc17fb9b..ad2d3e90e 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/compose/SelectSiteScreen.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/compose/SelectSiteScreen.kt @@ -108,6 +108,7 @@ fun SelectSiteScreen(finish: () -> Unit) { ) { Button(onClick = { Settings.putSelectSite(false) + Settings.putNeedSignIn(false) if (!site) Settings.putGallerySite(EhUrl.SITE_E) else diff --git a/app/src/main/java/com/hippo/ehviewer/ui/compose/SignInScreen.kt b/app/src/main/java/com/hippo/ehviewer/ui/compose/SignInScreen.kt index 63f68f5b6..c3803e061 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/compose/SignInScreen.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/compose/SignInScreen.kt @@ -61,7 +61,6 @@ import com.hippo.ehviewer.client.EhUtils import com.hippo.ehviewer.ui.LoginActivity.Companion.COOKIE_SIGN_IN_ROUTE_NAME import com.hippo.ehviewer.ui.LoginActivity.Companion.SELECT_SITE_ROUTE_NAME import com.hippo.ehviewer.ui.LoginActivity.Companion.WEBVIEW_SIGN_IN_ROUTE_NAME -import com.hippo.ehviewer.ui.scene.getProfile import com.hippo.util.ExceptionUtils import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.withUIContext diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/CookieSignInScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/CookieSignInScene.kt deleted file mode 100644 index 6e0f8005e..000000000 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/CookieSignInScene.kt +++ /dev/null @@ -1,216 +0,0 @@ -/* - * Copyright 2016 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.hippo.ehviewer.ui.scene - -import android.graphics.Paint -import android.os.Bundle -import android.text.TextUtils -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import com.hippo.app.BaseDialogBuilder -import com.hippo.ehviewer.EhApplication.Companion.ehCookieStore -import com.hippo.ehviewer.R -import com.hippo.ehviewer.client.EhClient -import com.hippo.ehviewer.client.EhCookieStore -import com.hippo.ehviewer.client.EhRequest -import com.hippo.ehviewer.client.EhUrl -import com.hippo.ehviewer.client.EhUtils -import com.hippo.ehviewer.databinding.SceneCookieSignInBinding -import com.hippo.util.ExceptionUtils -import com.hippo.util.getClipboardManager -import com.hippo.util.getTextFromClipboard -import eu.kanade.tachiyomi.util.lang.launchIO -import okhttp3.Cookie -import java.util.Locale - -class CookieSignInScene : BaseScene() { - private var _binding: SceneCookieSignInBinding? = null - private val binding - get() = _binding!! - - override fun needShowLeftDrawer(): Boolean { - return false - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - showSoftInput(binding.ipbMemberId) - } - - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, savedInstanceState: Bundle? - ): View { - _binding = SceneCookieSignInBinding.inflate(inflater, container, false) - binding.fromClipboard.run { - paintFlags = paintFlags or Paint.UNDERLINE_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG - } - binding.ipbPassHash.setOnEditorActionListener { _, _, _ -> - enter() - true - } - binding.ok.setOnClickListener { - enter() - } - binding.fromClipboard.setOnClickListener { - fillCookiesFromClipboard() - } - - // Try to get old version cookie info - val sharedPreferences = requireContext().getSharedPreferences("eh_info", 0) - val ipbMemberId = sharedPreferences.getString("ipb_member_id", null) - val ipbPassHash = sharedPreferences.getString("ipb_pass_hash", null) - val igneous = sharedPreferences.getString("igneous", null) - var getIt = false - if (!TextUtils.isEmpty(ipbMemberId)) { - binding.ipbMemberId.setText(ipbMemberId) - getIt = true - } - if (!TextUtils.isEmpty(ipbPassHash)) { - binding.ipbPassHash.setText(ipbPassHash) - getIt = true - } - if (!TextUtils.isEmpty(igneous)) { - binding.igneous.setText(igneous) - getIt = true - } - if (getIt) { - showTip(R.string.found_cookies, LENGTH_SHORT) - } - return binding.root - } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } - - fun enter() { - val ipbMemberId = binding.ipbMemberId.text.toString().trim { it <= ' ' } - val ipbPassHash = binding.ipbPassHash.text.toString().trim { it <= ' ' } - val igneous = binding.igneous.text.toString().trim { it <= ' ' } - if (TextUtils.isEmpty(ipbMemberId)) { - binding.ipbMemberIdLayout.error = getString(R.string.text_is_empty) - return - } else { - binding.ipbMemberIdLayout.error = null - } - if (TextUtils.isEmpty(ipbPassHash)) { - binding.ipbPassHashLayout.error = getString(R.string.text_is_empty) - return - } else { - binding.ipbPassHashLayout.error = null - } - hideSoftInput() - storeCookie(ipbMemberId, ipbPassHash, igneous) - val request = EhRequest() - .setMethod(EhClient.METHOD_GET_PROFILE) - .setCallback(CookieSignInListener()) - request.enqueue(this) - } - - private fun storeCookie(id: String, hash: String, igneous: String) { - EhUtils.signOut() - val store = ehCookieStore - store.addCookie(newCookie(EhCookieStore.KEY_IPD_MEMBER_ID, id, EhUrl.DOMAIN_E)) - store.addCookie(newCookie(EhCookieStore.KEY_IPD_MEMBER_ID, id, EhUrl.DOMAIN_EX)) - store.addCookie(newCookie(EhCookieStore.KEY_IPD_PASS_HASH, hash, EhUrl.DOMAIN_E)) - store.addCookie(newCookie(EhCookieStore.KEY_IPD_PASS_HASH, hash, EhUrl.DOMAIN_EX)) - if (igneous.isNotEmpty()) { - store.addCookie(newCookie(EhCookieStore.KEY_IGNEOUS, igneous, EhUrl.DOMAIN_E)) - store.addCookie(newCookie(EhCookieStore.KEY_IGNEOUS, igneous, EhUrl.DOMAIN_EX)) - } - } - - private fun fillCookiesFromClipboard() { - val context = requireContext() - hideSoftInput() - val text = context.getClipboardManager().getTextFromClipboard(context) - if (text == null) { - showTip(R.string.from_clipboard_error, LENGTH_SHORT) - return - } - try { - val kvs: Array = if (text.contains(";")) { - text.split(";".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - } else if (text.contains("\n")) { - text.split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - } else { - showTip(R.string.from_clipboard_error, LENGTH_SHORT) - return - } - if (kvs.size < 3) { - showTip(R.string.from_clipboard_error, LENGTH_SHORT) - return - } - for (s in kvs) { - val kv: Array = if (s.contains("=")) { - s.split("=".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - } else if (s.contains(":")) { - s.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - } else { - continue - } - if (kv.size != 2) { - continue - } - when (kv[0].trim { it <= ' ' }.lowercase(Locale.getDefault())) { - "ipb_member_id" -> binding.ipbMemberId.setText(kv[1].trim { it <= ' ' }) - "ipb_pass_hash" -> binding.ipbPassHash.setText(kv[1].trim { it <= ' ' }) - "igneous" -> binding.igneous.setText(kv[1].trim { it <= ' ' }) - } - } - enter() - } catch (e: Exception) { - ExceptionUtils.throwIfFatal(e) - e.printStackTrace() - showTip(R.string.from_clipboard_error, LENGTH_SHORT) - } - } - - private inner class CookieSignInListener : EhClient.Callback { - override fun onSuccess(result: Any?) { - launchIO { - getProfile() - } - navigateToTop() - } - - override fun onFailure(e: Exception) { - ehCookieStore.signOut() - BaseDialogBuilder(requireContext()).setTitle(R.string.sign_in_failed) - .setMessage( - """ - ${ExceptionUtils.getReadableString(e)} - ${getString(R.string.wrong_cookie_warning)} - """.trimIndent() - ) - .setPositiveButton(R.string.get_it, null).show() - } - - override fun onCancel() { - ehCookieStore.signOut() - } - } - - companion object { - private fun newCookie(name: String, value: String, domain: String): Cookie { - return Cookie.Builder().name(name).value(value) - .domain(domain).expiresAt(Long.MAX_VALUE).build() - } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/SelectSiteScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/SelectSiteScene.kt deleted file mode 100644 index 017d61eee..000000000 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/SelectSiteScene.kt +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2016 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.hippo.ehviewer.ui.scene - -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import com.hippo.ehviewer.EhApplication.Companion.ehCookieStore -import com.hippo.ehviewer.R -import com.hippo.ehviewer.Settings -import com.hippo.ehviewer.client.EhUrl -import com.hippo.ehviewer.databinding.SceneSelectSiteBinding - -class SelectSiteScene : BaseScene() { - private var _binding: SceneSelectSiteBinding? = null - private val binding - get() = _binding!! - - override fun needShowLeftDrawer(): Boolean { - return false - } - - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - _binding = SceneSelectSiteBinding.inflate(inflater, container, false) - if (ehCookieStore.hasSignedIn()) { - binding.siteEx.isChecked = true - } else { - binding.siteE.isChecked = true - } - binding.ok.setOnClickListener { - when (binding.buttonGroup.checkedButtonId) { - R.id.site_e -> { - Settings.putSelectSite(false) - Settings.putGallerySite(EhUrl.SITE_E) - navigateToTop() - } - - R.id.site_ex -> { - Settings.putSelectSite(false) - Settings.putGallerySite(EhUrl.SITE_EX) - navigateToTop() - } - - else -> { - showTip(R.string.no_select, LENGTH_SHORT) - } - } - } - return binding.root - } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } -} \ No newline at end of file diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/SignInScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/SignInScene.kt deleted file mode 100644 index 9ea58869e..000000000 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/SignInScene.kt +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright 2016 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.hippo.ehviewer.ui.scene - -import android.graphics.Paint -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.view.inputmethod.EditorInfo -import androidx.lifecycle.lifecycleScope -import com.hippo.app.BaseDialogBuilder -import com.hippo.ehviewer.R -import com.hippo.ehviewer.Settings -import com.hippo.ehviewer.UrlOpener -import com.hippo.ehviewer.client.EhClient -import com.hippo.ehviewer.client.EhUrl -import com.hippo.ehviewer.client.EhUtils -import com.hippo.ehviewer.client.parser.ProfileParser -import com.hippo.ehviewer.databinding.SceneLoginBinding -import com.hippo.util.ExceptionUtils -import eu.kanade.tachiyomi.util.lang.launchIO -import eu.kanade.tachiyomi.util.lang.withUIContext -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.Job - -class SignInScene : BaseScene() { - private var _binding: SceneLoginBinding? = null - private val binding - get() = _binding!! - private var mSignInJob: Job? = null - override fun needShowLeftDrawer(): Boolean { - return false - } - - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - _binding = SceneLoginBinding.inflate(inflater, container, false) - binding.signInViaWebview.run { - paintFlags = paintFlags or Paint.UNDERLINE_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG - } - binding.signInViaCookies.run { - paintFlags = paintFlags or Paint.UNDERLINE_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG - } - binding.touristMode.run { - paintFlags = paintFlags or Paint.UNDERLINE_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG - } - binding.password.setOnEditorActionListener { v, actionId, _ -> - if (v === binding.password) { - if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_NULL) { - signIn() - return@setOnEditorActionListener true - } - } - false - } - binding.register.setOnClickListener { - UrlOpener.openUrl(requireActivity(), EhUrl.URL_REGISTER, false) - } - binding.signIn.setOnClickListener { signIn() } - binding.signInViaWebview.setOnClickListener { navigate(R.id.webViewSignInScene, null) } - binding.signInViaCookies.setOnClickListener { navigate(R.id.cookieSignInScene, null) } - binding.touristMode.setOnClickListener { - // Set gallery size SITE_E if skip sign in - Settings.putGallerySite(EhUrl.SITE_E) - finishSignIn() - } - return binding.root - } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } - - private fun showProgress() { - if (View.VISIBLE != binding.progress.visibility) { - binding.progress.run { - alpha = 0.0f - visibility = View.VISIBLE - animate().alpha(1.0f).setDuration(500).start() - } - } - } - - private fun hideProgress() { - binding.progress.visibility = View.GONE - } - - private fun signIn() { - if (mSignInJob?.isActive == true) return - val username = binding.username.text.toString() - val password = binding.password.text.toString() - if (username.isEmpty()) { - binding.usernameLayout.error = getString(R.string.error_username_cannot_empty) - return - } else { - binding.usernameLayout.error = null - } - if (password.isEmpty()) { - binding.passwordLayout.error = getString(R.string.error_password_cannot_empty) - return - } else { - binding.passwordLayout.error = null - } - hideSoftInput() - showProgress() - - EhUtils.signOut() - mSignInJob = viewLifecycleOwner.lifecycleScope.launchIO { - runCatching { - (EhClient.execute(EhClient.METHOD_SIGN_IN, username, password) as String).let { - Settings.putDisplayName(it) - } - }.onFailure { - it.printStackTrace() - withUIContext { - hideProgress() - showResultErrorDialog(it) - } - }.onSuccess { - // This fragment's lifecycle is to be finished - GlobalScope.launchIO { - getProfile() - } - withUIContext { - finishSignIn() - } - } - } - } - - private fun finishSignIn() { - Settings.putNeedSignIn(false) - if (Settings.getSelectSite()) { - navigate(R.id.selectSiteScene, null) - } else { - navigateToTop() - } - } - - private fun showResultErrorDialog(e: Throwable) { - BaseDialogBuilder(requireContext()) - .setTitle(R.string.sign_in_failed) - .setMessage( - """ - ${ExceptionUtils.getReadableString(e)} - ${getString(R.string.sign_in_failed_tip)} - """.trimIndent() - ) - .setPositiveButton(R.string.get_it, null) - .show() - } -} - -suspend fun getProfile() { - runCatching { - (EhClient.execute(EhClient.METHOD_GET_PROFILE) as ProfileParser.Result).run { - Settings.putDisplayName(displayName) - Settings.putAvatar(avatar) - } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/WebViewSignInScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/WebViewSignInScene.kt index c650d9dad..8d2e5f596 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/WebViewSignInScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/WebViewSignInScene.kt @@ -28,6 +28,7 @@ import com.hippo.ehviewer.EhApplication.Companion.ehCookieStore import com.hippo.ehviewer.client.EhCookieStore import com.hippo.ehviewer.client.EhUrl import com.hippo.ehviewer.client.EhUtils +import com.hippo.ehviewer.ui.compose.getProfile import com.hippo.ehviewer.widget.DialogWebChromeClient import eu.kanade.tachiyomi.util.lang.launchIO import okhttp3.Cookie diff --git a/app/src/main/res/layout-television/scene_cookie_sign_in.xml b/app/src/main/res/layout-television/scene_cookie_sign_in.xml deleted file mode 100644 index a50024c8b..000000000 --- a/app/src/main/res/layout-television/scene_cookie_sign_in.xml +++ /dev/null @@ -1,166 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -