diff --git a/app/src/main/kotlin/com/d4rk/cleaner/ui/memory/MemoryManagerComposable.kt b/app/src/main/kotlin/com/d4rk/cleaner/ui/memory/MemoryManagerComposable.kt index c9c9f22..225b8bb 100644 --- a/app/src/main/kotlin/com/d4rk/cleaner/ui/memory/MemoryManagerComposable.kt +++ b/app/src/main/kotlin/com/d4rk/cleaner/ui/memory/MemoryManagerComposable.kt @@ -70,7 +70,7 @@ import com.d4rk.cleaner.R import com.d4rk.cleaner.data.model.ui.memorymanager.RamInfo import com.d4rk.cleaner.data.model.ui.memorymanager.StorageInfo import com.d4rk.cleaner.utils.PermissionsUtils -import com.d4rk.cleaner.utils.cleaning.FileUtils.formatSize +import com.d4rk.cleaner.utils.cleaning.StorageUtils.formatSize import com.d4rk.cleaner.utils.compose.bounceClick import com.d4rk.cleaner.utils.compose.components.StorageProgressBar import kotlinx.coroutines.delay @@ -165,8 +165,9 @@ fun MemoryManagerComposable() { ) Spacer(modifier = Modifier.width(8.dp)) - IconButton(modifier = Modifier.bounceClick() , - onClick = { viewModel.toggleListExpanded() }) { + IconButton( + modifier = Modifier.bounceClick() , + onClick = { viewModel.toggleListExpanded() }) { Icon( imageVector = if (listExpanded) Icons.Outlined.ArrowDropDown else Icons.AutoMirrored.Filled.ArrowLeft , contentDescription = if (listExpanded) "Collapse" else "Expand" @@ -375,14 +376,15 @@ fun DotsIndicator( updateTransition(targetState = selectedIndex , label = "Dot Transition") LazyRow( - modifier = modifier.wrapContentWidth().height(dotSize) , verticalAlignment = Alignment.CenterVertically + modifier = modifier + .wrapContentWidth() + .height(dotSize) , + verticalAlignment = Alignment.CenterVertically ) { items(totalDots) { index -> - val animatedDotSize : Dp by transition.animateDp( - transitionSpec = { - tween(durationMillis = animationDuration , easing = FastOutSlowInEasing) - } , label = "Dot Size Animation" - ) { + val animatedDotSize : Dp by transition.animateDp(transitionSpec = { + tween(durationMillis = animationDuration , easing = FastOutSlowInEasing) + } , label = "Dot Size Animation") { if (it == index) dotSize else dotSize / 1.4f } diff --git a/app/src/main/kotlin/com/d4rk/cleaner/ui/memory/MemoryManagerViewModel.kt b/app/src/main/kotlin/com/d4rk/cleaner/ui/memory/MemoryManagerViewModel.kt index 89b8efa..bb12f4a 100644 --- a/app/src/main/kotlin/com/d4rk/cleaner/ui/memory/MemoryManagerViewModel.kt +++ b/app/src/main/kotlin/com/d4rk/cleaner/ui/memory/MemoryManagerViewModel.kt @@ -40,15 +40,21 @@ class MemoryManagerViewModel : ViewModel() { * * @param context The application context. */ - fun updateStorageInfo(context: Context) { + fun updateStorageInfo(context : Context) { viewModelScope.launch { try { StorageUtils.getStorageInfo(context) { used, total, _ -> - val storageBreakdown = getStorageBreakdown(context) + + val usedStorageBytes : Double = + (used.toDoubleOrNull() ?: 0.0) * 1024 * 1024 * 1024 + val totalStorageBytes : Double = + (total.toDoubleOrNull() ?: 0.0) * 1024 * 1024 * 1024 + + val storageBreakdown : Map = getStorageBreakdown(context) _storageInfo.value = StorageInfo( - totalStorage = total.toLongOrNull() ?: 0, - freeStorage = (total.toLongOrNull() ?: 0) - (used.toLongOrNull() ?: 0), - usedStorage = used.toLongOrNull() ?: 0, + totalStorage = totalStorageBytes.toLong() , + freeStorage = (totalStorageBytes - usedStorageBytes).toLong() , + usedStorage = usedStorageBytes.toLong() , storageBreakdown = storageBreakdown ) } diff --git a/app/src/main/kotlin/com/d4rk/cleaner/utils/cleaning/FileUtils.kt b/app/src/main/kotlin/com/d4rk/cleaner/utils/cleaning/FileUtils.kt deleted file mode 100644 index 3f7d338..0000000 --- a/app/src/main/kotlin/com/d4rk/cleaner/utils/cleaning/FileUtils.kt +++ /dev/null @@ -1,26 +0,0 @@ -package com.d4rk.cleaner.utils.cleaning - -import java.util.Locale -import kotlin.math.log10 -import kotlin.math.pow - -object FileUtils { - - /** - * Formats a file size in bytes to a human-readable string (e.g., "128 MB"). - * - * @param size The file size in bytes. - * @return A formatted string representing the file size. - */ - fun formatSize(size: Long): String { - if (size <= 0) return "0 B" - val units = arrayOf("B", "KB", "MB", "GB", "TB") - val digitGroups = (log10(size.toDouble()) / log10(x= 1024.0)).toInt() - return String.format( - Locale.US, - format = "%.2f %s", - size / 1024.0.pow(digitGroups.toDouble()), - units[digitGroups] - ) - } -} \ No newline at end of file diff --git a/app/src/main/kotlin/com/d4rk/cleaner/utils/cleaning/StorageUtils.kt b/app/src/main/kotlin/com/d4rk/cleaner/utils/cleaning/StorageUtils.kt index 17eae3d..d2526f9 100644 --- a/app/src/main/kotlin/com/d4rk/cleaner/utils/cleaning/StorageUtils.kt +++ b/app/src/main/kotlin/com/d4rk/cleaner/utils/cleaning/StorageUtils.kt @@ -4,12 +4,14 @@ import android.app.usage.StorageStatsManager import android.content.Context import android.os.storage.StorageManager import android.os.storage.StorageVolume -import com.d4rk.cleaner.data.model.ui.memorymanager.StorageInfo import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import java.util.Locale import java.util.UUID +import kotlin.math.log10 +import kotlin.math.pow import kotlin.math.roundToInt object StorageUtils { @@ -36,10 +38,6 @@ object StorageUtils { val totalFormatted : String = (totalSize / (1024.0 * 1024.0 * 1024.0)).roundToInt().toString() val usageProgress : Float = usedSize.toFloat() / totalSize.toFloat() - StorageInfo( - totalStorage = totalSize , - usedStorage = usedSize , - ) withContext(Dispatchers.Main) { callback(usedFormatted , totalFormatted , usageProgress) @@ -47,7 +45,23 @@ object StorageUtils { } } - fun bytesToGB(bytes: Long): String { - return String.format("%.2f GB", bytes / (1024.0 * 1024.0 * 1024.0)) + fun formatSize(size : Long) : String { + if (size <= 0) return "0 B" + val units : Array = arrayOf("B" , "KB" , "MB" , "GB" , "TB") + val digitGroups : Int = (log10(size.toDouble()) / log10(x= 1024.0)).toInt() + val value : Double = size / 1024.0.pow(digitGroups.toDouble()) + + return if (value.compareTo(value.toLong()) == 0) { + String.format(Locale.US , format = "%d %s" , value.toLong() , units[digitGroups]) + } + else { + val decimalPart : Int = (value * 100).toInt() % 100 + if (decimalPart == 0) { + String.format(Locale.US , format = "%d %s" , value.toLong() , units[digitGroups]) + } + else { + String.format(Locale.US , format = "%.2f %s" , value , units[digitGroups]) + } + } } } \ No newline at end of file