From decc81bf4be3e1469e2463b14f3df7d8087565c4 Mon Sep 17 00:00:00 2001 From: Kevin Boulongne Date: Tue, 12 Nov 2024 16:14:24 +0100 Subject: [PATCH] refactor: Extract `TransfersListWithExpiredBottomSheet` to its own component --- .../TransfersListWithExpiredBottomSheet.kt | 90 +++++++++++++++++++ .../ui/screen/main/received/ReceivedScreen.kt | 56 ++---------- 2 files changed, 96 insertions(+), 50 deletions(-) create mode 100644 app/src/main/java/com/infomaniak/swisstransfer/ui/components/transfer/TransfersListWithExpiredBottomSheet.kt diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/components/transfer/TransfersListWithExpiredBottomSheet.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/components/transfer/TransfersListWithExpiredBottomSheet.kt new file mode 100644 index 000000000..3e32883f5 --- /dev/null +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/components/transfer/TransfersListWithExpiredBottomSheet.kt @@ -0,0 +1,90 @@ +/* + * 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.components.transfer + +import android.util.Log +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Surface +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import com.infomaniak.swisstransfer.ui.previewparameter.transfersPreviewData +import com.infomaniak.swisstransfer.ui.theme.Margin +import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme +import com.infomaniak.swisstransfer.ui.utils.PreviewLightAndDark +import java.util.Date + +@Composable +fun TransfersListWithExpiredBottomSheet( + navigateToDetails: (transferUuid: String) -> Unit, + getSelectedTransferUuid: () -> String?, +) { + + var isExpirySheetVisible: Boolean by rememberSaveable { mutableStateOf(false) } + var expirationDate: Date? by rememberSaveable { mutableStateOf(null) } + var downloadsLimit: Int? by rememberSaveable { mutableStateOf(null) } + + TransferItemList( + modifier = Modifier.padding(Margin.Medium), + transfers = transfersPreviewData, // TODO: Use real data + getSelectedTransferUuid = getSelectedTransferUuid, + onClick = { transfer -> + when { + transfer.expiresInDays < 0 -> { + isExpirySheetVisible = true + expirationDate = Date(transfer.expirationDateTimestamp) + } + transfer.downloadLeft == 0 -> { + isExpirySheetVisible = true + downloadsLimit = transfer.downloadLimit + } + else -> { + navigateToDetails(transfer.uuid) + } + } + } + ) + + TransferExpiredBottomSheet( + isVisible = { isExpirySheetVisible }, + expirationDate = { expirationDate }, + downloadsLimit = { downloadsLimit }, + onDeleteTransferClicked = { Log.d("TODO", "Delete expired Transfer") }, // TODO + closeBottomSheet = { + isExpirySheetVisible = false + expirationDate = null + downloadsLimit = null + }, + ) +} + +@PreviewLightAndDark +@Composable +private fun Preview() { + SwissTransferTheme { + Surface { + TransfersListWithExpiredBottomSheet( + navigateToDetails = {}, + getSelectedTransferUuid = { null }, + ) + } + } +} diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/received/ReceivedScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/received/ReceivedScreen.kt index da7ebdbf2..3dfb9a647 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/received/ReceivedScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/received/ReceivedScreen.kt @@ -17,28 +17,23 @@ */ package com.infomaniak.swisstransfer.ui.screen.main.received -import android.util.Log -import androidx.compose.foundation.layout.padding import androidx.compose.material3.Surface -import androidx.compose.runtime.* -import androidx.compose.runtime.saveable.rememberSaveable -import androidx.compose.ui.Modifier +import androidx.compose.runtime.Composable +import androidx.compose.runtime.derivedStateOf +import androidx.compose.runtime.getValue +import androidx.compose.runtime.remember import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.infomaniak.swisstransfer.R import com.infomaniak.swisstransfer.ui.components.EmptyState -import com.infomaniak.swisstransfer.ui.components.transfer.TransferExpiredBottomSheet -import com.infomaniak.swisstransfer.ui.components.transfer.TransferItemList +import com.infomaniak.swisstransfer.ui.components.transfer.TransfersListWithExpiredBottomSheet import com.infomaniak.swisstransfer.ui.images.AppImages.AppIllus import com.infomaniak.swisstransfer.ui.images.illus.MascotSearching -import com.infomaniak.swisstransfer.ui.previewparameter.transfersPreviewData import com.infomaniak.swisstransfer.ui.screen.main.components.BrandTopAppBarScaffold import com.infomaniak.swisstransfer.ui.screen.main.received.components.ReceivedEmptyFab import com.infomaniak.swisstransfer.ui.screen.main.sent.SentViewModel -import com.infomaniak.swisstransfer.ui.theme.Margin import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows -import java.util.Date @Composable fun ReceivedScreen( @@ -62,11 +57,6 @@ private fun ReceivedScreen( getSelectedTransferUuid: () -> String?, areTransfersEmpty: () -> Boolean, ) { - - var isVisible: Boolean by rememberSaveable { mutableStateOf(false) } - var expirationDate: Date? by rememberSaveable { mutableStateOf(null) } - var downloadsLimit: Int? by rememberSaveable { mutableStateOf(null) } - BrandTopAppBarScaffold( floatingActionButton = { ReceivedEmptyFab(areTransfersEmpty) }, ) { @@ -77,41 +67,7 @@ private fun ReceivedScreen( descriptionRes = R.string.noTransferReceivedDescription, ) } else { - TransferItemList( - modifier = Modifier.padding(Margin.Medium), - transfers = transfersPreviewData, // TODO: Use real data - getSelectedTransferUuid = getSelectedTransferUuid, - onClick = { transfer -> - when { - transfer.expiresInDays < 0 -> { - isVisible = true - expirationDate = Date(transfer.expirationDateTimestamp) - } - transfer.downloadLeft == 0 -> { - isVisible = true - downloadsLimit = transfer.downloadLimit - } - else -> { - navigateToDetails(transfer.uuid) - } - } - } - ) - - TransferExpiredBottomSheet( - isVisible = { isVisible }, - expirationDate = { expirationDate }, - downloadsLimit = { downloadsLimit }, - onDeleteTransferClicked = { - Log.d("TODO", "Delete expired Transfer") - // TODO - }, - closeBottomSheet = { - isVisible = false - expirationDate = null - downloadsLimit = null - }, - ) + TransfersListWithExpiredBottomSheet(navigateToDetails, getSelectedTransferUuid) } } }