[Android] OpenSubtitles: fix the login form not working in some cases

Nicolas Pomepuy git at videolan.org
Thu Nov 21 13:19:11 UTC 2024


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Nov 21 09:10:47 2024 +0100| [f5a75f248e2d216c169ecdcf0b4ef5f30c75411a] | committer: Duncan McNamara

OpenSubtitles: fix the login form not working in some cases

> https://code.videolan.org/videolan/vlc-android/commit/f5a75f248e2d216c169ecdcf0b4ef5f30c75411a
---

 .../gui/dialogs/SubtitleDownloaderDialogFragment.kt    |  8 +-------
 .../src/org/videolan/vlc/viewmodels/SubtitlesModel.kt  | 18 +++++++++++++++++-
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt
index 9a3ef39922..013bf31996 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt
@@ -58,8 +58,6 @@ class SubtitleDownloaderDialogFragment : VLCBottomSheetDialogFragment() {
     private lateinit var names: String
     private lateinit var viewModel: SubtitlesModel
     private lateinit var toast: Toast
-    private var lastUsername: String = ""
-    private var lastPassword: String = ""
 
     private var state: SubDownloadDialogState = SubDownloadDialogState.Download
         set(value) {
@@ -145,12 +143,8 @@ class SubtitleDownloaderDialogFragment : VLCBottomSheetDialogFragment() {
         binding.loginButton.setOnClickListener {
             if (viewModel.observableUser.get()?.logged == true) {
                 viewModel.logout(settings)
+                viewModel.clearCredentials()
             } else {
-                if (lastPassword == binding.password.text.toString() && lastUsername == binding.username.text.toString()) {
-                    return at setOnClickListener
-                }
-                lastPassword = binding.password.text.toString()
-                lastUsername = binding.username.text.toString()
                 viewModel.login(
                     settings,
                     binding.username.text.toString(),
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/SubtitlesModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/SubtitlesModel.kt
index 5092371d0d..83e7206467 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/SubtitlesModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/SubtitlesModel.kt
@@ -86,6 +86,10 @@ class SubtitlesModel(context: Context, private val mediaUri: Uri, private val na
     val observableError = ObservableField<Boolean>()
     val observableResultDescription = ObservableField<Spanned>()
     val observableResultDescriptionTalkback = ObservableField<String>()
+
+    private var lastUsername: String = ""
+    private var lastPassword: String = ""
+
     val oldLanguagesMigration by lazy {
         val newLangCodes =  context.resources.getStringArray(R.array.language_values)
         val oldLangCodes =  context.resources.getStringArray(R.array.old_language_values)
@@ -298,6 +302,9 @@ class SubtitlesModel(context: Context, private val mediaUri: Uri, private val na
     }
 
     fun login(settings: SharedPreferences, username: String, password: String) {
+        if (lastPassword == username && lastUsername == password) {
+            return
+        }
         viewModelScope.launch {
             withContext(Dispatchers.IO) {
                 try {
@@ -313,11 +320,16 @@ class SubtitlesModel(context: Context, private val mediaUri: Uri, private val na
                             return at withContext
                         }
                     }
+                    val code = call.code()
+                    if (code == 401) {
+                        lastPassword = password
+                        lastUsername = username
+                    }
                     observableUser.set(
                         OpenSubtitlesUser(
                             false,
                             null,
-                            errorMessage = if (call.code() == 401) getContext().getString(R.string.login_error) else getContext().getString(
+                            errorMessage = if (code == 401) getContext().getString(R.string.login_error) else getContext().getString(
                                 R.string.unknown_error
                             )
                         )
@@ -369,6 +381,10 @@ class SubtitlesModel(context: Context, private val mediaUri: Uri, private val na
     }
 
     fun saveLastUsedLanguage(lastUsedLanguages: List<String>) = Settings.getInstance(getContext()).putSingle(LAST_USED_LANGUAGES, lastUsedLanguages)
+    fun clearCredentials() {
+        lastPassword = ""
+        lastUsername = ""
+    }
 
     class Factory(private val context: Context, private val mediaUri: Uri, private val name: String) : ViewModelProvider.NewInstanceFactory() {
         override fun <T : ViewModel> create(modelClass: Class<T>): T {



More information about the Android mailing list