Skip to content

Commit

Permalink
Android Studio suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
D4rK7355608 committed Oct 10, 2024
1 parent 31b6564 commit 044a67b
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 142 deletions.
3 changes: 3 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

155 changes: 155 additions & 0 deletions app/src/main/kotlin/com/d4rk/cleaner/ui/components/CarouselLayout.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package com.d4rk.cleaner.ui.components

import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.Transition
import androidx.compose.animation.core.animateDp
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.animation.core.updateTransition
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.lerp
import com.d4rk.cleaner.ui.components.animations.hapticPagerSwipe
import kotlin.math.absoluteValue

@Composable
fun <T> CarouselLayout(
items: List<T>,
sidePadding: Dp,
pagerState: PagerState,
itemContent: @Composable (item: T) -> Unit
) {
Column(
modifier = Modifier
.fillMaxWidth()
) {
HorizontalPager(
state = pagerState,
modifier = Modifier
.fillMaxWidth()
.hapticPagerSwipe(pagerState),
contentPadding = PaddingValues(horizontal = sidePadding),
) { page ->
val pageOffset = remember(pagerState.currentPage, page) {
(pagerState.currentPage - page).absoluteValue.toFloat()
}
CarouselItem(items[page], pageOffset, itemContent)
}

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

DotsIndicator(
modifier = Modifier
.align(Alignment.CenterHorizontally)
.padding(bottom = 8.dp),
totalDots = items.size,
selectedIndex = pagerState.currentPage,
dotSize = 6.dp,
)
}
}

@Composable
fun <T> CarouselItem(
item: T,
pageOffset: Float,
itemContent: @Composable (item: T) -> Unit
) {
val scale = animateFloatAsState(
targetValue = lerp(0.95f, 1f, 1f - pageOffset.coerceIn(0f, 1f)),
animationSpec = tween(250),
label = "Carousel Item Scale for Page $pageOffset"
).value

val alpha = lerp(0.5f, 1f, 1f - pageOffset.coerceIn(0f, 1f))

Card(
modifier = Modifier
.fillMaxWidth()
.graphicsLayer {
scaleX = scale
scaleY = scale
this.alpha = alpha
}
) {
itemContent(item)
}
}

@Composable
fun DotsIndicator(
modifier: Modifier = Modifier,
totalDots: Int,
selectedIndex: Int,
selectedColor: Color = MaterialTheme.colorScheme.primary,
unSelectedColor: Color = Color.Gray,
dotSize: Dp,
animationDuration: Int = 300
) {
val transition: Transition<Int> =
updateTransition(targetState = selectedIndex, label = "Dot Transition")

LazyRow(
modifier = modifier
.wrapContentWidth()
.height(dotSize),
verticalAlignment = Alignment.CenterVertically
) {
items(count = totalDots, key = { index -> index }) { index ->
val animatedDotSize: Dp by transition.animateDp(transitionSpec = {
tween(durationMillis = animationDuration, easing = FastOutSlowInEasing)
}, label = "Dot Size Animation") {
if (it == index) dotSize else dotSize / 1.4f
}

val isSelected: Boolean = index == selectedIndex
val size: Dp = if (isSelected) animatedDotSize else animatedDotSize

IndicatorDot(
color = if (isSelected) selectedColor else unSelectedColor, size = size
)

if (index != totalDots - 1) {
Spacer(modifier = Modifier.padding(horizontal = 2.dp))
}
}
}
}

@Composable
fun IndicatorDot(
modifier: Modifier = Modifier,
size: Dp,
color: Color,
) {
Box(
modifier = modifier
.size(size)
.clip(CircleShape)
.background(color)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ import com.d4rk.cleaner.R
import com.d4rk.cleaner.data.model.ui.appmanager.ui.ApkInfo
import com.d4rk.cleaner.data.model.ui.error.UiErrorModel
import com.d4rk.cleaner.data.model.ui.screens.UiAppManagerModel
import com.d4rk.cleaner.ui.components.dialogs.ErrorAlertDialog
import com.d4rk.cleaner.utils.PermissionsUtils
import com.d4rk.cleaner.ui.components.animations.bounceClick
import com.d4rk.cleaner.ui.components.animations.hapticPagerSwipe
import com.d4rk.cleaner.ui.components.dialogs.ErrorAlertDialog
import com.d4rk.cleaner.ui.screens.loading.LoadingScreen
import com.d4rk.cleaner.utils.PermissionsUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.io.File
Expand All @@ -86,12 +87,16 @@ fun AppManagerScreen() {
val pagerState: PagerState = rememberPagerState(pageCount = { tabs.size })
val coroutineScope: CoroutineScope = rememberCoroutineScope()
val isLoading: Boolean by viewModel.isLoading.collectAsState()

val transition: Transition<Boolean> =
updateTransition(targetState = !isLoading, label = "LoadingTransition")

val progressAlpha: Float by transition.animateFloat(label = "Progress Alpha") {
if (it) 0f else 1f
}
val contentAlpha: Float by transition.animateFloat(label = "Content Alpha") {
if (it) 1f else 0f
}

val uiState: UiAppManagerModel by viewModel.uiState.collectAsState()
val uiErrorModel: UiErrorModel by viewModel.uiErrorModel.collectAsState()

Expand All @@ -107,14 +112,12 @@ fun AppManagerScreen() {
}

if (isLoading) {
Box(
modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center
) {
CircularProgressIndicator()
}
LoadingScreen(progressAlpha)
} else {
Column(
modifier = Modifier.alpha(contentAlpha),
modifier = Modifier
.fillMaxSize()
.alpha(contentAlpha)
) {
TabRow(
selectedTabIndex = pagerState.currentPage,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.d4rk.cleaner.ui.screens.loading

import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha

@Composable
fun LoadingScreen(progressAlpha: Float) {
Box(
modifier = Modifier
.fillMaxSize()
.animateContentSize()
.alpha(progressAlpha),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator()
}
}
Loading

0 comments on commit 044a67b

Please sign in to comment.