Skip to content

Commit

Permalink
Close activity and cancel upload when cancel button is clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarX authored and sirambd committed Nov 4, 2024
1 parent af3b4de commit b8f0b7a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fun NewTransferNavHost(navController: NavHostController, closeActivity: () -> Un
ValidateUserEmailScreen()
}
composable<UploadProgressDestination> {
UploadProgressScreen()
UploadProgressScreen(closeActivity = closeActivity)
}
composable<UploadSuccessDestination> {
// TODO: Use correct TransferType instead of hard-coded value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows
import com.infomaniak.swisstransfer.workers.UploadWorker

@Composable
fun UploadProgressScreen(uploadProgressViewModel: UploadProgressViewModel = hiltViewModel<UploadProgressViewModel>()) {
fun UploadProgressScreen(
uploadProgressViewModel: UploadProgressViewModel = hiltViewModel<UploadProgressViewModel>(),
closeActivity: () -> Unit,
) {
val progress by uploadProgressViewModel.progress.collectAsStateWithLifecycle()

val adScreenType = rememberSaveable { UploadProgressAdType.entries.random() }
Expand All @@ -62,7 +65,8 @@ fun UploadProgressScreen(uploadProgressViewModel: UploadProgressViewModel = hilt
showBottomSheet = GetSetCallbacks(get = { showBottomSheet }, set = { showBottomSheet = it }),
adScreenType = adScreenType,
onCancel = {
// TODO
uploadProgressViewModel.cancelUpload()
closeActivity()
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,24 @@ package com.infomaniak.swisstransfer.ui.screen.newtransfer.upload

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.infomaniak.multiplatform_swisstransfer.SwissTransferInjection
import com.infomaniak.swisstransfer.di.IoDispatcher
import com.infomaniak.swisstransfer.workers.UploadWorker
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class UploadProgressViewModel @Inject constructor(
uploadWorkerScheduler: UploadWorker.Scheduler,
@IoDispatcher ioDispatcher: CoroutineDispatcher,
private val uploadWorkerScheduler: UploadWorker.Scheduler,
private val swissTransferInjection: SwissTransferInjection,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : ViewModel() {
val uploadedSizeInBytes: MutableStateFlow<Long> = MutableStateFlow(9_842_314L)
val totalSizeInBytes: MutableStateFlow<Long> = MutableStateFlow(12_342_314L)
private val uploadManager inline get() = swissTransferInjection.uploadManager

val progress = uploadWorkerScheduler.trackUploadProgressFlow()
.flowOn(ioDispatcher)
Expand All @@ -44,4 +45,14 @@ class UploadProgressViewModel @Inject constructor(
started = SharingStarted.Eagerly,
initialValue = UploadWorker.UploadTransferProgress(0, 0)
)

fun cancelUpload() {
uploadWorkerScheduler.cancelWork()

viewModelScope.launch(ioDispatcher) {
uploadManager.getLastUpload()?.let {
uploadManager.deleteUploadSession(it.uuid)
}
}
}
}

0 comments on commit b8f0b7a

Please sign in to comment.