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