Skip to content

Commit

Permalink
fix: Show error in notification when upload is waiting for internet
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisCAD committed Mar 5, 2025
1 parent 4f08ae6 commit eae7df1
Showing 1 changed file with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.infomaniak.multiplatform_swisstransfer.network.exceptions.NetworkExce
import com.infomaniak.multiplatform_swisstransfer.utils.FileUtils
import com.infomaniak.swisstransfer.ui.screen.newtransfer.PickedFile
import com.infomaniak.swisstransfer.ui.utils.NotificationsUtils
import com.infomaniak.swisstransfer.upload.UploadState.Ongoing.Status
import com.infomaniak.swisstransfer.workers.FileChunkSizeManager
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.*
Expand Down Expand Up @@ -165,12 +166,7 @@ class UploadForegroundService : ForegroundService(Companion, redeliverIntentIfKi
// it should be because we have uris of picked/shared files, i.e. a transfer draft.
notificationsUtils.buildTransferDraftNotification()
}
is UploadState.Ongoing -> notificationsUtils.buildUploadProgressNotification(
authorEmail = state.info.authorEmail,
transferType = state.info.type,
totalBytes = state.info.totalSize,
uploadedBytes = state.uploadedBytes,
)
is UploadState.Ongoing -> buildOngoingNotification(state)
is UploadState.Retry -> {
notificationsUtils.buildUploadFailedNotification(canRetry = true)
}
Expand All @@ -182,14 +178,25 @@ class UploadForegroundService : ForegroundService(Companion, redeliverIntentIfKi
}
}

private fun buildOngoingNotification(state: UploadState.Ongoing): Notification = when (state.status) {
Status.InProgress, Status.Initializing -> notificationsUtils.buildUploadProgressNotification(
authorEmail = state.info.authorEmail,
transferType = state.info.type,
totalBytes = state.info.totalSize,
uploadedBytes = state.uploadedBytes,
)
//TODO[UL-retry]: Show the progress as above but change the message once we support retries.
Status.WaitingForInternet -> notificationsUtils.buildUploadFailedNotification(canRetry = true)
}

private var currentState by _state::value

override suspend fun run(): Nothing = Dispatchers.Default {
launch { keepNotificationUpToDate() }
repeatWhileActive {
val startRequest: StartUploadRequest = startSignal.receive()
currentState = UploadState.Ongoing(
status = UploadState.Ongoing.Status.Initializing,
status = Status.Initializing,
uploadedBytes = 0L,
info = startRequest.info
)
Expand All @@ -201,7 +208,7 @@ class UploadForegroundService : ForegroundService(Companion, redeliverIntentIfKi
val transferUuid = isInternetConnectedFlow.mapLatest { isInternetConnected ->
if (isInternetConnected.not()) {
currentState = uploadStateFlow.filterIsInstance<UploadState.Ongoing>().first().copy(
status = UploadState.Ongoing.Status.WaitingForInternet
status = Status.WaitingForInternet
)
awaitCancellation()
}
Expand Down Expand Up @@ -269,7 +276,7 @@ class UploadForegroundService : ForegroundService(Companion, redeliverIntentIfKi
val info = startRequest.info
updateState(
UploadState.Ongoing(
status = UploadState.Ongoing.Status.Initializing,
status = Status.Initializing,
uploadedBytes = 0L,
info = info,
)
Expand Down

0 comments on commit eae7df1

Please sign in to comment.