Skip to content

Commit

Permalink
feat: Implementing the "Retry" feature
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne committed Dec 20, 2024
1 parent 76643bc commit 269244a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ sealed class NewTransferNavigation : NavigationDestination() {
@Serializable
data class UploadSuccessDestination(val transferType: TransferTypeUi, val transferUrl: String) : NewTransferNavigation()
@Serializable
data object UploadErrorDestination : NewTransferNavigation()
data class UploadErrorDestination(val transferType: TransferTypeUi, val totalSize: Long) : NewTransferNavigation()

companion object {
val startDestination = ImportFilesDestination
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fun NewTransferNavHost(navController: NavHostController, closeActivity: () -> Un
navigateToUploadSuccess = { transferUrl ->
navController.navigate(UploadSuccessDestination(args.transferType, transferUrl))
},
navigateToUploadError = { navController.navigate(UploadErrorDestination) },
navigateToUploadError = { navController.navigate(UploadErrorDestination(args.transferType, args.totalSize)) },
navigateBackToImportFiles = { navController.popBackStack(route = ImportFilesDestination, inclusive = false) },
)
}
Expand All @@ -65,9 +65,18 @@ fun NewTransferNavHost(navController: NavHostController, closeActivity: () -> Un
)
}
composable<UploadErrorDestination> {
val args = it.toRoute<UploadErrorDestination>()
UploadErrorScreen(
retryTransfer = { /* TODO */ },
navigateBackToImportFiles = { navController.popBackStack(route = ImportFilesDestination, inclusive = false) },
navigateBackToUploadProgress = {
// TODO: Why doesn't this work ? It should, no ?? :(
// "NavController: Ignoring popBackStack to route UploadProgressDestination as it was not found on the current back stack"
// navController.popBackStack(route = UploadProgressDestination(args.transferType, args.totalSize), inclusive = false)
// TODO: Instead, I'm using this, which looks like it works fine. But this doesn't make sense to me.
navController.navigate(UploadProgressDestination(args.transferType, args.totalSize))
},
navigateBackToImportFiles = {
navController.popBackStack(route = ImportFilesDestination, inclusive = false)
},
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.infomaniak.swisstransfer.ui.screen.newtransfer.upload
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import com.infomaniak.swisstransfer.R
import com.infomaniak.swisstransfer.ui.components.*
import com.infomaniak.swisstransfer.ui.images.AppImages.AppIllus
Expand All @@ -29,22 +30,26 @@ import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows
import com.infomaniak.core2.R as RCore2

@Composable
fun UploadErrorScreen(retryTransfer: () -> Unit, navigateBackToImportFiles: () -> Unit) {
fun UploadErrorScreen(
navigateBackToUploadProgress: () -> Unit,
navigateBackToImportFiles: () -> Unit,
uploadProgressViewModel: UploadProgressViewModel = hiltViewModel<UploadProgressViewModel>(),
) {
BottomStickyButtonScaffold(
topBar = { BrandTopAppBar() },
topButton = {
LargeButton(
modifier = it,
title = stringResource(RCore2.string.buttonRetry),
onClick = retryTransfer,
onClick = { uploadProgressViewModel.resendLastTransfer(onCompletion = navigateBackToUploadProgress) },
)
},
bottomButton = {
LargeButton(
modifier = it,
title = stringResource(R.string.buttonEditTransfer),
style = ButtonType.SECONDARY,
onClick = navigateBackToImportFiles,
onClick = { uploadProgressViewModel.removeAllUploadSession(onCompletion = navigateBackToImportFiles) },
)
}
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,7 @@ fun UploadProgressScreen(
uploadProgressViewModel.trackUploadProgress()
}

HandleProgressState(
uiState = { uiState },
navigateToUploadSuccess = navigateToUploadSuccess,
navigateToUploadError = {
// If the transfer fails, we need to remove the upload session
// before going back to ImportFilesScreen to edit the transfer.
// TODO: This is possibly only mandatory for editing the transfer, and not retrying it.
// This needs to be checked when implementing the "Retry" feature.
uploadProgressViewModel.removeAllUploadSession()
navigateToUploadError()
},
navigateBackToImportFiles = navigateBackToImportFiles,
)
HandleProgressState({ uiState }, navigateToUploadSuccess, navigateToUploadError, navigateBackToImportFiles)

UploadProgressScreen(
progressState = { uiState },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,26 @@ import com.infomaniak.multiplatform_swisstransfer.managers.UploadManager
import com.infomaniak.network.NetworkAvailability
import com.infomaniak.sentry.SentryLog
import com.infomaniak.swisstransfer.di.IoDispatcher
import com.infomaniak.swisstransfer.di.MainDispatcher
import com.infomaniak.swisstransfer.ui.screen.newtransfer.TransferSendManager
import com.infomaniak.swisstransfer.workers.UploadWorker
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject

@HiltViewModel
class UploadProgressViewModel @Inject constructor(
@ApplicationContext private val appContext: Context,
private val uploadWorkerScheduler: UploadWorker.Scheduler,
private val uploadManager: UploadManager,
private val transferSendManager: TransferSendManager,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
@MainDispatcher private val mainDispatcher: CoroutineDispatcher,
) : ViewModel() {

val isNetworkAvailable = NetworkAvailability(appContext).isNetworkAvailable
Expand Down Expand Up @@ -91,8 +96,18 @@ class UploadProgressViewModel @Inject constructor(
}
}

fun removeAllUploadSession() {
viewModelScope.launch(ioDispatcher) { uploadManager.removeAllUploadSession() }
fun removeAllUploadSession(onCompletion: () -> Unit) {
viewModelScope.launch(ioDispatcher) {
uploadManager.removeAllUploadSession()
withContext(mainDispatcher) { onCompletion() }
}
}

fun resendLastTransfer(onCompletion: () -> Unit) {
viewModelScope.launch(ioDispatcher) {
transferSendManager.resendLastTransfer()
withContext(mainDispatcher) { onCompletion() }
}
}

companion object {
Expand Down

0 comments on commit 269244a

Please sign in to comment.