Skip to content

Commit

Permalink
refactor: Move generateNewUploadSession into it's own viewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
sirambd committed Nov 4, 2024
1 parent 30bab78 commit a5f060c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.infomaniak.multiplatform_swisstransfer.SwissTransferInjection
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.RemoteUploadFile
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadFileSession
import com.infomaniak.multiplatform_swisstransfer.common.models.EmailLanguage
import com.infomaniak.multiplatform_swisstransfer.common.utils.mapToList
import com.infomaniak.multiplatform_swisstransfer.data.NewUploadSession
import com.infomaniak.swisstransfer.di.IoDispatcher
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferType
Expand Down Expand Up @@ -94,10 +98,10 @@ class NewTransferViewModel @Inject constructor(
}
}

fun sendTransfer(newUploadSession: NewUploadSession) {
fun sendTransfer() {
viewModelScope.launch(ioDispatcher) {
runCatching {
uploadManager.createAndGetUpload(newUploadSession)
uploadManager.createAndGetUpload(generateNewUploadSession())
uploadWorkerScheduler.scheduleWork()
}.onFailure { exception ->
// TODO: Handle user feedback
Expand All @@ -106,6 +110,27 @@ class NewTransferViewModel @Inject constructor(
}
}

private fun generateNewUploadSession(): NewUploadSession {
return NewUploadSession(
duration = "30",
authorEmail = "",
password = "",
message = "sisi test",
numberOfDownload = 20,
language = EmailLanguage.ENGLISH,
recipientsEmails = emptyList(),
files = importationFilesManager.importedFiles.value.mapToList { fileUi ->
object : UploadFileSession {
override val localPath: String = fileUi.localPath ?: ""
override val mimeType: String = fileUi.mimeType ?: ""
override val name: String = fileUi.fileName
override val remoteUploadFile: RemoteUploadFile? = null
override val size: Long = fileUi.fileSize
}
}
)
}

companion object {
private const val IS_VIEW_MODEL_RESTORED_KEY = "IS_VIEW_MODEL_RESTORED_KEY"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.FileUi
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.RemoteUploadFile
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadFileSession
import com.infomaniak.multiplatform_swisstransfer.common.models.EmailLanguage
import com.infomaniak.multiplatform_swisstransfer.common.utils.mapToList
import com.infomaniak.multiplatform_swisstransfer.data.NewUploadSession
import com.infomaniak.swisstransfer.R
import com.infomaniak.swisstransfer.ui.components.*
import com.infomaniak.swisstransfer.ui.previewparameter.FileUiListPreviewParameter
Expand Down Expand Up @@ -81,8 +76,8 @@ fun ImportFilesScreen(
removeFileByUid = newTransferViewModel::removeFileByUid,
addFiles = newTransferViewModel::importFiles,
closeActivity = closeActivity,
navigateToUploadProgress = { newUploadSession ->
newTransferViewModel.sendTransfer(newUploadSession)
navigateToUploadProgress = {
newTransferViewModel.sendTransfer()
navigateToUploadProgress()
},
initialShowUploadSourceChoiceBottomSheet = true,
Expand All @@ -99,7 +94,7 @@ private fun ImportFilesScreen(
addFiles: (List<Uri>) -> Unit,
closeActivity: () -> Unit,
initialShowUploadSourceChoiceBottomSheet: Boolean,
navigateToUploadProgress: (NewUploadSession) -> Unit,
navigateToUploadProgress: () -> Unit,
) {
val context = LocalContext.current
var showUploadSourceChoiceBottomSheet by rememberSaveable { mutableStateOf(initialShowUploadSourceChoiceBottomSheet) }
Expand Down Expand Up @@ -127,7 +122,7 @@ private fun ImportFilesScreen(
},
topButton = { modifier ->
SendButton(filesToImportCount, currentSessionFilesCount, files, modifier) {
navigateToUploadProgress(generateNewUploadSession(importedFiles))
navigateToUploadProgress()
}
},
content = {
Expand Down Expand Up @@ -170,27 +165,6 @@ private fun ImportFilesScreen(
)
}

private fun generateNewUploadSession(importedFiles: List<FileUi>): NewUploadSession {
return NewUploadSession(
duration = "30",
authorEmail = "",
password = "",
message = "sisi test",
numberOfDownload = 20,
language = EmailLanguage.ENGLISH,
recipientsEmails = emptyList(),
files = importedFiles.mapToList { fileUi ->
object : UploadFileSession {
override val localPath: String = fileUi.localPath ?: ""
override val mimeType: String = fileUi.mimeType ?: ""
override val name: String = fileUi.fileName
override val remoteUploadFile: RemoteUploadFile? = null
override val size: Long = fileUi.fileSize
}
}
)
}

@Composable
private fun ColumnScope.ImportTextFields(selectedTransferType: () -> TransferType) {

Expand Down

0 comments on commit a5f060c

Please sign in to comment.