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 28, 2024
1 parent aaa6e78 commit 7a1f7bc
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 122 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ dependencies {
implementation(libs.androidx.lifecycle.process)
implementation(libs.androidx.lifecycle.viewmodel.ktx)
implementation(libs.androidx.lifecycle.viewmodel.compose)
implementation(libs.androidx.lifecycle.runtime.compose)

// Google
implementation(libs.play.services.ads)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ import com.d4rk.cleaner.data.model.StorageInfo
import kotlin.math.absoluteValue

val StorageIcons = mapOf(
"Installed Apps" to Icons.Outlined.Apps ,
"System" to Icons.Outlined.Android ,
"Music" to Icons.Outlined.MusicNote ,
"Images" to Icons.Outlined.Image ,
"Documents" to Icons.Outlined.FolderOpen ,
"Downloads" to Icons.Outlined.Download ,
"Installed Apps" to Icons.Outlined.Apps,
"System" to Icons.Outlined.Android,
"Music" to Icons.Outlined.MusicNote,
"Images" to Icons.Outlined.Image,
"Documents" to Icons.Outlined.FolderOpen,
"Downloads" to Icons.Outlined.Download,
"Other Files" to Icons.Outlined.FolderOpen
)

Expand All @@ -96,67 +96,68 @@ fun MemoryManagerComposable() {
modifier = Modifier.fillMaxSize()
) {
HorizontalPager(
state = pagerState , modifier = Modifier
.fillMaxWidth()
state = pagerState, modifier = Modifier
.fillMaxWidth()
) { page ->
val pageOffset = (page - pagerState.currentPage).absoluteValue
val scale by animateFloatAsState(
targetValue = lerp(
start = 0.9f , stop = 1f , fraction = 1f - pageOffset.coerceIn(0 , 1)
) , label = ""
start = 0.9f, stop = 1f, fraction = 1f - pageOffset.coerceIn(0, 1)
), label = ""
)

Card(modifier = Modifier.padding(16.dp)
.graphicsLayer {
scaleX = scale
scaleY = scale
alpha = lerp(
start = 0.5f , stop = 1f , fraction = 1f - pageOffset.coerceIn(0 , 1)
)
}
.offset {
val pageOffsetState = (page - pagerState.currentPage)
IntOffset(
x = (10.dp * pageOffsetState.toFloat()).roundToPx() , y = 0
)
}
.fillMaxWidth()) {
Card(modifier = Modifier
.padding(16.dp)
.graphicsLayer {
scaleX = scale
scaleY = scale
alpha = lerp(
start = 0.5f, stop = 1f, fraction = 1f - pageOffset.coerceIn(0, 1)
)
}
.offset {
val pageOffsetState = (page - pagerState.currentPage)
IntOffset(
x = (10.dp * pageOffsetState.toFloat()).roundToPx(), y = 0
)
}
.fillMaxWidth()) {
when (page) {
0 -> StorageInfoCard(storageInfo)
1 -> RamInfoCard(ramInfo)
}
}
}
DotsIndicator(
modifier = Modifier.align(Alignment.CenterHorizontally) ,
totalDots = 2 ,
selectedIndex = pagerState.currentPage ,
modifier = Modifier.align(Alignment.CenterHorizontally),
totalDots = 2,
selectedIndex = pagerState.currentPage,
dotSize = 6.dp
)

Spacer(modifier = Modifier.height(16.dp))

Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp)
.fillMaxWidth()
.padding(horizontal = 16.dp)
) {
Box(modifier = Modifier.weight(1f)) {
Column(modifier = Modifier.animateContentSize()) {
if (listExpanded) {
LazyColumn {
items(storageInfo.storageBreakdown.entries.toList()) { entry ->
StorageBreakdownItem(icon = entry.key , size = entry.value)
StorageBreakdownItem(icon = entry.key, size = entry.value)
}
}
}
}
}

Spacer(modifier = Modifier.width(8.dp))
IconButton(onClick = { listExpanded = ! listExpanded }) {
IconButton(onClick = { listExpanded = !listExpanded }) {
Icon(
imageVector = if (listExpanded) Icons.Outlined.ArrowDropDown else Icons.AutoMirrored.Filled.ArrowLeft ,
imageVector = if (listExpanded) Icons.Outlined.ArrowDropDown else Icons.AutoMirrored.Filled.ArrowLeft,
contentDescription = if (listExpanded) "Collapse" else "Expand"
)
}
Expand All @@ -165,49 +166,49 @@ fun MemoryManagerComposable() {
}

@Composable
fun StorageInfoCard(storageInfo : StorageInfo) {
fun StorageInfoCard(storageInfo: StorageInfo) {
Column(modifier = Modifier.padding(16.dp)) {
Text(
text = "Storage Information" ,
style = MaterialTheme.typography.headlineSmall ,
text = "Storage Information",
style = MaterialTheme.typography.headlineSmall,
fontWeight = FontWeight.Bold
)
Spacer(modifier = Modifier.height(8.dp))
LinearProgressIndicator(
progress = { storageInfo.usedStorage.toFloat() / storageInfo.totalStorage.toFloat() } ,
progress = { storageInfo.usedStorage.toFloat() / storageInfo.totalStorage.toFloat() },
modifier = Modifier
.fillMaxWidth()
.height(8.dp) ,
color = MaterialTheme.colorScheme.primary ,
.fillMaxWidth()
.height(8.dp),
color = MaterialTheme.colorScheme.primary,
)
Spacer(modifier = Modifier.height(8.dp))
StorageInfoText(label = "Used:" , size = storageInfo.usedStorage)
StorageInfoText(label = "Free:" , size = storageInfo.freeStorage)
StorageInfoText(label = "Total:" , size = storageInfo.totalStorage)
StorageInfoText(label = "Used:", size = storageInfo.usedStorage)
StorageInfoText(label = "Free:", size = storageInfo.freeStorage)
StorageInfoText(label = "Total:", size = storageInfo.totalStorage)
}
}

@Composable
fun StorageBreakdownItem(icon : String , size : Long) {
fun StorageBreakdownItem(icon: String, size: Long) {
Card(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 4.dp) ,
.fillMaxWidth()
.padding(vertical = 4.dp),
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp) ,
.fillMaxWidth()
.padding(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Card(
modifier = Modifier.size(48.dp) ,
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.primaryContainer) ,
modifier = Modifier.size(48.dp),
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.primaryContainer),
) {
Box(modifier = Modifier.fillMaxSize() , contentAlignment = Alignment.Center) {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Icon(
imageVector = StorageIcons[icon] ?: Icons.Filled.Info ,
contentDescription = icon ,
imageVector = StorageIcons[icon] ?: Icons.Filled.Info,
contentDescription = icon,
tint = MaterialTheme.colorScheme.onPrimaryContainer
)
}
Expand All @@ -217,78 +218,78 @@ fun StorageBreakdownItem(icon : String , size : Long) {

Column {
Text(
text = icon ,
style = MaterialTheme.typography.bodyMedium ,
text = icon,
style = MaterialTheme.typography.bodyMedium,
fontWeight = FontWeight.Bold
)
Text(text = formatSize(size) , style = MaterialTheme.typography.bodySmall)
Text(text = formatSize(size), style = MaterialTheme.typography.bodySmall)
}
}
}
}

@Composable
fun StorageInfoText(label : String , size : Long) {
Text(text = "$label ${formatSize(size)}" , style = MaterialTheme.typography.bodyMedium)
fun StorageInfoText(label: String, size: Long) {
Text(text = "$label ${formatSize(size)}", style = MaterialTheme.typography.bodyMedium)
}


@Composable
fun RamInfoCard(ramInfo : RamInfo) {
fun RamInfoCard(ramInfo: RamInfo) {
Column(modifier = Modifier.padding(16.dp)) {
Text(
"RAM Information" ,
style = MaterialTheme.typography.headlineSmall ,
"RAM Information",
style = MaterialTheme.typography.headlineSmall,
fontWeight = FontWeight.Bold
)
Spacer(modifier = Modifier.height(8.dp))
StorageProgressBar(
StorageInfo(
totalStorage = ramInfo.totalRam ,
usedStorage = ramInfo.usedRam ,
totalStorage = ramInfo.totalRam,
usedStorage = ramInfo.usedRam,
freeStorage = ramInfo.availableRam
)
)
Spacer(modifier = Modifier.height(8.dp))
StorageInfoText(label = "Used RAM:" , size = ramInfo.usedRam)
StorageInfoText(label = "Free RAM:" , size = ramInfo.availableRam)
StorageInfoText(label = "Total RAM:" , size = ramInfo.totalRam)
StorageInfoText(label = "Used RAM:", size = ramInfo.usedRam)
StorageInfoText(label = "Free RAM:", size = ramInfo.availableRam)
StorageInfoText(label = "Total RAM:", size = ramInfo.totalRam)
}
}

@Composable
fun StorageProgressBar(storageInfo : StorageInfo) {
fun StorageProgressBar(storageInfo: StorageInfo) {
val progress =
(storageInfo.usedStorage.toFloat() / storageInfo.totalStorage.toFloat()).coerceIn(
0f ,
1f
)
(storageInfo.usedStorage.toFloat() / storageInfo.totalStorage.toFloat()).coerceIn(
0f,
1f
)
LinearProgressIndicator(
progress = { progress } ,
progress = { progress },
modifier = Modifier
.fillMaxWidth()
.height(8.dp) ,
color = MaterialTheme.colorScheme.primary ,
.fillMaxWidth()
.height(8.dp),
color = MaterialTheme.colorScheme.primary,
)
}

@Composable
fun DotsIndicator(
modifier : Modifier = Modifier ,
totalDots : Int ,
selectedIndex : Int ,
selectedColor : Color = MaterialTheme.colorScheme.primary ,
unSelectedColor : Color = Color.Gray ,
dotSize : Dp ,
modifier: Modifier = Modifier,
totalDots: Int,
selectedIndex: Int,
selectedColor: Color = MaterialTheme.colorScheme.primary,
unSelectedColor: Color = Color.Gray,
dotSize: Dp,
) {
LazyRow(
modifier = modifier
.wrapContentWidth()
.wrapContentHeight()
.wrapContentWidth()
.wrapContentHeight()
) {
items(totalDots) { index ->
IndicatorDot(
color = if (index == selectedIndex) selectedColor else unSelectedColor ,
color = if (index == selectedIndex) selectedColor else unSelectedColor,
size = dotSize
)

Expand All @@ -301,14 +302,14 @@ fun DotsIndicator(

@Composable
fun IndicatorDot(
modifier : Modifier = Modifier ,
size : Dp ,
color : Color ,
modifier: Modifier = Modifier,
size: Dp,
color: Color,
) {
Box(
modifier = modifier
.size(size)
.clip(CircleShape)
.background(color)
.size(size)
.clip(CircleShape)
.background(color)
)
}
Loading

0 comments on commit 7a1f7bc

Please sign in to comment.