Maybe fix a few crashes related to mangadex logins

This commit is contained in:
Jobobby04 2021-10-18 23:11:35 -04:00
parent f76c48c8ae
commit be52ea6f18
2 changed files with 13 additions and 12 deletions

View File

@ -12,7 +12,6 @@ import exh.md.utils.MdUtil
import exh.util.seconds import exh.util.seconds
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.withTimeoutOrNull import kotlinx.coroutines.withTimeoutOrNull
import kotlinx.serialization.encodeToString
class MangaDexLoginHelper(val authServiceLazy: Lazy<MangaDexAuthService>, val preferences: PreferencesHelper, val mdList: MdList) { class MangaDexLoginHelper(val authServiceLazy: Lazy<MangaDexAuthService>, val preferences: PreferencesHelper, val mdList: MdList) {
val authService by authServiceLazy val authService by authServiceLazy
@ -31,7 +30,7 @@ class MangaDexLoginHelper(val authServiceLazy: Lazy<MangaDexAuthService>, val pr
} }
val refresh = runCatching { val refresh = runCatching {
val jsonResponse = authService.refreshToken(RefreshTokenDto(refreshToken)) val jsonResponse = authService.refreshToken(RefreshTokenDto(refreshToken))
preferences.trackToken(mdList).set(MdUtil.jsonParser.encodeToString(jsonResponse.token)) MdUtil.updateLoginToken(jsonResponse.token, preferences, mdList)
} }
val e = refresh.exceptionOrNull() val e = refresh.exceptionOrNull()
@ -52,12 +51,16 @@ class MangaDexLoginHelper(val authServiceLazy: Lazy<MangaDexAuthService>, val pr
if (e is CancellationException) throw e if (e is CancellationException) throw e
val loginResponseDto = loginResult.getOrNull() val loginResponseDto = loginResult.getOrNull()
if (loginResponseDto != null) {
MdUtil.updateLoginToken( MdUtil.updateLoginToken(
loginResponseDto?.token, loginResponseDto.token,
preferences, preferences,
mdList mdList
) )
loginResponseDto != null true
} else {
false
}
} }
} }

View File

@ -300,10 +300,8 @@ class MdUtil {
fun refreshToken(preferences: PreferencesHelper, mdList: MdList) = getLoginBody(preferences, mdList)?.refresh fun refreshToken(preferences: PreferencesHelper, mdList: MdList) = getLoginBody(preferences, mdList)?.refresh
fun updateLoginToken(token: LoginBodyTokenDto?, preferences: PreferencesHelper, mdList: MdList) { fun updateLoginToken(token: LoginBodyTokenDto, preferences: PreferencesHelper, mdList: MdList) {
if (token != null) {
preferences.trackToken(mdList).set(jsonParser.encodeToString(token)) preferences.trackToken(mdList).set(jsonParser.encodeToString(token))
} else preferences.trackToken(mdList).delete()
} }
fun getAuthHeaders(headers: Headers, preferences: PreferencesHelper, mdList: MdList) = fun getAuthHeaders(headers: Headers, preferences: PreferencesHelper, mdList: MdList) =