LoginSource changes
This commit is contained in:
parent
fbe10151f4
commit
a3c26c63d4
@ -1,17 +1,25 @@
|
|||||||
package eu.kanade.tachiyomi.source.online
|
package eu.kanade.tachiyomi.source.online
|
||||||
|
|
||||||
import android.app.Activity
|
|
||||||
import eu.kanade.tachiyomi.source.Source
|
import eu.kanade.tachiyomi.source.Source
|
||||||
import eu.kanade.tachiyomi.ui.base.controller.DialogController
|
|
||||||
|
|
||||||
interface LoginSource : Source {
|
interface LoginSource : Source {
|
||||||
val needsLogin: Boolean
|
val requiresLogin: Boolean
|
||||||
|
|
||||||
|
val twoFactorAuth: AuthSupport
|
||||||
|
|
||||||
fun isLogged(): Boolean
|
fun isLogged(): Boolean
|
||||||
|
|
||||||
fun getLoginDialog(source: Source, activity: Activity): DialogController
|
fun getUsername(): String
|
||||||
|
|
||||||
suspend fun login(username: String, password: String, twoFactorCode: String = ""): Boolean
|
fun getPassword(): String
|
||||||
|
|
||||||
|
suspend fun login(username: String, password: String, twoFactorCode: String?): Boolean
|
||||||
|
|
||||||
suspend fun logout(): Boolean
|
suspend fun logout(): Boolean
|
||||||
|
|
||||||
|
enum class AuthSupport {
|
||||||
|
NOT_SUPPORTED,
|
||||||
|
SUPPORTED,
|
||||||
|
REQUIRED
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package eu.kanade.tachiyomi.source.online.all
|
package eu.kanade.tachiyomi.source.online.all
|
||||||
|
|
||||||
import android.app.Activity
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
@ -12,7 +11,6 @@ import eu.kanade.tachiyomi.network.GET
|
|||||||
import eu.kanade.tachiyomi.network.POST
|
import eu.kanade.tachiyomi.network.POST
|
||||||
import eu.kanade.tachiyomi.network.asObservableSuccess
|
import eu.kanade.tachiyomi.network.asObservableSuccess
|
||||||
import eu.kanade.tachiyomi.network.await
|
import eu.kanade.tachiyomi.network.await
|
||||||
import eu.kanade.tachiyomi.source.Source
|
|
||||||
import eu.kanade.tachiyomi.source.model.FilterList
|
import eu.kanade.tachiyomi.source.model.FilterList
|
||||||
import eu.kanade.tachiyomi.source.model.MangasPage
|
import eu.kanade.tachiyomi.source.model.MangasPage
|
||||||
import eu.kanade.tachiyomi.source.model.Page
|
import eu.kanade.tachiyomi.source.model.Page
|
||||||
@ -26,7 +24,6 @@ import eu.kanade.tachiyomi.source.online.MetadataSource
|
|||||||
import eu.kanade.tachiyomi.source.online.RandomMangaSource
|
import eu.kanade.tachiyomi.source.online.RandomMangaSource
|
||||||
import eu.kanade.tachiyomi.source.online.UrlImportableSource
|
import eu.kanade.tachiyomi.source.online.UrlImportableSource
|
||||||
import eu.kanade.tachiyomi.ui.base.controller.BaseController
|
import eu.kanade.tachiyomi.ui.base.controller.BaseController
|
||||||
import eu.kanade.tachiyomi.ui.base.controller.DialogController
|
|
||||||
import eu.kanade.tachiyomi.ui.manga.MangaController
|
import eu.kanade.tachiyomi.ui.manga.MangaController
|
||||||
import eu.kanade.tachiyomi.util.lang.runAsObservable
|
import eu.kanade.tachiyomi.util.lang.runAsObservable
|
||||||
import eu.kanade.tachiyomi.util.lang.withIOContext
|
import eu.kanade.tachiyomi.util.lang.withIOContext
|
||||||
@ -46,7 +43,6 @@ import exh.metadata.metadata.MangaDexSearchMetadata
|
|||||||
import exh.source.DelegatedHttpSource
|
import exh.source.DelegatedHttpSource
|
||||||
import exh.ui.metadata.adapters.MangaDexDescriptionAdapter
|
import exh.ui.metadata.adapters.MangaDexDescriptionAdapter
|
||||||
import exh.util.urlImportFetchSearchManga
|
import exh.util.urlImportFetchSearchManga
|
||||||
import exh.widget.preference.MangadexLoginDialog
|
|
||||||
import kotlinx.serialization.decodeFromString
|
import kotlinx.serialization.decodeFromString
|
||||||
import kotlinx.serialization.json.JsonObject
|
import kotlinx.serialization.json.JsonObject
|
||||||
import kotlinx.serialization.json.JsonPrimitive
|
import kotlinx.serialization.json.JsonPrimitive
|
||||||
@ -180,21 +176,27 @@ class MangaDex(delegate: HttpSource, val context: Context) :
|
|||||||
return FollowsHandler(client, headers, Injekt.get(), useLowQualityThumbnail()).fetchFollows()
|
return FollowsHandler(client, headers, Injekt.get(), useLowQualityThumbnail()).fetchFollows()
|
||||||
}
|
}
|
||||||
|
|
||||||
override val needsLogin: Boolean = true
|
override val requiresLogin: Boolean = true
|
||||||
|
|
||||||
override fun getLoginDialog(source: Source, activity: Activity): DialogController {
|
override val twoFactorAuth = LoginSource.AuthSupport.SUPPORTED
|
||||||
return MangadexLoginDialog(source as MangaDex)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun isLogged(): Boolean {
|
override fun isLogged(): Boolean {
|
||||||
val httpUrl = MdUtil.baseUrl.toHttpUrl()
|
val httpUrl = MdUtil.baseUrl.toHttpUrl()
|
||||||
return trackManager.mdList.isLogged && network.cookieManager.get(httpUrl).any { it.name == REMEMBER_ME }
|
return trackManager.mdList.isLogged && network.cookieManager.get(httpUrl).any { it.name == REMEMBER_ME }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getUsername(): String {
|
||||||
|
return trackManager.mdList.getUsername()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getPassword(): String {
|
||||||
|
return trackManager.mdList.getPassword()
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun login(
|
override suspend fun login(
|
||||||
username: String,
|
username: String,
|
||||||
password: String,
|
password: String,
|
||||||
twoFactorCode: String
|
twoFactorCode: String?
|
||||||
): Boolean {
|
): Boolean {
|
||||||
return withIOContext {
|
return withIOContext {
|
||||||
val formBody = FormBody.Builder().apply {
|
val formBody = FormBody.Builder().apply {
|
||||||
@ -202,7 +204,7 @@ class MangaDex(delegate: HttpSource, val context: Context) :
|
|||||||
add("login_password", password)
|
add("login_password", password)
|
||||||
add("no_js", "1")
|
add("no_js", "1")
|
||||||
add("remember_me", "1")
|
add("remember_me", "1")
|
||||||
add("two_factor", twoFactorCode)
|
add("two_factor", twoFactorCode ?: "")
|
||||||
}
|
}
|
||||||
|
|
||||||
runCatching {
|
runCatching {
|
||||||
@ -223,6 +225,8 @@ class MangaDex(delegate: HttpSource, val context: Context) :
|
|||||||
} else {
|
} else {
|
||||||
throw Exception("Json data was null")
|
throw Exception("Json data was null")
|
||||||
}
|
}
|
||||||
|
}.also {
|
||||||
|
preferences.setTrackCredentials(trackManager.mdList, username, password)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,6 +61,7 @@ import exh.md.similar.ui.EnableMangaDexSimilarDialogController
|
|||||||
import exh.savedsearches.EXHSavedSearch
|
import exh.savedsearches.EXHSavedSearch
|
||||||
import exh.source.getMainSource
|
import exh.source.getMainSource
|
||||||
import exh.source.isEhBasedSource
|
import exh.source.isEhBasedSource
|
||||||
|
import exh.widget.preference.MangadexLoginDialog
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.flow.drop
|
import kotlinx.coroutines.flow.drop
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
@ -182,8 +183,8 @@ open class BrowseSourceController(bundle: Bundle) :
|
|||||||
preferences.shownMangaDexSimilarAskDialog().set(true)
|
preferences.shownMangaDexSimilarAskDialog().set(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mainSource is LoginSource && mainSource.needsLogin && !mainSource.isLogged()) {
|
if (mainSource is LoginSource && mainSource.requiresLogin && !mainSource.isLogged()) {
|
||||||
val dialog = mainSource.getLoginDialog(mainSource, activity!!)
|
val dialog = MangadexLoginDialog(mainSource)
|
||||||
dialog.showDialog(router)
|
dialog.showDialog(router)
|
||||||
}
|
}
|
||||||
// SY <--
|
// SY <--
|
||||||
|
@ -12,11 +12,10 @@ import com.bluelinelabs.conductor.ControllerChangeType
|
|||||||
import com.dd.processbutton.iml.ActionProcessButton
|
import com.dd.processbutton.iml.ActionProcessButton
|
||||||
import eu.kanade.tachiyomi.R
|
import eu.kanade.tachiyomi.R
|
||||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
||||||
import eu.kanade.tachiyomi.data.track.TrackManager
|
|
||||||
import eu.kanade.tachiyomi.databinding.PrefSiteLoginTwoFactorAuthBinding
|
import eu.kanade.tachiyomi.databinding.PrefSiteLoginTwoFactorAuthBinding
|
||||||
import eu.kanade.tachiyomi.source.Source
|
import eu.kanade.tachiyomi.source.Source
|
||||||
import eu.kanade.tachiyomi.source.SourceManager
|
import eu.kanade.tachiyomi.source.SourceManager
|
||||||
import eu.kanade.tachiyomi.source.online.all.MangaDex
|
import eu.kanade.tachiyomi.source.online.LoginSource
|
||||||
import eu.kanade.tachiyomi.ui.base.controller.DialogController
|
import eu.kanade.tachiyomi.ui.base.controller.DialogController
|
||||||
import eu.kanade.tachiyomi.util.lang.launchUI
|
import eu.kanade.tachiyomi.util.lang.launchUI
|
||||||
import eu.kanade.tachiyomi.util.system.toast
|
import eu.kanade.tachiyomi.util.system.toast
|
||||||
@ -33,9 +32,7 @@ import uy.kohesive.injekt.injectLazy
|
|||||||
|
|
||||||
class MangadexLoginDialog(bundle: Bundle? = null) : DialogController(bundle) {
|
class MangadexLoginDialog(bundle: Bundle? = null) : DialogController(bundle) {
|
||||||
|
|
||||||
val source = Injekt.get<SourceManager>().get(args.getLong("key", 0))?.getMainSource() as? MangaDex
|
val source = Injekt.get<SourceManager>().get(args.getLong("key", 0))?.getMainSource() as LoginSource
|
||||||
|
|
||||||
val service = Injekt.get<TrackManager>().mdList
|
|
||||||
|
|
||||||
val preferences: PreferencesHelper by injectLazy()
|
val preferences: PreferencesHelper by injectLazy()
|
||||||
|
|
||||||
@ -43,7 +40,7 @@ class MangadexLoginDialog(bundle: Bundle? = null) : DialogController(bundle) {
|
|||||||
|
|
||||||
lateinit var binding: PrefSiteLoginTwoFactorAuthBinding
|
lateinit var binding: PrefSiteLoginTwoFactorAuthBinding
|
||||||
|
|
||||||
constructor(source: MangaDex) : this(
|
constructor(source: LoginSource) : this(
|
||||||
bundleOf(
|
bundleOf(
|
||||||
"key" to source.id
|
"key" to source.id
|
||||||
)
|
)
|
||||||
@ -65,14 +62,26 @@ class MangadexLoginDialog(bundle: Bundle? = null) : DialogController(bundle) {
|
|||||||
|
|
||||||
setCredentialsOnView()
|
setCredentialsOnView()
|
||||||
|
|
||||||
binding.twoFactorCheck.setOnCheckedChangeListener { _, isChecked ->
|
when (source.twoFactorAuth) {
|
||||||
binding.twoFactorHolder.isVisible = isChecked
|
LoginSource.AuthSupport.REQUIRED -> {
|
||||||
|
binding.twoFactorCheck.isVisible = false
|
||||||
|
binding.twoFactorHolder.isVisible = true
|
||||||
|
}
|
||||||
|
LoginSource.AuthSupport.SUPPORTED -> {
|
||||||
|
binding.twoFactorCheck.setOnCheckedChangeListener { _, isChecked ->
|
||||||
|
binding.twoFactorHolder.isVisible = isChecked
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LoginSource.AuthSupport.NOT_SUPPORTED -> {
|
||||||
|
binding.twoFactorCheck.isVisible = false
|
||||||
|
binding.twoFactorHolder.isVisible = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setCredentialsOnView() {
|
private fun setCredentialsOnView() {
|
||||||
binding.username.setText(service.getUsername())
|
binding.username.setText(source.getUsername())
|
||||||
binding.password.setText(service.getPassword())
|
binding.password.setText(source.getPassword())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkLogin() {
|
private fun checkLogin() {
|
||||||
@ -94,14 +103,13 @@ class MangadexLoginDialog(bundle: Bundle? = null) : DialogController(bundle) {
|
|||||||
|
|
||||||
scope.launch {
|
scope.launch {
|
||||||
try {
|
try {
|
||||||
val result = source?.login(
|
val result = source.login(
|
||||||
username,
|
username,
|
||||||
password,
|
password,
|
||||||
twoFactor.toString()
|
twoFactor.toString()
|
||||||
) ?: false
|
)
|
||||||
if (result) {
|
if (result) {
|
||||||
dialog?.dismiss()
|
dialog?.dismiss()
|
||||||
preferences.setTrackCredentials(service, username, password)
|
|
||||||
launchUI {
|
launchUI {
|
||||||
binding.root.context.toast(R.string.login_success)
|
binding.root.context.toast(R.string.login_success)
|
||||||
}
|
}
|
||||||
@ -135,9 +143,9 @@ class MangadexLoginDialog(bundle: Bundle? = null) : DialogController(bundle) {
|
|||||||
private fun onDialogClosed() {
|
private fun onDialogClosed() {
|
||||||
scope.cancel()
|
scope.cancel()
|
||||||
if (activity != null) {
|
if (activity != null) {
|
||||||
(activity as? Listener)?.siteLoginDialogClosed(source!!)
|
(activity as? Listener)?.siteLoginDialogClosed(source)
|
||||||
} else {
|
} else {
|
||||||
(targetController as? Listener)?.siteLoginDialogClosed(source!!)
|
(targetController as? Listener)?.siteLoginDialogClosed(source)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user