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) {
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@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@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)))
}
}
}