Skip to content

Instantly share code, notes, and snippets.

View muhammedesadcomert's full-sized avatar
🎯
Focusing

Muhammed Esad Cömert muhammedesadcomert

🎯
Focusing
View GitHub Profile
@muhammedesadcomert
muhammedesadcomert / ShimmerButton.kt
Created January 10, 2024 12:30
Creating Shimmer Effect in Jetpack Compose
val shimmer = rememberShimmer(
shimmerBounds = ShimmerBounds.View, theme = defaultShimmerTheme.copy(
blendMode = BlendMode.DstOut,
shaderColors = listOf(
MaterialTheme.colorScheme.surface.copy(alpha = 0f),
MaterialTheme.colorScheme.surface.copy(alpha = 0.5f),
MaterialTheme.colorScheme.surface.copy(alpha = 0f),
),
shaderColorStops = listOf(0.0f, 0.5f, 1.0f)
)
@muhammedesadcomert
muhammedesadcomert / Pager.kt
Last active January 5, 2024 12:24
Infinite Transition with Pager
val pagerState = rememberPagerState { Int.MAX_VALUE }
LaunchedEffect(Unit) {
while (true) {
delay(1500) // The duration of the image on the screen
if (pagerState.currentPage >= YourEnumClass.entries.lastIndex - 1) {
pagerState.animateScrollToPage(0) // If it reaches the end of the list, it returns to the beginning
} else {
pagerState.animateScrollToPage(pagerState.currentPage + 1)
}
@muhammedesadcomert
muhammedesadcomert / AppNavHost.kt
Last active January 4, 2024 09:28
Changing navigation bar color according to screen
@Composable
fun AppNavHost(navController: NavHostController) {
val context = LocalContext.current
val navigationBarColor = animateColorAsState(
targetValue = when (navController.currentDestination()?.route) {
Screen.ScreenOne.route -> color_one
Screen.ScreenTwo.route -> color_two
else -> default_color
},
label = "navigationBarColor",
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
@Composable
fun NavHostController.currentDestination(): NavDestination? {
val navBackStackEntry by currentBackStackEntryAsState()
@muhammedesadcomert
muhammedesadcomert / FirstPlacement.kt
Created January 2, 2024 07:33
Get the first position of a Composable.
@Composable
fun Modifier.onFirstPlacement(onFirstPlacement: (LayoutCoordinates) -> Unit): Modifier {
var isFirstPlacement by remember { mutableStateOf(true) }
var currentPosition by remember { mutableStateOf(Offset.Zero) }
return onPlaced {
if (isFirstPlacement) {
onFirstPlacement(it)
if (currentPosition == it.positionInWindow()) {
isFirstPlacement = false
@SuppressLint("ComposableModifierFactory")
@Composable
fun Modifier.bouncyClick(enabled: Boolean = true): Modifier {
return if (enabled) {
var clickState by remember { mutableStateOf(ClickState.Idle) }
val scale by animateFloatAsState(
targetValue = if (clickState == ClickState.Pressed) 0.925f else 1f,
animationSpec = spring(
dampingRatio = Spring.DampingRatioNoBouncy,
stiffness = Spring.StiffnessMedium
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@muhammedesadcomert
muhammedesadcomert / DocumentConversion.kt
Last active December 5, 2023 19:46
Firebase DocumentSnapshot conversion for Enum and Long fields
inline fun <reified T : Any> DocumentSnapshot.toObjectWithConversion(): T? {
val instance = T::class.primaryConstructor?.callBy(
T::class.primaryConstructor!!.parameters.associateWith { parameter ->
val value = this.get(parameter.name.orEmpty())
when {
parameter.type.classifier is KClass<*> && (parameter.type.classifier as KClass<*>).isSubclassOf(Enum::class) -> {
val enumClass = parameter.type.classifier as KClass<Enum<*>>
(value as? String)?.toEnumSafe(enumClass)
}
@muhammedesadcomert
muhammedesadcomert / infix_fun_kotlin.kt
Last active November 3, 2022 09:00
Infix functions in Kotlin
/*
* In Kotlin, a functions marked with infix keyword can also be called using
* infix notation means calling without using parenthesis and dot.
*/
// Call using dot and parenthesis
var result1 = a.shr(1)
// Call using infix notation
var result2 = a shr 2