[Android] OpenSubtitles: fix crash when logging in without connection

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 08:52:42 2024 +0100| [8b35f89743db9160a96c17e70f7466bc034846e5] | committer: Duncan McNamara

OpenSubtitles: fix crash when logging in without connection

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

 .../org/videolan/vlc/viewmodels/SubtitlesModel.kt  | 40 ++++++++++++++++------
 1 file changed, 30 insertions(+), 10 deletions(-)

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 f39c2b322b..5092371d0d 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/SubtitlesModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/SubtitlesModel.kt
@@ -300,18 +300,38 @@ class SubtitlesModel(context: Context, private val mediaUri: Uri, private val na
     fun login(settings: SharedPreferences, username: String, password: String) {
         viewModelScope.launch {
             withContext(Dispatchers.IO) {
-                val call = OpenSubtitleRepository.getInstance().login(username, password)
-                if (call.isSuccessful) {
-                    val userResult = call.body()
-                    if (userResult != null) {
-                        val openSubtitlesUser = OpenSubtitlesUser(true, userResult, username = username)
-                        OpenSubtitlesUtils.saveUser(settings, openSubtitlesUser)
-                        observableUser.set(openSubtitlesUser)
-                        checkUserInfos(settings)
-                        return at withContext
+                try {
+                    val call = OpenSubtitleRepository.getInstance().login(username, password)
+                    if (call.isSuccessful) {
+                        val userResult = call.body()
+                        if (userResult != null) {
+                            val openSubtitlesUser =
+                                OpenSubtitlesUser(true, userResult, username = username)
+                            OpenSubtitlesUtils.saveUser(settings, openSubtitlesUser)
+                            observableUser.set(openSubtitlesUser)
+                            checkUserInfos(settings)
+                            return at withContext
+                        }
                     }
+                    observableUser.set(
+                        OpenSubtitlesUser(
+                            false,
+                            null,
+                            errorMessage = if (call.code() == 401) getContext().getString(R.string.login_error) else getContext().getString(
+                                R.string.unknown_error
+                            )
+                        )
+                    )
+                } catch (e: NoConnectivityException) {
+                    observableUser.set(
+                        OpenSubtitlesUser(
+                            false,
+                            null,
+                            errorMessage = getContext().getString(R.string.no_internet_connection)
+                        )
+                    )
                 }
-                observableUser.set(OpenSubtitlesUser(false, null, errorMessage = if (call.code() == 401) getContext().getString(R.string.login_error) else getContext().getString(R.string.unknown_error)))
+
             }
         }
     }



More information about the Android mailing list