diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt index 58d5d05d6..e8c213786 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt @@ -148,8 +148,6 @@ private fun ImportFilesScreen( sendTransfer: () -> Unit, ) { val context = LocalContext.current - var shouldShowInitialFilePick by rememberSaveable { mutableStateOf(shouldStartByPromptingUserForFiles) } - var showTransferOption by rememberSaveable { mutableStateOf(null) } val importedFiles = files() val humanReadableSize = remember(importedFiles) { @@ -158,23 +156,6 @@ private fun ImportFilesScreen( getHumanReadableSize(context, spaceLeft) } - val filePickerLauncher = rememberLauncherForActivityResult( - contract = ActivityResultContracts.OpenMultipleDocuments() - ) { uris: List -> - addFiles(uris) - } - - fun pickFiles() { - shouldShowInitialFilePick = false - filePickerLauncher.launch(arrayOf("*/*")) - } - - fun closeTransferOption() { - showTransferOption = null - } - - LaunchedEffect(Unit) { if (shouldShowInitialFilePick) pickFiles() } - BottomStickyButtonScaffold( topBar = { SwissTransferTopAppBar( @@ -192,14 +173,7 @@ private fun ImportFilesScreen( .padding(vertical = Margin.Large) .verticalScroll(rememberScrollState()), ) { - ImportFilesTitle(modifier = Modifier.padding(horizontal = Margin.Medium), titleRes = R.string.myFilesTitle) - ImportedFilesCard( - modifier = Modifier.padding(Margin.Medium), - files = files, - humanReadableSize = { humanReadableSize }, - pickFiles = ::pickFiles, - removeFileByUid = removeFileByUid, - ) + FilesToImport(files, humanReadableSize, removeFileByUid, addFiles, shouldStartByPromptingUserForFiles) ImportTextFields( modifier = Modifier .padding(horizontal = Margin.Medium) @@ -207,30 +181,52 @@ private fun ImportFilesScreen( transferMessage = transferMessage, selectedTransferType = selectedTransferType.get, ) - ImportFilesTitle(Modifier.padding(Margin.Medium), titleRes = R.string.transferTypeTitle) - TransferTypeButtons(selectedTransferType) - ImportFilesTitle(Modifier.padding(Margin.Medium), titleRes = R.string.advancedSettingsTitle) - TransferOptionsTypes( - modifier = Modifier.padding(horizontal = Margin.Medium), - transferOptionsStates = transferOptionsCallbacks.transferOptionsStates, - onClick = { selectedOptionType -> showTransferOption = selectedOptionType }, - ) + SendByOptions(selectedTransferType) + TransferOptions(transferOptionsCallbacks) } - - TransferOptions({ showTransferOption }, transferOptionsCallbacks, ::closeTransferOption) } ) } +@Composable +private fun FilesToImport( + files: () -> List, + humanReadableSize: String, + removeFileByUid: (uid: String) -> Unit, + addFiles: (List) -> Unit, + shouldStartByPromptingUserForFiles: Boolean, +) { + var shouldShowInitialFilePick by rememberSaveable { mutableStateOf(shouldStartByPromptingUserForFiles) } + + val filePickerLauncher = rememberLauncherForActivityResult( + contract = ActivityResultContracts.OpenMultipleDocuments(), + onResult = addFiles, + ) + + fun pickFiles() { + shouldShowInitialFilePick = false + filePickerLauncher.launch(arrayOf("*/*")) + } + + LaunchedEffect(Unit) { if (shouldShowInitialFilePick) pickFiles() } + + ImportFilesTitle(modifier = Modifier.padding(horizontal = Margin.Medium), titleRes = R.string.myFilesTitle) + ImportedFilesCard( + modifier = Modifier.padding(Margin.Medium), + files = files, + humanReadableSize = { humanReadableSize }, + pickFiles = ::pickFiles, + removeFileByUid = removeFileByUid, + ) +} + @Composable private fun ColumnScope.ImportTextFields( modifier: Modifier, transferMessage: GetSetCallbacks, selectedTransferType: () -> TransferTypeUi, ) { - EmailAddressesTextFields(modifier, selectedTransferType) - SwissTransferTextField( modifier = modifier, label = stringResource(R.string.transferMessagePlaceholder), @@ -256,7 +252,31 @@ private fun ColumnScope.EmailAddressesTextFields(modifier: Modifier, selectedTra } @Composable -private fun TransferOptions( +private fun SendByOptions(selectedTransferType: GetSetCallbacks) { + ImportFilesTitle(Modifier.padding(Margin.Medium), titleRes = R.string.transferTypeTitle) + TransferTypeButtons(selectedTransferType) +} + +@Composable +private fun TransferOptions(transferOptionsCallbacks: TransferOptionsCallbacks) { + + var showTransferOption by rememberSaveable { mutableStateOf(null) } + + fun closeTransferOption() { + showTransferOption = null + } + + ImportFilesTitle(Modifier.padding(Margin.Medium), titleRes = R.string.advancedSettingsTitle) + TransferOptionsTypes( + modifier = Modifier.padding(horizontal = Margin.Medium), + transferOptionsStates = transferOptionsCallbacks.transferOptionsStates, + onClick = { selectedOptionType -> showTransferOption = selectedOptionType }, + ) + TransferOptionsDialogs({ showTransferOption }, transferOptionsCallbacks, ::closeTransferOption) +} + +@Composable +private fun TransferOptionsDialogs( selectedOptionType: () -> TransferOptionType?, transferOptionsCallbacks: TransferOptionsCallbacks, closeTransferOption: () -> Unit, @@ -290,6 +310,15 @@ private fun TransferOptions( } } +@Composable +private fun ImportFilesTitle(modifier: Modifier = Modifier, @StringRes titleRes: Int) { + Text( + modifier = modifier, + style = SwissTransferTheme.typography.bodySmallRegular, + text = stringResource(titleRes), + ) +} + @Composable private fun SendButton( filesToImportCount: () -> Int, @@ -320,15 +349,6 @@ private fun SendButton( ) } -@Composable -private fun ImportFilesTitle(modifier: Modifier = Modifier, @StringRes titleRes: Int) { - Text( - text = stringResource(titleRes), - style = SwissTransferTheme.typography.bodySmallRegular, - modifier = modifier, - ) -} - data class TransferOptionsCallbacks( val transferOptionsStates: () -> List, val onTransferOptionValueSelected: (SettingOption) -> Unit,