SourceLoginDialog.kt 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package eu.kanade.tachiyomi.widget.preference
  2. import android.os.Bundle
  3. import android.view.View
  4. import eu.kanade.tachiyomi.R
  5. import eu.kanade.tachiyomi.data.source.Source
  6. import eu.kanade.tachiyomi.data.source.online.LoginSource
  7. import eu.kanade.tachiyomi.ui.setting.SettingsActivity
  8. import eu.kanade.tachiyomi.util.toast
  9. import kotlinx.android.synthetic.main.pref_account_login.view.*
  10. import rx.android.schedulers.AndroidSchedulers
  11. import rx.schedulers.Schedulers
  12. class SourceLoginDialog : LoginDialogPreference() {
  13. companion object {
  14. fun newInstance(source: Source): LoginDialogPreference {
  15. val fragment = SourceLoginDialog()
  16. val bundle = Bundle(1)
  17. bundle.putInt("key", source.id)
  18. fragment.arguments = bundle
  19. return fragment
  20. }
  21. }
  22. lateinit var source: LoginSource
  23. override fun onCreate(savedInstanceState: Bundle?) {
  24. super.onCreate(savedInstanceState)
  25. val sourceId = arguments.getInt("key")
  26. source = (activity as SettingsActivity).sourceManager.get(sourceId) as LoginSource
  27. }
  28. override fun setCredentialsOnView(view: View) = with(view) {
  29. dialog_title.text = getString(R.string.login_title, source.toString())
  30. username.setText(preferences.sourceUsername(source))
  31. password.setText(preferences.sourcePassword(source))
  32. }
  33. override fun checkLogin() {
  34. requestSubscription?.unsubscribe()
  35. v?.apply {
  36. if (username.text.length == 0 || password.text.length == 0)
  37. return
  38. login.progress = 1
  39. requestSubscription = source.login(username.text.toString(), password.text.toString())
  40. .subscribeOn(Schedulers.io())
  41. .observeOn(AndroidSchedulers.mainThread())
  42. .subscribe({ logged ->
  43. if (logged) {
  44. preferences.setSourceCredentials(source,
  45. username.text.toString(),
  46. password.text.toString())
  47. dialog.dismiss()
  48. context.toast(R.string.login_success)
  49. } else {
  50. preferences.setSourceCredentials(source, "", "")
  51. login.progress = -1
  52. }
  53. }, { error ->
  54. login.progress = -1
  55. login.setText(R.string.unknown_error)
  56. })
  57. }
  58. }
  59. }