OpenSubtitles: fix crash when logging in without connection

This commit is contained in:
Nicolas Pomepuy 2024-11-21 08:52:42 +01:00 committed by Duncan McNamara
parent 750bad3d64
commit 8b35f89743

View File

@ -300,18 +300,38 @@ class SubtitlesModel(context: Context, private val mediaUri: Uri, private val na
fun login(settings: SharedPreferences, username: String, password: String) { fun login(settings: SharedPreferences, username: String, password: String) {
viewModelScope.launch { viewModelScope.launch {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val call = OpenSubtitleRepository.getInstance().login(username, password) try {
if (call.isSuccessful) { val call = OpenSubtitleRepository.getInstance().login(username, password)
val userResult = call.body() if (call.isSuccessful) {
if (userResult != null) { val userResult = call.body()
val openSubtitlesUser = OpenSubtitlesUser(true, userResult, username = username) if (userResult != null) {
OpenSubtitlesUtils.saveUser(settings, openSubtitlesUser) val openSubtitlesUser =
observableUser.set(openSubtitlesUser) OpenSubtitlesUser(true, userResult, username = username)
checkUserInfos(settings) OpenSubtitlesUtils.saveUser(settings, openSubtitlesUser)
return@withContext observableUser.set(openSubtitlesUser)
checkUserInfos(settings)
return@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)))
} }
} }
} }