Skip to content

Commit

Permalink
review: Send Transfers into a callback instead of directly sending th…
Browse files Browse the repository at this point in the history
…e initial value
  • Loading branch information
KevinBoulongne committed Nov 13, 2024
1 parent 9d4d028 commit d05bbb3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ import com.infomaniak.swisstransfer.ui.utils.PreviewLightAndDark
fun TransferItemList(
modifier: Modifier = Modifier,
direction: TransferDirection,
transfers: List<TransferUi>,
getSelectedTransferUuid: () -> String?,
getTransfers: () -> List<TransferUi>,
onClick: (TransferUi) -> Unit,
) {

Expand All @@ -58,11 +58,11 @@ fun TransferItemList(
item { Text(stringResource(titleRes), style = SwissTransferTheme.typography.h1) }

items(
count = transfers.count(),
key = { transfers[it].uuid },
contentType = { transfers[it] },
count = getTransfers().count(),
key = { getTransfers()[it].uuid },
contentType = { getTransfers()[it] },
itemContent = {
val transfer = transfers[it]
val transfer = getTransfers()[it]
TransferItem(
transfer = transfer,
isSelected = { selectedTransferUuid == transfer.uuid },
Expand All @@ -79,9 +79,9 @@ private fun Preview(@PreviewParameter(TransferUiListPreviewParameter::class) tra
SwissTransferTheme {
Surface {
TransferItemList(
transfers = transfers,
direction = TransferDirection.SENT,
getSelectedTransferUuid = { null },
getTransfers = { transfers },
onClick = {},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ import java.util.Date
@Composable
fun TransfersListWithExpiredBottomSheet(
direction: TransferDirection,
transfers: List<TransferUi>,
navigateToDetails: (transferUuid: String) -> Unit,
getSelectedTransferUuid: () -> String?,
getTransfers: () -> List<TransferUi>,
) {

var isExpirySheetVisible: Boolean by rememberSaveable { mutableStateOf(false) }
Expand All @@ -50,8 +50,8 @@ fun TransfersListWithExpiredBottomSheet(
TransferItemList(
modifier = Modifier.padding(Margin.Medium),
direction = direction,
transfers = transfers,
getSelectedTransferUuid = getSelectedTransferUuid,
getTransfers = getTransfers,
onClick = { transfer ->
when {
transfer.expiresInDays < 0 -> {
Expand Down Expand Up @@ -89,9 +89,9 @@ private fun Preview(@PreviewParameter(TransferUiListPreviewParameter::class) tra
Surface {
TransfersListWithExpiredBottomSheet(
direction = TransferDirection.RECEIVED,
transfers = transfers,
navigateToDetails = {},
getSelectedTransferUuid = { null },
getTransfers = { transfers },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ 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.multiplatform_swisstransfer.common.interfaces.ui.TransferUi
import com.infomaniak.multiplatform_swisstransfer.common.models.TransferDirection
import com.infomaniak.swisstransfer.R
Expand All @@ -44,19 +43,20 @@ fun ReceivedScreen(
transfersViewModel: TransfersViewModel = hiltViewModel<TransfersViewModel>(),
) {

val transfers by transfersViewModel.receivedTransfers.collectAsStateWithLifecycle()
val areTransfersEmpty by remember { derivedStateOf { transfers.isEmpty() } }
val getTransfers = { transfersViewModel.receivedTransfers.value }

ReceivedScreen(navigateToDetails, getSelectedTransferUuid, transfers, areTransfersEmpty)
ReceivedScreen(navigateToDetails, getSelectedTransferUuid, getTransfers)
}

@Composable
private fun ReceivedScreen(
navigateToDetails: (transferUuid: String) -> Unit,
getSelectedTransferUuid: () -> String?,
transfers: List<TransferUi>,
areTransfersEmpty: Boolean,
getTransfers: () -> List<TransferUi>,
) {

val areTransfersEmpty by remember { derivedStateOf { getTransfers().isEmpty() } }

BrandTopAppBarScaffold(
floatingActionButton = { ReceivedEmptyFab { areTransfersEmpty } },
) {
Expand All @@ -67,7 +67,12 @@ private fun ReceivedScreen(
descriptionRes = R.string.noTransferReceivedDescription,
)
} else {
TransfersListWithExpiredBottomSheet(TransferDirection.RECEIVED, transfers, navigateToDetails, getSelectedTransferUuid)
TransfersListWithExpiredBottomSheet(
direction = TransferDirection.RECEIVED,
navigateToDetails = navigateToDetails,
getSelectedTransferUuid = getSelectedTransferUuid,
getTransfers = getTransfers,
)
}
}
}
Expand All @@ -80,8 +85,7 @@ private fun Preview() {
ReceivedScreen(
navigateToDetails = {},
getSelectedTransferUuid = { null },
transfers = emptyList(),
areTransfersEmpty = true,
getTransfers = { emptyList() },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.TransferUi
import com.infomaniak.multiplatform_swisstransfer.common.models.TransferDirection
import com.infomaniak.swisstransfer.ui.components.NewTransferFab
Expand All @@ -45,20 +44,19 @@ fun SentScreen(
transfersViewModel: TransfersViewModel = hiltViewModel<TransfersViewModel>(),
) {

val transfers by transfersViewModel.sentTransfers.collectAsStateWithLifecycle()
val areTransfersEmpty by remember { derivedStateOf { transfers.isEmpty() } }
val getTransfers = { transfersViewModel.sentTransfers.value }

SentScreen(navigateToDetails, getSelectedTransferUuid, transfers, areTransfersEmpty)
SentScreen(navigateToDetails, getSelectedTransferUuid, getTransfers)
}

@Composable
private fun SentScreen(
navigateToDetails: (transferUuid: String) -> Unit,
getSelectedTransferUuid: () -> String?,
transfers: List<TransferUi>,
areTransfersEmpty: Boolean,
getTransfers: () -> List<TransferUi>,
) {

val areTransfersEmpty by remember { derivedStateOf { getTransfers().isEmpty() } }
val windowAdaptiveInfo = currentWindowAdaptiveInfo()

BrandTopAppBarScaffold(
Expand All @@ -71,7 +69,12 @@ private fun SentScreen(
if (areTransfersEmpty) {
SentEmptyScreen()
} else {
TransfersListWithExpiredBottomSheet(TransferDirection.SENT, transfers, navigateToDetails, getSelectedTransferUuid)
TransfersListWithExpiredBottomSheet(
direction = TransferDirection.SENT,
navigateToDetails = navigateToDetails,
getSelectedTransferUuid = getSelectedTransferUuid,
getTransfers = getTransfers,
)
}
}
}
Expand All @@ -84,8 +87,7 @@ private fun Preview(@PreviewParameter(TransferUiListPreviewParameter::class) tra
SentScreen(
navigateToDetails = {},
getSelectedTransferUuid = { null },
transfers = transfers,
areTransfersEmpty = false,
getTransfers = { transfers },
)
}
}
Expand Down

0 comments on commit d05bbb3

Please sign in to comment.