Skip to content

Commit

Permalink
refactor: Extract TransfersListWithExpiredBottomSheet to its own co…
Browse files Browse the repository at this point in the history
…mponent
  • Loading branch information
KevinBoulongne committed Nov 12, 2024
1 parent 3f45f68 commit 25edbd4
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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 },
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) },
) {
Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit 25edbd4

Please sign in to comment.