From abde5b92ada87ea3236d9c200dec580aed4a82b2 Mon Sep 17 00:00:00 2001 From: Gibran Chevalley Date: Mon, 14 Oct 2024 08:59:23 +0200 Subject: [PATCH 1/2] Use a PreviewParameter to factorize FileUiItem instantiation inside previews --- .../swisstransfer/ui/components/FileItem.kt | 20 ++------ .../ui/components/FileItemList.kt | 24 ++------- .../ui/components/SmallFileItem.kt | 22 +++------ .../FileUiListPreviewParameterProvider.kt | 49 +++++++++++++++++++ .../importfiles/ImportFilesScreen.kt | 16 ++---- .../components/ImportedFilesCard.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 e338afb5f..f5f6a2c48 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.CustomShapes import com.infomaniak.swisstransfer.ui.theme.Margin import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme @@ -129,7 +131,7 @@ private fun FileItemContent( @PreviewAllWindows @Composable -private fun FileItemPreview() { +private fun FileItemPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List) { SwissTransferTheme { Surface { Column( @@ -140,13 +142,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, @@ -158,13 +154,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 35299de01..20acf8f84 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.PreviewAllWindows @@ -59,27 +61,7 @@ fun FileItemList( @PreviewAllWindows @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/SmallFileItem.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/components/SmallFileItem.kt index 1811567c7..979460b3a 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/components/SmallFileItem.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/components/SmallFileItem.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.CustomShapes 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 SmallFileItemPreview() { +private fun SmallFileItemPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List) { SwissTransferTheme { Surface(color = SwissTransferTheme.materialColors.surfaceContainerHighest) { Column(Modifier.padding(16.dp)) { + val file = files[0] + SmallFileItem( - 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 SmallFileItemPreview() { Spacer(modifier = Modifier.height(16.dp)) SmallFileItem( - 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 7e2314780..43648927f 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 @@ -25,10 +25,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.screen.newtransfer.importfiles.components.ImportedFilesCard import com.infomaniak.swisstransfer.ui.theme.Margin @@ -109,18 +111,8 @@ private fun ImportFilesScreen( @PreviewAllWindows @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/components/ImportedFilesCard.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/components/ImportedFilesCard.kt index c8fc22e65..ab45c3041 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/components/ImportedFilesCard.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/components/ImportedFilesCard.kt @@ -30,12 +30,14 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier 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.CustomShapes import com.infomaniak.swisstransfer.ui.theme.Margin import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme @@ -138,21 +140,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 ImportededFilesCardPreview() { +private fun ImportededFilesCardPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List) { SwissTransferTheme { ImportedFilesCard( 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 }, humanReadableSize = { "20 GB" }, showUploadSourceChoiceBottomSheet = {}, removeFileByUid = {} From a13a199d187098998be3fbdb8c3e7cd01cfefdaa Mon Sep 17 00:00:00 2001 From: Gibran Chevalley Date: Fri, 18 Oct 2024 10:34:15 +0200 Subject: [PATCH 2/2] Replace [0] accessor with .first() --- .../com/infomaniak/swisstransfer/ui/components/SmallFileItem.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/components/SmallFileItem.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/components/SmallFileItem.kt index 979460b3a..07711333c 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/components/SmallFileItem.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/components/SmallFileItem.kt @@ -71,7 +71,7 @@ private fun SmallFileItemPreview(@PreviewParameter(FileUiListPreviewParameter::c SwissTransferTheme { Surface(color = SwissTransferTheme.materialColors.surfaceContainerHighest) { Column(Modifier.padding(16.dp)) { - val file = files[0] + val file = files.first() SmallFileItem( file = file,