Skip to content

Instantly share code, notes, and snippets.

View shahzadansari's full-sized avatar

Shahzad Ansari shahzadansari

View GitHub Profile
@shahzadansari
shahzadansari / SideEffect.kt
Created November 22, 2023 19:09
SideEffect Demo (runs every time composable gets recomposed)
Box(modifier = Modifier.fillMaxSize()) {
var count by remember { mutableIntStateOf(0) }
StateDependentComposable(count)
Button(
onClick = { count++ },
content = { Text("Increment") },
modifier = Modifier.align(Alignment.Center)
)
}
@shahzadansari
shahzadansari / Playground.kt
Created November 22, 2023 18:29
Sample showing how logs can cause recompositions and how to fix them
@Composable
fun LogsCausingRecompositions() {
Box(modifier = Modifier.fillMaxSize()) {
var count by remember { mutableIntStateOf(0) }
Log.d("MyTag:", "Count: $count")
Button(
onClick = { count++ },
content = { Text("Increment") },
@shahzadansari
shahzadansari / LifecycleObserver.kt
Last active October 25, 2023 17:08
LifecycleObserver with DisposableEffect
@Composable
fun Screen() {
LifecycleObserver { event ->
if (event == Lifecycle.Event.ON_RESUME) {
// doSomething()
}
}
}
/** Inspired by: https://medium.com/@mohammadjoumani/life-cycle-in-jetpack-compose-2e96136ab936 **/
@shahzadansari
shahzadansari / build.gradle.kts
Last active November 26, 2023 09:30
Custom gradle task which copies app-debug.apk to Desktop and renames it to Git's current checked out branch appended with current time in 12-hour format
// 1. Add following snippet in your build.gradle.kts(:app)
// Run this command in terminal -> ./gradlew copyDebugApkToDesktopWithTime
// Note: It will replace "/" in your branch name with "-" as "/" creates a directory.
// fix/bug becomes fix-bug. You can adjust it as per your needs.
import java.text.SimpleDateFormat
import java.util.Date
tasks.register("copyDebugApkToDesktopWithTime") {
@shahzadansari
shahzadansari / build.gradle.kts
Last active November 26, 2023 09:29
Custom gradle task which copies app-debug.apk to Desktop and renames it to Git's current checked out branch
// 1. Add following snippet in your build.gradle.kts(:app)
// Run this command in terminal -> ./gradlew copyDebugApkToDesktop
// Note: It will replace "/" in your branch name with "-" as "/" creates a directory.
// fix/bug becomes fix-bug. You can adjust it as per your needs.
tasks.register("copyDebugApkToDesktop") {
dependsOn("assembleDebug")
doLast {
val sourceDir = "build/outputs/apk/debug/app-debug.apk"
@shahzadansari
shahzadansari / ConnectionLiveData.kt
Created May 27, 2022 20:04
A utility class for monitoring network connection in real-time with LiveData.
import android.content.Context
import android.content.Context.CONNECTIVITY_SERVICE
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET
import android.net.NetworkRequest
import android.util.Log
import androidx.lifecycle.LiveData
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers