Skip to content

Commit

Permalink
chore(ImportFileScreen): Dissect the screen content in sub component
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Nov 28, 2024
1 parent 7c7a33d commit c92e266
Showing 1 changed file with 69 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ private fun ImportFilesScreen(
sendTransfer: () -> Unit,
) {
val context = LocalContext.current
var shouldShowInitialFilePick by rememberSaveable { mutableStateOf(shouldStartByPromptingUserForFiles) }
var showTransferOption by rememberSaveable { mutableStateOf<TransferOptionType?>(null) }

val importedFiles = files()
val humanReadableSize = remember(importedFiles) {
Expand All @@ -158,23 +156,6 @@ private fun ImportFilesScreen(
getHumanReadableSize(context, spaceLeft)
}

val filePickerLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.OpenMultipleDocuments()
) { uris: List<Uri> ->
addFiles(uris)
}

fun pickFiles() {
shouldShowInitialFilePick = false
filePickerLauncher.launch(arrayOf("*/*"))
}

fun closeTransferOption() {
showTransferOption = null
}

LaunchedEffect(Unit) { if (shouldShowInitialFilePick) pickFiles() }

BottomStickyButtonScaffold(
topBar = {
SwissTransferTopAppBar(
Expand All @@ -192,45 +173,60 @@ 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)
.fillMaxWidth(),
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<FileUi>,
humanReadableSize: String,
removeFileByUid: (uid: String) -> Unit,
addFiles: (List<Uri>) -> 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<String>,
selectedTransferType: () -> TransferTypeUi,
) {

EmailAddressesTextFields(modifier, selectedTransferType)

SwissTransferTextField(
modifier = modifier,
label = stringResource(R.string.transferMessagePlaceholder),
Expand All @@ -256,7 +252,31 @@ private fun ColumnScope.EmailAddressesTextFields(modifier: Modifier, selectedTra
}

@Composable
private fun TransferOptions(
private fun SendByOptions(selectedTransferType: GetSetCallbacks<TransferTypeUi>) {
ImportFilesTitle(Modifier.padding(Margin.Medium), titleRes = R.string.transferTypeTitle)
TransferTypeButtons(selectedTransferType)
}

@Composable
private fun TransferOptions(transferOptionsCallbacks: TransferOptionsCallbacks) {

var showTransferOption by rememberSaveable { mutableStateOf<TransferOptionType?>(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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<TransferOptionState>,
val onTransferOptionValueSelected: (SettingOption) -> Unit,
Expand Down

0 comments on commit c92e266

Please sign in to comment.