don't check processing state when upload returned 200

This commit is contained in:
Conny Duck 2022-11-29 21:13:54 +01:00
parent 478dd8c601
commit 5c994ac91f
5 changed files with 23 additions and 16 deletions

View File

@ -1219,7 +1219,8 @@ class ComposeActivity :
val uploadPercent: Int = 0,
val id: String? = null,
val description: String? = null,
val focus: Attachment.Focus? = null
val focus: Attachment.Focus? = null,
val processed: Boolean?
) {
enum class Type {
IMAGE, VIDEO, AUDIO;

View File

@ -134,7 +134,8 @@ class ComposeViewModel @Inject constructor(
type = type,
mediaSize = mediaSize,
description = description,
focus = focus
focus = focus,
processed = null
)
stashMediaItem = mediaItem
@ -159,7 +160,7 @@ class ComposeViewModel @Inject constructor(
is UploadEvent.ProgressEvent ->
item.copy(uploadPercent = event.percentage)
is UploadEvent.FinishedEvent ->
item.copy(id = event.mediaId, uploadPercent = -1)
item.copy(id = event.mediaId, uploadPercent = -1, processed = event.processed)
is UploadEvent.ErrorEvent -> {
media.update { mediaValue -> mediaValue.filter { it.localId != mediaItem.localId } }
uploadError.emit(event.error)
@ -190,7 +191,8 @@ class ComposeViewModel @Inject constructor(
uploadPercent = -1,
id = id,
description = description,
focus = focus
focus = focus,
processed = true
)
mediaValue + mediaItem
}
@ -294,7 +296,7 @@ class ComposeViewModel @Inject constructor(
uri = item.uri.toString(),
description = item.description,
focus = item.focus,
processed = false
processed = item.processed == true
)
}
val tootToSend = StatusToSend(

View File

@ -23,7 +23,6 @@ import android.util.Log
import android.webkit.MimeTypeMap
import androidx.core.content.FileProvider
import androidx.core.net.toUri
import at.connyduck.calladapter.networkresult.fold
import com.keylesspalace.tusky.BuildConfig
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.components.compose.ComposeActivity.QueuedMedia
@ -51,6 +50,7 @@ import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.shareIn
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import retrofit2.HttpException
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
@ -63,7 +63,7 @@ sealed interface FinalUploadEvent
sealed class UploadEvent {
data class ProgressEvent(val percentage: Int) : UploadEvent()
data class FinishedEvent(val mediaId: String) : UploadEvent(), FinalUploadEvent
data class FinishedEvent(val mediaId: String, val processed: Boolean) : UploadEvent(), FinalUploadEvent
data class ErrorEvent(val error: Throwable) : UploadEvent(), FinalUploadEvent
}
@ -286,16 +286,20 @@ class MediaUploader @Inject constructor(
null
}
mediaUploadApi.uploadMedia(body, description, focus).fold({ result ->
send(UploadEvent.FinishedEvent(result.id))
}, { throwable ->
val errorMessage = throwable.getServerErrorMessage()
val uploadResponse = mediaUploadApi.uploadMedia(body, description, focus)
val responseBody = uploadResponse.body()
if (uploadResponse.isSuccessful && responseBody != null) {
send(UploadEvent.FinishedEvent(responseBody.id, uploadResponse.code() == 200))
} else {
val error = HttpException(uploadResponse)
val errorMessage = error.getServerErrorMessage()
if (errorMessage == null) {
throw throwable
throw error
} else {
throw UploadServerError(errorMessage)
}
})
}
awaitClose()
}
}

View File

@ -1,8 +1,8 @@
package com.keylesspalace.tusky.network
import at.connyduck.calladapter.networkresult.NetworkResult
import com.keylesspalace.tusky.entity.MediaUploadResult
import okhttp3.MultipartBody
import retrofit2.Response
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part
@ -17,5 +17,5 @@ interface MediaUploadApi {
@Part file: MultipartBody.Part,
@Part description: MultipartBody.Part? = null,
@Part focus: MultipartBody.Part? = null
): NetworkResult<MediaUploadResult>
): Response<MediaUploadResult>
}

View File

@ -140,7 +140,7 @@ class SendStatusService : Service(), Injectable {
val media = statusToSend.media.map { mediaItem ->
if (mediaItem.id == null) {
when (val uploadState = mediaUploader.getMediaUploadState(mediaItem.localId)) {
is UploadEvent.FinishedEvent -> mediaItem.copy(id = uploadState.mediaId)
is UploadEvent.FinishedEvent -> mediaItem.copy(id = uploadState.mediaId, processed = uploadState.processed)
is UploadEvent.ErrorEvent -> {
Log.w(TAG, "failed uploading media", uploadState.error)
failSending(statusId)