Skip to content

Commit

Permalink
fix: Resume network with old uploaded size
Browse files Browse the repository at this point in the history
  • Loading branch information
sirambd committed Nov 26, 2024
1 parent a4239ba commit 032984d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class UploadProgressViewModel @Inject constructor(
}.stateIn(
scope = viewModelScope,
started = SharingStarted.Eagerly,
initialValue = UploadWorker.UploadProgressUiState.Default,
initialValue = UploadWorker.UploadProgressUiState.Default(),
)

fun trackUploadProgress() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ class UploadWorker @AssistedInject constructor(
.build()

return workManager.getWorkInfosFlow(workQuery).mapLatest { workInfoList ->
val workInfo = workInfoList.firstOrNull() ?: return@mapLatest UploadProgressUiState.Default
val workInfo = workInfoList.firstOrNull() ?: return@mapLatest UploadProgressUiState.Default()
return@mapLatest when (workInfo.state) {
State.RUNNING -> UploadProgressUiState.Progress(workInfo.progress).also { lastUploadedSize = it.uploadedSize }
State.SUCCEEDED -> UploadProgressUiState.Success.create(workInfo.outputData, sharedApiUrlCreator)
State.FAILED, State.CANCELLED -> UploadProgressUiState.Error(lastUploadedSize)
else -> UploadProgressUiState.Default
else -> UploadProgressUiState.Default(lastUploadedSize)
} ?: UploadProgressUiState.Error(lastUploadedSize)
}.filterNotNull()
}
Expand All @@ -134,8 +134,9 @@ class UploadWorker @AssistedInject constructor(
}
}

sealed class UploadProgressUiState(open val uploadedSize: Long = 0) {
data object Default : UploadProgressUiState()
sealed class UploadProgressUiState(open val uploadedSize: Long) {
@Immutable
data class Default(override val uploadedSize: Long = 0) : UploadProgressUiState(uploadedSize)

@Immutable
data class Progress(override val uploadedSize: Long) : UploadProgressUiState(uploadedSize) {
Expand Down

0 comments on commit 032984d

Please sign in to comment.