Skip to content

Commit

Permalink
Better name "human readable size" related variables
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarX committed Oct 18, 2024
1 parent 5d02b15 commit ded2e33
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ private fun ImportFilesScreen(
) {
val context = LocalContext.current
var showUploadSourceChoiceBottomSheet by rememberSaveable { mutableStateOf(true) }
val formattedSizeWithUnits by remember {
val humanReadableSize by remember {
derivedStateOf {
val totalFileSize = files().sumOf { it.fileSizeInBytes }
getFormattedSizeWithUnits(totalFileSize, context)
getHumanReadableFormattedSize(totalFileSize, context)
}
}
val isSendButtonEnabled by remember { derivedStateOf { files().isNotEmpty() } }
Expand Down Expand Up @@ -92,7 +92,7 @@ private fun ImportFilesScreen(
SelectedFilesCard(
modifier = Modifier.padding(Margin.Medium),
files = files,
formattedSizeWithUnits = { formattedSizeWithUnits },
humanReadableSize = { humanReadableSize },
showUploadSourceChoiceBottomSheet = { showUploadSourceChoiceBottomSheet = true },
removeFileByUid = removeFileByUid,
)
Expand All @@ -106,7 +106,7 @@ private fun ImportFilesScreen(
)
}

private fun getFormattedSizeWithUnits(usedSpace: Long, context: Context): String {
private fun getHumanReadableFormattedSize(usedSpace: Long, context: Context): String {
val spaceLeft = (TOTAL_FILE_SIZE - usedSpace).coerceAtLeast(0)
return Formatter.formatShortFileSize(context, spaceLeft)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import kotlinx.parcelize.Parcelize
fun SelectedFilesCard(
modifier: Modifier = Modifier,
files: () -> List<FileUiItem>,
formattedSizeWithUnits: () -> String,
humanReadableSize: () -> String,
showUploadSourceChoiceBottomSheet: () -> Unit,
removeFileByUid: (uid: String) -> Unit,
) {
Expand All @@ -72,7 +72,7 @@ fun SelectedFilesCard(
style = SwissTransferTheme.typography.bodySmallRegular,
)
Text(
formatSpaceLeft(formattedSizeWithUnits),
formatSpaceLeft(humanReadableSize),
color = SwissTransferTheme.colors.secondaryTextColor,
style = SwissTransferTheme.typography.bodySmallRegular,
)
Expand Down Expand Up @@ -110,14 +110,15 @@ fun SelectedFilesCard(
}

@Composable
private fun formatSpaceLeft(formattedSizeWithUnits: () -> String): String {
val formattedSize = formattedSizeWithUnits()
val quantity = LocalContext.current.getQuantityFromFormattedSizeWithUnits(formattedSize)
private fun formatSpaceLeft(humanReadableSize: () -> String): String {
val formattedSize = humanReadableSize()
val quantity = LocalContext.current.getQuantityFromHumanReadableSize(formattedSize)
return pluralStringResource(R.plurals.transferSpaceLeft, quantity, formattedSize)
}

private fun Context.getQuantityFromFormattedSizeWithUnits(formattedSize: String): Int {
val sizeParts = formattedSize.split(' ', Typography.nbsp) // Space for languages such as EN and NBSP for languages such as FR
private fun Context.getQuantityFromHumanReadableSize(humanReadableSize: String): Int {
// Space character for languages such as EN and NBSP character for languages such as FR
val sizeParts = humanReadableSize.split(' ', Typography.nbsp)

return if (sizeParts.size == 2) {
val local = resources.configuration.getLocales().get(0)
Expand Down Expand Up @@ -176,7 +177,7 @@ private fun SelectedFilesCardPreview() {
}
)
},
formattedSizeWithUnits = { "20 GB" },
humanReadableSize = { "20 GB" },
showUploadSourceChoiceBottomSheet = {},
removeFileByUid = {}
)
Expand Down

0 comments on commit ded2e33

Please sign in to comment.