Skip to content

Commit

Permalink
refactor: Move humanReadableFileSize inside the FileImport Component
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Dec 3, 2024
1 parent f7c03d5 commit a15b681
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.hilt.navigation.compose.hiltViewModel
Expand All @@ -46,15 +45,11 @@ import com.infomaniak.swisstransfer.ui.screen.main.settings.components.SettingOp
import com.infomaniak.swisstransfer.ui.screen.newtransfer.ImportFilesViewModel
import com.infomaniak.swisstransfer.ui.screen.newtransfer.ImportFilesViewModel.SendActionResult
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.*
import com.infomaniak.swisstransfer.ui.theme.Dimens
import com.infomaniak.swisstransfer.ui.theme.Margin
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import com.infomaniak.swisstransfer.ui.utils.GetSetCallbacks
import com.infomaniak.swisstransfer.ui.utils.HumanReadableSizeUtils.getHumanReadableSize
import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows

private const val TOTAL_FILE_SIZE: Long = 50_000_000_000L

@Composable
fun ImportFilesScreen(
importFilesViewModel: ImportFilesViewModel = hiltViewModel<ImportFilesViewModel>(),
Expand Down Expand Up @@ -148,15 +143,6 @@ private fun ImportFilesScreen(
shouldStartByPromptingUserForFiles: Boolean,
sendTransfer: () -> Unit,
) {
val context = LocalContext.current

val importedFiles = files()
val humanReadableSize = remember(importedFiles) {
val usedSpace = importedFiles.sumOf { it.fileSize }
val spaceLeft = (TOTAL_FILE_SIZE - usedSpace).coerceAtLeast(0)
getHumanReadableSize(context, spaceLeft)
}

BottomStickyButtonScaffold(
topBar = {
SwissTransferTopAppBar(
Expand All @@ -169,13 +155,8 @@ private fun ImportFilesScreen(
SendButton(filesToImportCount, currentSessionFilesCount, files, modifier, sendTransfer)
},
content = {
Column(
modifier = Modifier
.widthIn(max = Dimens.MaxSinglePaneScreenWidth)
.padding(vertical = Margin.Small)
.verticalScroll(rememberScrollState()),
) {
FilesToImport(files, humanReadableSize, removeFileByUid, addFiles, shouldStartByPromptingUserForFiles)
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
FilesToImport(files, removeFileByUid, addFiles, shouldStartByPromptingUserForFiles)
ImportTextFields(
modifier = Modifier
.padding(horizontal = Margin.Medium)
Expand All @@ -193,7 +174,6 @@ private fun ImportFilesScreen(
@Composable
private fun FilesToImport(
files: () -> List<FileUi>,
humanReadableSize: String,
removeFileByUid: (uid: String) -> Unit,
addFiles: (List<Uri>) -> Unit,
shouldStartByPromptingUserForFiles: Boolean,
Expand All @@ -216,7 +196,6 @@ private fun FilesToImport(
ImportedFilesCard(
modifier = Modifier.padding(Margin.Medium),
files = files,
humanReadableSize = { humanReadableSize },
pickFiles = ::pickFiles,
removeFileByUid = removeFileByUid,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
Expand All @@ -40,25 +44,38 @@ import com.infomaniak.swisstransfer.ui.theme.CustomShapes
import com.infomaniak.swisstransfer.ui.theme.Margin
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import com.infomaniak.swisstransfer.ui.utils.HumanReadableSizeUtils.formatSpaceLeft
import com.infomaniak.swisstransfer.ui.utils.HumanReadableSizeUtils.getHumanReadableSize
import com.infomaniak.swisstransfer.ui.utils.PreviewLightAndDark
import kotlinx.parcelize.Parcelize

private const val TOTAL_FILE_SIZE: Long = 50_000_000_000L

@Composable
fun ImportedFilesCard(
modifier: Modifier = Modifier,
files: () -> List<FileUi>,
humanReadableSize: () -> String,
pickFiles: () -> Unit,
removeFileByUid: (uid: String) -> Unit,
) {

val context = LocalContext.current

val humanReadableSize by remember {
derivedStateOf {
val usedSpace = files().sumOf { it.fileSize }
val spaceLeft = (TOTAL_FILE_SIZE - usedSpace).coerceAtLeast(0)
getHumanReadableSize(context, spaceLeft)
}
}

SwissTransferCard(modifier) {
SharpRippleButton(onClick = { /* TODO */ }) {
TextDotText(
firstText = {
val fileCount = files().count()
pluralStringResource(R.plurals.filesCount, fileCount, fileCount)
},
secondText = { formatSpaceLeft(humanReadableSize) },
secondText = { formatSpaceLeft { humanReadableSize } },
modifier = Modifier.padding(start = Margin.Medium),
)
Spacer(Modifier.weight(1.0f))
Expand Down Expand Up @@ -130,7 +147,6 @@ private fun ImportedFilesCardPreview(@PreviewParameter(FileUiListPreviewParamete
ImportedFilesCard(
modifier = Modifier.padding(Margin.Medium),
files = { files },
humanReadableSize = { "20 GB" },
pickFiles = {},
removeFileByUid = {},
)
Expand Down

0 comments on commit a15b681

Please sign in to comment.