Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: The Delete button in the Expired bottomSheet is now functional #218

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,41 @@ package com.infomaniak.swisstransfer.ui.components.transfer
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.PreviewParameter
import com.infomaniak.core2.FORMAT_DATE_SIMPLE
import com.infomaniak.core2.format
import com.infomaniak.multiplatform_swisstransfer.common.ext.toDateFromSeconds
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.TransferUi
import com.infomaniak.swisstransfer.R
import com.infomaniak.swisstransfer.ui.components.LargeButton
import com.infomaniak.swisstransfer.ui.components.SwissTransferBottomSheet
import com.infomaniak.swisstransfer.ui.images.AppImages.AppIcons
import com.infomaniak.swisstransfer.ui.images.AppImages.AppIllus
import com.infomaniak.swisstransfer.ui.images.icons.Bin
import com.infomaniak.swisstransfer.ui.images.illus.mascotDead.MascotDead
import com.infomaniak.swisstransfer.ui.previewparameter.TransferUiListPreviewParameter
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows
import java.util.Date

@Composable
fun TransferExpiredBottomSheet(
isVisible: () -> Boolean,
expirationDate: () -> Date?,
downloadsLimit: () -> Int?,
expiredTransfer: () -> TransferUi?,
onDeleteTransferClicked: () -> Unit,
closeBottomSheet: () -> Unit,
) {

if (!isVisible()) return
val transfer = expiredTransfer() ?: return

val date = expirationDate()
val descriptionText = if (date != null) {
val descriptionText = if (transfer.expiresInDays < 0) {
stringResource(
R.string.transferExpiredDateReachedDescription,
date.format(FORMAT_DATE_SIMPLE),
transfer.expirationDateTimestamp.toDateFromSeconds().format(FORMAT_DATE_SIMPLE),
)
} else {
stringResource(R.string.transferExpiredLimitReachedDescription, downloadsLimit()!!)
stringResource(
R.string.transferExpiredLimitReachedDescription,
transfer.downloadLimit,
)
}

SwissTransferBottomSheet(
Expand All @@ -61,7 +64,10 @@ fun TransferExpiredBottomSheet(
modifier = it,
titleRes = R.string.transferExpiredButton,
imageVector = AppIcons.Bin,
onClick = onDeleteTransferClicked,
onClick = {
onDeleteTransferClicked()
closeBottomSheet()
},
)
},
imageVector = AppIllus.MascotDead.image(),
Expand All @@ -72,13 +78,11 @@ fun TransferExpiredBottomSheet(

@PreviewAllWindows
@Composable
private fun Preview() {
private fun Preview(@PreviewParameter(TransferUiListPreviewParameter::class) transfers: List<TransferUi>) {
SwissTransferTheme {
Surface {
TransferExpiredBottomSheet(
isVisible = { true },
expirationDate = { Date() },
downloadsLimit = { 42 },
expiredTransfer = { transfers.first() },
onDeleteTransferClicked = {},
closeBottomSheet = {},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
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
Expand All @@ -27,61 +26,44 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.PreviewParameter
import com.infomaniak.multiplatform_swisstransfer.common.ext.toDateFromSeconds
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.TransferUi
import com.infomaniak.multiplatform_swisstransfer.common.models.TransferDirection
import com.infomaniak.swisstransfer.ui.previewparameter.TransferUiListPreviewParameter
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
import com.infomaniak.swisstransfer.ui.utils.isExpired

@Composable
fun TransfersListWithExpiredBottomSheet(
direction: TransferDirection,
navigateToDetails: (transferUuid: String) -> Unit,
getSelectedTransferUuid: () -> String?,
getTransfers: () -> List<TransferUi>,
onSwiped: (String) -> Unit,
onDeleteTransfer: (String) -> Unit,
) {

var isExpirySheetVisible: Boolean by rememberSaveable { mutableStateOf(false) }
var expirationDate: Date? by rememberSaveable { mutableStateOf(null) }
var downloadsLimit: Int? by rememberSaveable { mutableStateOf(null) }
var expiredTransfer: TransferUi? by rememberSaveable { mutableStateOf(null) }

TransferItemList(
modifier = Modifier.padding(Margin.Medium),
direction = direction,
getSelectedTransferUuid = getSelectedTransferUuid,
getTransfers = getTransfers,
onSwiped = onSwiped,
onSwiped = onDeleteTransfer,
onClick = { transfer ->
when {
transfer.expiresInDays < 0 -> {
isExpirySheetVisible = true
expirationDate = transfer.expirationDateTimestamp.toDateFromSeconds()
}
transfer.downloadLeft == 0 -> {
isExpirySheetVisible = true
downloadsLimit = transfer.downloadLimit
}
else -> {
navigateToDetails(transfer.uuid)
}
if (transfer.isExpired) {
expiredTransfer = transfer
} 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
},
expiredTransfer = { expiredTransfer },
onDeleteTransferClicked = { expiredTransfer?.uuid?.let(onDeleteTransfer) },
closeBottomSheet = { expiredTransfer = null },
)
}

Expand All @@ -95,7 +77,7 @@ private fun Preview(@PreviewParameter(TransferUiListPreviewParameter::class) tra
navigateToDetails = {},
getSelectedTransferUuid = { null },
getTransfers = { transfers },
onSwiped = {},
onDeleteTransfer = {},
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fun ReceivedScreen(
navigateToDetails = navigateToDetails,
getSelectedTransferUuid = getSelectedTransferUuid,
getTransfers = { transfers!! },
onSwiped = transfersViewModel::deleteTransfer,
onDeleteTransfer = transfersViewModel::deleteTransfer,
)
}
}
Expand All @@ -62,7 +62,7 @@ private fun ReceivedScreen(
navigateToDetails: (transferUuid: String) -> Unit,
getSelectedTransferUuid: () -> String?,
getTransfers: () -> List<TransferUi>,
onSwiped: (String) -> Unit,
onDeleteTransfer: (String) -> Unit,
) {

val areTransfersEmpty by remember { derivedStateOf { getTransfers().isEmpty() } }
Expand All @@ -82,7 +82,7 @@ private fun ReceivedScreen(
navigateToDetails = navigateToDetails,
getSelectedTransferUuid = getSelectedTransferUuid,
getTransfers = getTransfers,
onSwiped = onSwiped,
onDeleteTransfer = onDeleteTransfer,
)
}
}
Expand All @@ -97,7 +97,7 @@ private fun Preview() {
navigateToDetails = {},
getSelectedTransferUuid = { null },
getTransfers = { emptyList() },
onSwiped = {},
onDeleteTransfer = {},
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fun SentScreen(
navigateToDetails = navigateToDetails,
getSelectedTransferUuid = getSelectedTransferUuid,
getTransfers = { transfers!! },
onSwiped = transfersViewModel::deleteTransfer,
onDeleteTransfer = transfersViewModel::deleteTransfer,
)
}
}
Expand All @@ -69,7 +69,7 @@ private fun SentScreen(
navigateToDetails: (transferUuid: String) -> Unit,
getSelectedTransferUuid: () -> String?,
getTransfers: () -> List<TransferUi>,
onSwiped: (String) -> Unit,
onDeleteTransfer: (String) -> Unit,
) {

val areTransfersEmpty by remember { derivedStateOf { getTransfers().isEmpty() } }
Expand All @@ -90,7 +90,7 @@ private fun SentScreen(
navigateToDetails = navigateToDetails,
getSelectedTransferUuid = getSelectedTransferUuid,
getTransfers = getTransfers,
onSwiped = onSwiped,
onDeleteTransfer = onDeleteTransfer,
)
}
}
Expand All @@ -105,7 +105,7 @@ private fun Preview(@PreviewParameter(TransferUiListPreviewParameter::class) tra
navigateToDetails = {},
getSelectedTransferUuid = { null },
getTransfers = { transfers },
onSwiped = {},
onDeleteTransfer = {},
)
}
}
Expand Down
Loading