Skip to content

Instantly share code, notes, and snippets.

View cavin-macwan's full-sized avatar
💭
I've not failed, I've just found 10,000 ways that won't work

Cavin cavin-macwan

💭
I've not failed, I've just found 10,000 ways that won't work
View GitHub Profile
@cavin-macwan
cavin-macwan / ObserveAsEvents.kt
Created April 9, 2025 06:28
This snippet allows you to collect one time events like showing the snackbar or navigation without being lost
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext
@cavin-macwan
cavin-macwan / AppPackageManager.kt
Created March 24, 2025 06:37
Things that you can do with Package Manager API in Android
// 1. Retrieve installed apps (system & user-installed):
val apps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
// 2. Get the App Name and icon
val appName = packageManager.getApplicationLabel(appInfo).toString()
val appIcon = packageManager.getApplicationIcon(appInfo.packageName)
// 3. Get App permisions
val permissions = packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS).requestedPermissions
@cavin-macwan
cavin-macwan / typewritereffect.kt
Created March 17, 2025 18:40
Type writer text effect in jetpack compose
@Composable
fun TypeWriterTextAnimation() {
Box(
modifier = Modifier
.fillMaxSize()
.padding(10.dp),
contentAlignment = Alignment.Center
) {
TypewriterText(
baseText = "Jetpack Compose is ",
@cavin-macwan
cavin-macwan / JetpackComposePermission.kt
Created March 3, 2025 17:51
This gist is about permission handling implementation using permissions compose library
import android.Manifest
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
@cavin-macwan
cavin-macwan / Accordian.kt
Created February 20, 2025 18:16
Expand & Collapse Animation for Accordion in Jetpack Compose
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
@cavin-macwan
cavin-macwan / DotsAndLinesDemo.kt
Created February 20, 2025 17:52
Interactive Particle Network
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
@cavin-macwan
cavin-macwan / MetalPowerButton.kt
Created February 20, 2025 04:53
Metal Power Button in Jetpack Compose
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.spring
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.gestures.detectTapGestures
@cavin-macwan
cavin-macwan / CollapsingToolbar.kt
Created February 19, 2025 19:45
Collapsing Toolbar with Smooth Scroll Animation in Jetpack Compose
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
@cavin-macwan
cavin-macwan / ShakeContent.kt
Created February 18, 2025 17:28
Shake Animation Implementation (useful for error states)
/**
* ```
* @Composable
* fun ShakeScreen() {
* var shake by remember { mutableStateOf(false) }
* val interactionSource = remember { MutableInteractionSource() }
*
* Box(
* contentAlignment = Alignment.Center,
* modifier = Modifier.fillMaxSize()
@cavin-macwan
cavin-macwan / ExpandableCard.kt
Created February 13, 2025 19:16
ExpandableCard is a custom composable card that expands and collapses when clicked. It features smooth animations, a rotating dropdown icon, and dynamic content visibility, making it perfect for displaying information in a structured way.
/**
* Use it like this:
*
* ```
* @Composable
* fun ExpandableScreen() {
* Box(
* modifier = Modifier
* .padding(16.dp)
* .fillMaxSize(),