Skip to content

Commit

Permalink
Improved the Storage handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihai-Cristian Condrea committed Aug 13, 2024
1 parent fb43302 commit 2e04620
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String , Long> = 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
)
}
Expand Down
26 changes: 0 additions & 26 deletions app/src/main/kotlin/com/d4rk/cleaner/utils/cleaning/FileUtils.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,18 +38,30 @@ 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)
}
}
}

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<String> = 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])
}
}
}
}

0 comments on commit 2e04620

Please sign in to comment.