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 kotlinx.coroutines.CancellationException
import kotlinx.coroutines.withTimeoutOrNull
import kotlinx.serialization.encodeToString
class MangaDexLoginHelper(val authServiceLazy: Lazy<MangaDexAuthService>, val preferences: PreferencesHelper, val mdList: MdList) {
val authService by authServiceLazy
@ -31,7 +30,7 @@ class MangaDexLoginHelper(val authServiceLazy: Lazy<MangaDexAuthService>, val pr
}
val refresh = runCatching {
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()
@ -52,12 +51,16 @@ class MangaDexLoginHelper(val authServiceLazy: Lazy<MangaDexAuthService>, val pr
if (e is CancellationException) throw e
val loginResponseDto = loginResult.getOrNull()
MdUtil.updateLoginToken(
loginResponseDto?.token,
preferences,
mdList
)
loginResponseDto != null
if (loginResponseDto != null) {
MdUtil.updateLoginToken(
loginResponseDto.token,
preferences,
mdList
)
true
} else {
false
}
}
}

View File

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