From c24eb138ec3c27b2b3a11b8658dc215e5b1a4d9d Mon Sep 17 00:00:00 2001 From: Gibran Chevalley Date: Mon, 14 Oct 2024 08:59:23 +0200 Subject: [PATCH] Use a PreviewParameter to factorize FileUiItem instantiation inside previews --- .../swisstransfer/ui/components/FileItem.kt | 20 ++------ .../ui/components/FileItemList.kt | 24 ++------- .../ui/components/SmallFileTile.kt | 22 +++------ .../FileUiListPreviewParameterProvider.kt | 49 +++++++++++++++++++ .../importfiles/ImportFilesScreen.kt | 16 ++---- .../importfiles/SelectedFilesCard.kt | 16 ++---- 6 files changed, 72 insertions(+), 75 deletions(-) create mode 100644 app/src/main/java/com/infomaniak/swisstransfer/ui/previewparameter/FileUiListPreviewParameterProvider.kt diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/components/FileItem.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/components/FileItem.kt index 97f2e25a0..ee99bb27a 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/components/FileItem.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/components/FileItem.kt @@ -27,7 +27,9 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.compose.ui.unit.dp +import com.infomaniak.swisstransfer.ui.previewparameter.FileUiListPreviewParameter import com.infomaniak.swisstransfer.ui.theme.Margin import com.infomaniak.swisstransfer.ui.theme.Shapes import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme @@ -131,7 +133,7 @@ private fun FileItemContent( @PreviewSmallWindow @PreviewLargeWindow @Composable -private fun FileItemPreview() { +private fun FileItemPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List) { SwissTransferTheme { Surface { Column( @@ -142,13 +144,7 @@ private fun FileItemPreview() { { var isChecked by remember { mutableStateOf(true) } - val iconFile = object : FileUiItem { - override val fileName: String = "How to not get fired.pdf" - override val uid: String = fileName - override val fileSizeInBytes: Long = 10302130 - override val mimeType: String? = null - override val uri: String = "" - } + val iconFile = files[0] FileItem( iconFile, isRemoveButtonVisible = true, @@ -160,13 +156,7 @@ private fun FileItemPreview() { Spacer(modifier = Modifier.height(16.dp)) - val imageFile = object : FileUiItem { - override val fileName: String = "Time-Clock-Circle--Streamline-Ultimate.svg (1).png" - override val uid: String = fileName - override val fileSizeInBytes: Long = 456782 - override val mimeType: String? = null - override val uri: String = "https://picsum.photos/200/300" - } + val imageFile = files[1] FileItem( file = imageFile, isRemoveButtonVisible = true, diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/components/FileItemList.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/components/FileItemList.kt index 8a7dbbbfd..991762704 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/components/FileItemList.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/components/FileItemList.kt @@ -23,7 +23,9 @@ import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.lazy.grid.items import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier +import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.compose.ui.unit.dp +import com.infomaniak.swisstransfer.ui.previewparameter.FileUiListPreviewParameter import com.infomaniak.swisstransfer.ui.theme.Margin import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme import com.infomaniak.swisstransfer.ui.utils.PreviewLargeWindow @@ -61,27 +63,7 @@ fun FileItemList( @PreviewSmallWindow @PreviewLargeWindow @Composable -private fun FileItemListPreview() { - val files = listOf(object : FileUiItem { - override val fileName: String = "The 5-Step Guide to Not Breaking Your Code.txt" - override val uid: String = fileName - override val fileSizeInBytes: Long = 57689032 - override val mimeType: String? = null - override val uri: String = "" - }, object : FileUiItem { - override val fileName: String = "Introduction to Turning It Off and On Again.pptx" - override val uid: String = fileName - override val fileSizeInBytes: Long = 89723143 - override val mimeType: String? = null - override val uri: String = "" - }, object : FileUiItem { - override val fileName: String = "Learning to Copy and Paste: A Complete Guide.docx" - override val uid: String = fileName - override val fileSizeInBytes: Long = 237866728 - override val mimeType: String? = null - override val uri: String = "" - }) - +private fun FileItemListPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List) { SwissTransferTheme { FileItemList( files = files, diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/components/SmallFileTile.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/components/SmallFileTile.kt index 49d123802..f1f0ee6fc 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/components/SmallFileTile.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/components/SmallFileTile.kt @@ -26,9 +26,11 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Shape import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.times +import com.infomaniak.swisstransfer.ui.previewparameter.FileUiListPreviewParameter import com.infomaniak.swisstransfer.ui.theme.Shapes import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme @@ -65,18 +67,14 @@ enum class SmallFileTileSize(val size: Dp, val shape: Shape) { @Preview(name = "Light") @Preview(name = "Dark", uiMode = Configuration.UI_MODE_NIGHT_YES or Configuration.UI_MODE_TYPE_NORMAL) @Composable -private fun SmallFileTilePreview() { +private fun SmallFileTilePreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List) { SwissTransferTheme { Surface(color = SwissTransferTheme.materialColors.surfaceContainerHighest) { Column(Modifier.padding(16.dp)) { + val file = files[0] + SmallFileTile( - file = object : FileUiItem { - override val fileName: String = "How to not get fired.pdf" - override val uid: String = fileName - override val fileSizeInBytes: Long = 10302130 - override val mimeType: String? = null - override val uri: String = "" - }, + file = file, smallFileTileSize = SmallFileTileSize.LARGE, onRemove = {} ) @@ -84,13 +82,7 @@ private fun SmallFileTilePreview() { Spacer(modifier = Modifier.height(16.dp)) SmallFileTile( - file = object : FileUiItem { - override val fileName: String = "How to not get fired.pdf" - override val uid: String = fileName - override val fileSizeInBytes: Long = 10302130 - override val mimeType: String? = null - override val uri: String = "" - }, + file = file, smallFileTileSize = SmallFileTileSize.SMALL, ) } diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/previewparameter/FileUiListPreviewParameterProvider.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/previewparameter/FileUiListPreviewParameterProvider.kt new file mode 100644 index 000000000..5a3a453ce --- /dev/null +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/previewparameter/FileUiListPreviewParameterProvider.kt @@ -0,0 +1,49 @@ +/* + * Infomaniak SwissTransfer - Android + * Copyright (C) 2024 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.infomaniak.swisstransfer.ui.previewparameter + +import androidx.compose.ui.tooling.preview.PreviewParameterProvider +import com.infomaniak.swisstransfer.ui.components.FileUiItem + +class FileUiListPreviewParameter : PreviewParameterProvider> { + override val values: Sequence> = sequenceOf( + listOf( + object : FileUiItem { // Non-image file + override val fileName: String = "How to not get fired.pdf" + override val uid: String = fileName + override val fileSizeInBytes: Long = 10302130 + override val mimeType: String? = null + override val uri: String = "" + }, + object : FileUiItem { // Image file + override val fileName: String = "Opening images tutorial.png" + override val uid: String = fileName + override val fileSizeInBytes: Long = 456782 + override val mimeType: String? = null + override val uri: String = "https://picsum.photos/200/300" + }, + object : FileUiItem { + override val fileName: String = "The 5 step guide to turning it off and on again.docx" + override val uid: String = fileName + override val fileSizeInBytes: Long = 89723143 + override val mimeType: String? = null + override val uri: String = "" + }, + ) + ) +} 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 ace5600c9..7f896622d 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 @@ -27,10 +27,12 @@ import androidx.compose.runtime.* import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.infomaniak.swisstransfer.R import com.infomaniak.swisstransfer.ui.components.* +import com.infomaniak.swisstransfer.ui.previewparameter.FileUiListPreviewParameter import com.infomaniak.swisstransfer.ui.screen.newtransfer.NewTransferViewModel import com.infomaniak.swisstransfer.ui.theme.Margin import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme @@ -114,18 +116,8 @@ private fun getFormattedSizeWithUnits(usedSpace: Long, context: Context): String @PreviewSmallWindow @PreviewLargeWindow @Composable -private fun ImportFilesScreenPreview() { +private fun ImportFilesScreenPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List) { SwissTransferTheme { - ImportFilesScreen({ - listOf( - object : FileUiItem { - override val uid = "" - override val fileName = "Time-Clock-Circle--Streamline-Ultimate.svg (1).svg" - override val fileSizeInBytes = 2367832L - override val mimeType = null - override val uri = "" - } - ) - }, {}, {}, closeActivity = {}) + ImportFilesScreen({ files }, {}, {}, closeActivity = {}) } } diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/SelectedFilesCard.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/SelectedFilesCard.kt index e52d9b55d..5a57d0e9a 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/SelectedFilesCard.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/SelectedFilesCard.kt @@ -36,12 +36,14 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.pluralStringResource import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.compose.ui.unit.dp import com.infomaniak.swisstransfer.R import com.infomaniak.swisstransfer.ui.components.* import com.infomaniak.swisstransfer.ui.images.AppImages import com.infomaniak.swisstransfer.ui.images.icons.AddThick import com.infomaniak.swisstransfer.ui.images.icons.ChevronRightSmall +import com.infomaniak.swisstransfer.ui.previewparameter.FileUiListPreviewParameter import com.infomaniak.swisstransfer.ui.theme.Margin import com.infomaniak.swisstransfer.ui.theme.Shapes import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme @@ -161,21 +163,11 @@ private data class TransferLazyRowKey( @Preview(name = "Light") @Preview(name = "Dark", uiMode = Configuration.UI_MODE_NIGHT_YES or Configuration.UI_MODE_TYPE_NORMAL) @Composable -private fun SelectedFilesCardPreview() { +private fun SelectedFilesCardPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List) { SwissTransferTheme { SelectedFilesCard( modifier = Modifier.padding(Margin.Medium), - files = { - listOf( - object : FileUiItem { - override val uid = "" - override val fileName = "Time-Clock-Circle--Streamline-Ultimate.svg (1).svg" - override val fileSizeInBytes = 234567832L - override val mimeType = null - override val uri = "" - } - ) - }, + files = { files }, formattedSizeWithUnits = { "20 GB" }, showUploadSourceChoiceBottomSheet = {}, removeFileByUid = {}