Skip to content

Commit

Permalink
add speed unit (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
amir1376 authored Jan 1, 2025
1 parent a2077c7 commit f106c9b
Show file tree
Hide file tree
Showing 29 changed files with 538 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class AppComponent(
DownloadItemOpener,
ContainsEffects<AppEffects> by supportEffects(),
KoinComponent {
private val appRepository: AppRepository by inject()
val appRepository: AppRepository by inject()
private val appSettings: AppSettingsStorage by inject()
private val integration: Integration by inject()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private fun SizeCell(
val length by downloadChecker.length.collectAsState()
CellText(
length?.let {
convertSizeToHumanReadable(it).rememberString()
convertPositiveSizeToHumanReadable(it, LocalSizeUnit.current).rememberString()
} ?: ""
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ fun RenderFileTypeAndSize(
iconModifier
)
val size = fileInfo.totalLength?.let {
convertSizeToHumanReadable(it)
convertPositiveSizeToHumanReadable(it, LocalSizeUnit.current)
}.takeIf {
// this is a length of a html page (error)
fileInfo.isSuccessFul
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ class AddSingleDownloadComponent(
backedBy = speedLimit,
describe = {
if (it == 0L) Res.string.unlimited.asStringSource()
else convertSpeedToHumanReadable(it).asStringSource()
else convertPositiveSpeedToHumanReadable(
it, appSettings.speedUnit.value
).asStringSource()
}
),
IntConfigurable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ private fun RenderFileTypeAndSize(
iconModifier
)
val size = fileInfo.totalLength?.let {
convertSizeToHumanReadable(it)
convertPositiveSizeToHumanReadable(it, LocalSizeUnit.current)
}.takeIf {
// this is a length of a html page (error)
fileInfo.isSuccessFul
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.abdownloadmanager.desktop.pages.editdownload

import com.abdownloadmanager.desktop.repository.AppRepository
import com.abdownloadmanager.desktop.utils.*
import com.abdownloadmanager.desktop.utils.mvi.ContainsEffects
import com.abdownloadmanager.desktop.utils.mvi.supportEffects
Expand Down Expand Up @@ -30,6 +31,7 @@ class EditDownloadComponent(
private val downloaderClient: DownloaderClient by inject()
val iconProvider: FileIconProvider by inject()
val downloadSystem: DownloadSystem by inject()
private val appRepository: AppRepository by inject()
val editDownloadUiChecker = MutableStateFlow(null as EditDownloadState?)

init {
Expand Down Expand Up @@ -73,7 +75,8 @@ class EditDownloadComponent(
.contains(editedDownloadFile)
}
},
scope,
scope = scope,
appRepository = appRepository,
)
editDownloadUiChecker.value = editDownloadState
pendingCredential?.let { credentials ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package com.abdownloadmanager.desktop.pages.editdownload
import com.abdownloadmanager.desktop.pages.settings.configurable.IntConfigurable
import com.abdownloadmanager.desktop.pages.settings.configurable.SpeedLimitConfigurable
import com.abdownloadmanager.desktop.pages.settings.configurable.StringConfigurable
import com.abdownloadmanager.desktop.repository.AppRepository
import com.abdownloadmanager.desktop.utils.FileNameValidator
import com.abdownloadmanager.desktop.utils.LinkChecker
import com.abdownloadmanager.desktop.utils.convertSpeedToHumanReadable
import com.abdownloadmanager.desktop.utils.convertPositiveSpeedToHumanReadable
import com.abdownloadmanager.resources.Res
import com.abdownloadmanager.utils.isValidUrl
import ir.amirab.downloader.connection.DownloaderClient
Expand All @@ -16,7 +17,6 @@ import ir.amirab.downloader.downloaditem.withCredentials
import ir.amirab.util.compose.StringSource
import ir.amirab.util.compose.asStringSource
import ir.amirab.util.compose.asStringSourceWithARgs
import ir.amirab.util.flow.createMutableStateFlowFromStateFlow
import ir.amirab.util.flow.mapStateFlow
import ir.amirab.util.flow.mapTwoWayStateFlow
import ir.amirab.util.flow.onEachLatest
Expand Down Expand Up @@ -126,6 +126,7 @@ class EditDownloadState(
val currentDownloadItem: MutableStateFlow<DownloadItem>,
val editedDownloadItem: MutableStateFlow<DownloadItem>,
val downloaderClient: DownloaderClient,
val appRepository: AppRepository,
conflictDetector: DownloadConflictDetector,
scope: CoroutineScope,
) {
Expand Down Expand Up @@ -165,7 +166,7 @@ class EditDownloadState(
),
describe = {
if (it == 0L) Res.string.unlimited.asStringSource()
else convertSpeedToHumanReadable(it).asStringSource()
else convertPositiveSpeedToHumanReadable(it, appRepository.speedUnit.value).asStringSource()
}
),
IntConfigurable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import ir.amirab.downloader.utils.ByteConverter
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import com.abdownloadmanager.desktop.ui.widget.ActionButton
Expand Down Expand Up @@ -735,14 +734,10 @@ private fun Footer(component: HomeComponent) {
val activeCount by component.activeDownloadCountFlow.collectAsState()
FooterItem(MyIcons.activeCount, activeCount.toString(), "")
val size by component.globalSpeedFlow.collectAsState(0)
val speed = baseConvertBytesToHumanReadable(size)
val speed = convertPositiveBytesToSizeUnit(size, LocalSpeedUnit.current)
if (speed != null) {
val speedText = ByteConverter.prettify(speed.value)
val unitText = ByteConverter.unitPrettify(speed.unit)
?.let {
"$it/s"
}
.orEmpty()
val speedText = speed.formatedValue()
val unitText = speed.unit.toString() + "/s"
FooterItem(MyIcons.speed, speedText, unitText)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.abdownloadmanager.resources.Res
import com.abdownloadmanager.resources.*
import com.abdownloadmanager.utils.FileIconProvider
import com.abdownloadmanager.utils.category.Category
import ir.amirab.util.compose.resources.myStringResource
Expand Down Expand Up @@ -175,7 +174,10 @@ fun SpeedCell(
(itemState as? ProcessingDownloadItemState)?.speed?.let { remaining ->
if (itemState.status == DownloadJobStatus.Downloading) {
Text(
text = convertSpeedToHumanReadable(remaining),
text = convertPositiveSpeedToHumanReadable(
remaining,
LocalSpeedUnit.current,
),
maxLines = 1,
fontSize = myTextSizes.base,
overflow = TextOverflow.Ellipsis,
Expand All @@ -190,7 +192,10 @@ fun SizeCell(
) {
item.contentLength.let {
Text(
convertSizeToHumanReadable(it).rememberString(),
convertPositiveSizeToHumanReadable(
it,
LocalSizeUnit.current
).rememberString(),
maxLines = 1,
fontSize = myTextSizes.base,
overflow = TextOverflow.Ellipsis,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.abdownloadmanager.desktop.storage.AppSettingsStorage
import ir.amirab.util.compose.IconSource
import com.abdownloadmanager.desktop.ui.icon.MyIcons
import com.abdownloadmanager.desktop.utils.BaseComponent
import com.abdownloadmanager.desktop.utils.convertSpeedToHumanReadable
import com.abdownloadmanager.desktop.utils.convertPositiveSpeedToHumanReadable
import com.abdownloadmanager.desktop.utils.mvi.ContainsEffects
import com.abdownloadmanager.desktop.utils.mvi.supportEffects
import androidx.compose.runtime.*
Expand All @@ -20,6 +20,8 @@ import ir.amirab.util.compose.asStringSource
import ir.amirab.util.compose.asStringSourceWithARgs
import ir.amirab.util.compose.localizationmanager.LanguageInfo
import ir.amirab.util.compose.localizationmanager.LanguageManager
import ir.amirab.util.datasize.CommonSizeConvertConfigs
import ir.amirab.util.datasize.ConvertSizeConfig
import ir.amirab.util.osfileutil.FileUtils
import ir.amirab.util.flow.createMutableStateFlowFromStateFlow
import ir.amirab.util.flow.mapStateFlow
Expand Down Expand Up @@ -121,6 +123,26 @@ fun trackDeletedFilesOnDisk(appRepository: AppRepository): BooleanConfigurable {
)
}

fun speedUnit(appRepository: AppRepository, scope: CoroutineScope): EnumConfigurable<ConvertSizeConfig> {
return EnumConfigurable(
title = Res.string.settings_download_speed_unit.asStringSource(),
description = Res.string.settings_download_speed_unit_description.asStringSource(),
backedBy = createMutableStateFlowFromStateFlow(
appRepository.speedUnit,
updater = { appRepository.setSpeedUnit(it) },
scope = scope
),
possibleValues = listOf(
CommonSizeConvertConfigs.BinaryBytes,
CommonSizeConvertConfigs.BinaryBits,
),
describe = {
val u = it.baseSize.longString()
"$u/s".asStringSource()
},
)
}

fun showDownloadFinishWindow(settingsStorage: AppSettingsStorage): BooleanConfigurable {
return BooleanConfigurable(
title = Res.string.settings_show_completion_dialog.asStringSource(),
Expand Down Expand Up @@ -152,7 +174,7 @@ fun speedLimitConfig(appRepository: AppRepository): SpeedLimitConfigurable {
if (it == 0L) {
Res.string.unlimited.asStringSource()
} else {
convertSpeedToHumanReadable(it).asStringSource()
convertPositiveSpeedToHumanReadable(it, appRepository.speedUnit.value).asStringSource()
}
}
)
Expand Down Expand Up @@ -398,6 +420,7 @@ class SettingsComponent(
uiScaleConfig(appSettings),
autoStartConfig(appSettings),
mergeTopBarWithTitleBarConfig(appSettings),
speedUnit(appRepository, scope),
playSoundNotification(appSettings),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,55 @@ package com.abdownloadmanager.desktop.pages.settings.configurable.widgets
import com.abdownloadmanager.desktop.pages.settings.configurable.SpeedLimitConfigurable
import com.abdownloadmanager.desktop.ui.widget.CheckBox
import com.abdownloadmanager.desktop.ui.widget.DoubleTextField
import com.abdownloadmanager.desktop.utils.baseConvertBytesToHumanReadable
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.*
import com.abdownloadmanager.desktop.ui.widget.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import ir.amirab.downloader.utils.ByteConverter
import ir.amirab.downloader.utils.ByteConverter.BYTES
import ir.amirab.downloader.utils.ByteConverter.K_BYTES
import ir.amirab.downloader.utils.ByteConverter.M_BYTES
import com.abdownloadmanager.desktop.utils.LocalSpeedUnit
import ir.amirab.util.datasize.*

@Composable
fun RenderSpeedConfig(cfg: SpeedLimitConfigurable, modifier: Modifier) {
val value by cfg.stateFlow.collectAsState()
val setValue = cfg::set
val units = listOf(
BYTES,
K_BYTES,
M_BYTES,

val speedUnit = LocalSpeedUnit.current
val allowedFactors = listOf(
SizeFactors.FactorValue.Kilo,
SizeFactors.FactorValue.Mega,
)
val enabled= isConfigEnabled()
val hasLimitSpeed = value != 0L

var currentUnit by remember(hasLimitSpeed) { mutableStateOf(baseConvertBytesToHumanReadable(value)?.unit ?: BYTES) }
val units = allowedFactors.map {
SizeUnit(
factorValue = it,
baseSize = speedUnit.baseSize,
factors = speedUnit.factors
)
}
val enabled = isConfigEnabled()
val hasLimitSpeed = value > 0L

var currentUnit by remember(hasLimitSpeed) {
mutableStateOf(
SizeConverter.bytesToSize(
value,
speedUnit.copy(acceptedFactors = allowedFactors)
).unit
)
}
var currentValue by remember(value) {
val v = ByteConverter.run {
prettify(
byteTo(value, currentUnit)
).toDouble()
}
val v = SizeConverter.bytesToSize(
value, currentUnit.asConverterConfig()
).formatedValue().toDouble()
mutableStateOf(v)
}
LaunchedEffect(currentValue, currentUnit) {
setValue(
ByteConverter.unitToByte(currentValue, currentUnit)
SizeConverter.sizeToBytes(
SizeWithUnit(currentValue, currentUnit),
)
)
}
ConfigTemplate(
Expand Down Expand Up @@ -77,7 +89,7 @@ fun RenderSpeedConfig(cfg: SpeedLimitConfigurable, modifier: Modifier) {
}
) {
val prettified = remember(it) {
ByteConverter.unitPrettify(it) + "/s"
"$it/s"
}
Text(prettified)
}
Expand All @@ -90,12 +102,22 @@ fun RenderSpeedConfig(cfg: SpeedLimitConfigurable, modifier: Modifier) {
value = hasLimitSpeed,
enabled = enabled,
onValueChange = {
if (it) {
setValue(ByteConverter.unitToByte(10.0, K_BYTES))
} else {
setValue(0)
}
})
if (it) {
setValue(
SizeConverter.sizeToBytes(
SizeWithUnit(
256.0, SizeUnit(
SizeFactors.FactorValue.Kilo,
BaseSize.Bytes,
SizeFactors.BinarySizeFactors,
)
)
)
)
} else {
setValue(0)
}
})
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@ package com.abdownloadmanager.desktop.pages.singleDownloadPage

import androidx.compose.foundation.background
import androidx.compose.foundation.basicMarquee
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.onClick
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.abdownloadmanager.desktop.ui.icon.MyIcons
import com.abdownloadmanager.desktop.ui.theme.myColors
import com.abdownloadmanager.desktop.ui.theme.myTextSizes
import com.abdownloadmanager.desktop.ui.widget.ActionButton
import com.abdownloadmanager.desktop.ui.widget.Text
import com.abdownloadmanager.desktop.utils.convertSizeToHumanReadable
import com.abdownloadmanager.desktop.utils.LocalSizeUnit
import com.abdownloadmanager.desktop.utils.convertPositiveSizeToHumanReadable
import com.abdownloadmanager.desktop.utils.div
import com.abdownloadmanager.resources.Res
import com.abdownloadmanager.utils.compose.WithContentColor
Expand Down Expand Up @@ -150,8 +147,10 @@ private fun RenderFileIconAndSize(
)
Spacer(Modifier.height(4.dp))
Text(
text = convertSizeToHumanReadable(itemState.contentLength)
.rememberString(),
text = convertPositiveSizeToHumanReadable(
itemState.contentLength,
LocalSizeUnit.current,
).rememberString(),
)
}
}
Loading

0 comments on commit f106c9b

Please sign in to comment.