Skip to content

Commit

Permalink
Improved the code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
D4rK7355608 committed Jun 26, 2024
1 parent 6044c7e commit aaa6e78
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 294 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.d4rk.cleaner.data.model

enum class ButtonState { Pressed , Idle }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.d4rk.cleaner.data.model

data class InternalStorageInfo(
val totalStorage: Long,
val freeStorage: Long,
val usedStorage: Long
)
7 changes: 7 additions & 0 deletions app/src/main/kotlin/com/d4rk/cleaner/data/model/RamInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.d4rk.cleaner.data.model

data class RamInfo(
val totalRam: Long = 0,
val availableRam: Long = 0,
val usedRam: Long = 0
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.d4rk.cleaner.data.model

data class StorageInfo(
val totalStorage: Long = 0,
val freeStorage: Long = 0,
val usedStorage: Long = 0,
val storageBreakdown: Map<String, Long> = emptyMap()
)
228 changes: 0 additions & 228 deletions app/src/main/kotlin/com/d4rk/cleaner/ui/memory/MemoryFragment.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.lerp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.d4rk.cleaner.data.model.RamInfo
import com.d4rk.cleaner.data.model.StorageInfo
import kotlin.math.absoluteValue

val StorageIcons = mapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import android.os.StatFs
import android.os.storage.StorageManager
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.d4rk.cleaner.data.model.InternalStorageInfo
import com.d4rk.cleaner.data.model.RamInfo
import com.d4rk.cleaner.data.model.StorageInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand Down Expand Up @@ -87,7 +90,7 @@ class MemoryManagerViewModel : ViewModel() {
breakdown["Images"] = getDirectorySize(File(externalStoragePath, "DCIM")) +
getDirectorySize(File(externalStoragePath, "Pictures"))
breakdown["Documents"] = getDirectorySize(File(externalStoragePath, "Documents"))
breakdown["Downloads"] = getDirectorySize(File(externalStoragePath, "Download")) // Use "Download"
breakdown["Downloads"] = getDirectorySize(File(externalStoragePath, "Download"))
breakdown["Other Files"] = getOtherFilesSize(breakdown)

return breakdown
Expand Down Expand Up @@ -148,28 +151,9 @@ class MemoryManagerViewModel : ViewModel() {
}
}

data class InternalStorageInfo(
val totalStorage: Long,
val freeStorage: Long,
val usedStorage: Long
)

data class StorageInfo(
val totalStorage: Long = 0,
val freeStorage: Long = 0,
val usedStorage: Long = 0,
val storageBreakdown: Map<String, Long> = emptyMap()
)

data class RamInfo(
val totalRam: Long = 0,
val availableRam: Long = 0,
val usedRam: Long = 0
)

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(1024.0)).toInt()
return String.format("%.2f %s", size / 1024.0.pow(digitGroups.toDouble()), units[digitGroups])
return String.format("%.2f %s", size / 1024.0.pow(digitGroups.toDouble()), units[digitGroups]) // FIXME: Implicitly using the default locale is a common source of bugs: Use `String.format(Locale, ...)` instead
}
47 changes: 47 additions & 0 deletions app/src/main/kotlin/com/d4rk/cleaner/utils/AnimationUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.d4rk.cleaner.utils

import android.annotation.SuppressLint
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.gestures.waitForUpOrCancellation
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.pointerInput
import com.d4rk.cleaner.data.model.ButtonState

@SuppressLint("ReturnFromAwaitPointerEventScope")
@Composable
fun Modifier.bounceClick() = composed {
var buttonState by remember { mutableStateOf(ButtonState.Idle) }
val scale by animateFloatAsState(
if (buttonState == ButtonState.Pressed) 0.95f else 1f , label = ""
)
this
.graphicsLayer {
scaleX = scale
scaleY = scale
}
.clickable(interactionSource = remember { MutableInteractionSource() } ,
indication = null ,
onClick = { })
.pointerInput(buttonState) {
awaitPointerEventScope {
buttonState = if (buttonState == ButtonState.Pressed) {
waitForUpOrCancellation()
ButtonState.Idle
}
else {
awaitFirstDown(false)
ButtonState.Pressed
}
}
}
}
Loading

0 comments on commit aaa6e78

Please sign in to comment.