Browse Source

Prevent long-running requests to make the app crash when user is logging out.

housekeeping/remove-warnings
Antoine POPINEAU 4 years ago
parent
commit
a19e500f09
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
  1. 6
      app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt
  2. 42
      app/src/main/java/com/github/apognu/otter/repositories/HttpUpstream.kt
  3. 8
      app/src/main/java/com/github/apognu/otter/utils/Util.kt

6
app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt

@ -29,9 +29,9 @@ class FavoritesRepository(override val context: Context?) : Repository<Track, Tr @@ -29,9 +29,9 @@ class FavoritesRepository(override val context: Context?) : Repository<Track, Tr
track.downloaded = downloaded.contains(track.id)
track.bestUpload()?.let { upload ->
val url = mustNormalizeUrl(upload.listen_url)
track.cached = Otter.get().exoCache.isCached(url, 0, upload.duration * 1000L)
maybeNormalizeUrl(upload.listen_url)?.let { url ->
track.cached = Otter.get().exoCache.isCached(url, 0, upload.duration * 1000L)
}
}
track

42
app/src/main/java/com/github/apognu/otter/repositories/HttpUpstream.kt

@ -65,32 +65,40 @@ class HttpUpstream<D : Any, R : FunkwhaleResponse<D>>(val behavior: Behavior, pr @@ -65,32 +65,40 @@ class HttpUpstream<D : Any, R : FunkwhaleResponse<D>>(val behavior: Behavior, pr
}
suspend fun get(url: String): Result<R, FuelError> {
val request = Fuel.get(mustNormalizeUrl(url)).apply {
if (!Settings.isAnonymous()) {
header("Authorization", "Bearer ${Settings.getAccessToken()}")
return try {
val request = Fuel.get(mustNormalizeUrl(url)).apply {
if (!Settings.isAnonymous()) {
header("Authorization", "Bearer ${Settings.getAccessToken()}")
}
}
}
val (_, response, result) = request.awaitObjectResponseResult(GenericDeserializer<R>(type))
val (_, response, result) = request.awaitObjectResponseResult(GenericDeserializer<R>(type))
if (response.statusCode == 401) {
return retryGet(url)
}
if (response.statusCode == 401) {
return retryGet(url)
}
return result
result
} catch (e: Exception) {
Result.error(FuelError.wrap(e))
}
}
private suspend fun retryGet(url: String): Result<R, FuelError> {
return if (HTTP.refresh()) {
val request = Fuel.get(mustNormalizeUrl(url)).apply {
if (!Settings.isAnonymous()) {
header("Authorization", "Bearer ${Settings.getAccessToken()}")
return try {
return if (HTTP.refresh()) {
val request = Fuel.get(mustNormalizeUrl(url)).apply {
if (!Settings.isAnonymous()) {
header("Authorization", "Bearer ${Settings.getAccessToken()}")
}
}
}
request.awaitObjectResult(GenericDeserializer(type))
} else {
Result.Failure(FuelError.wrap(RefreshError))
request.awaitObjectResult(GenericDeserializer(type))
} else {
Result.Failure(FuelError.wrap(RefreshError))
}
} catch (e: Exception) {
Result.error(FuelError.wrap(e))
}
}
}

8
app/src/main/java/com/github/apognu/otter/utils/Util.kt

@ -22,9 +22,13 @@ fun Any.log() { @@ -22,9 +22,13 @@ fun Any.log() {
}
fun maybeNormalizeUrl(rawUrl: String?): String? {
if (rawUrl == null || rawUrl.isEmpty()) return null
try {
if (rawUrl == null || rawUrl.isEmpty()) return null
return mustNormalizeUrl(rawUrl)
return mustNormalizeUrl(rawUrl)
} catch (e: Exception) {
return null
}
}
fun mustNormalizeUrl(rawUrl: String): String {

Loading…
Cancel
Save