Skip to content

Instantly share code, notes, and snippets.

View FireZenk's full-sized avatar
🌍
Improving the world

Jorge Garrido FireZenk

🌍
Improving the world
View GitHub Profile
@FireZenk
FireZenk / ExampleRoute1.kt
Last active January 26, 2024 10:38
Jetpack's Android navigation made simple
package com.github.firezenk
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.composable
import com.github.firezenk.Route
import com.github.firezenk.exampleRoute2
internal val exampleRoute1 = Route("ExampleRoute1", linkedMapOf(
@FireZenk
FireZenk / LifecycleExt.kt
Created April 22, 2022 09:53
Lifecycle event listener for Compose
@Composable
fun OnLifecycleEvent(onEvent: (owner: LifecycleOwner, event: Lifecycle.Event) -> Unit) {
val eventHandler = rememberUpdatedState(onEvent)
val lifecycleOwner = rememberUpdatedState(LocalLifecycleOwner.current)
DisposableEffect(lifecycleOwner.value) {
val lifecycle = lifecycleOwner.value.lifecycle
val observer = LifecycleEventObserver { owner, event ->
eventHandler.value(owner, event)
}
@FireZenk
FireZenk / BluetoothExt.kt
Created April 22, 2022 09:50
Bluetooth activation request for Compose
@Composable
fun ActivateBluetooth(launcher: (() -> Unit) -> Unit, isRequestingActivation: (Boolean) -> Unit,
onActivated: () -> Unit) {
val bluetoothActivationLauncher =
rememberLauncherForActivityResult(RequestBluetoothActivation()) {
if (it) {
isRequestingActivation(false)
onActivated()
}
}
@FireZenk
FireZenk / LocationExt.kt
Last active April 22, 2022 09:51
Open Location services activation settings from Compose
@Composable
fun ActivateLocationServices() {
val context = LocalContext.current
val lm = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (!LocationManagerCompat.isLocationEnabled(lm))
context.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
}
@FireZenk
FireZenk / PermissionsExt.kt
Created April 22, 2022 09:46
Android permission request for Compose
@ExperimentalPermissionsApi
@Composable
fun CheckPermissions(permissions: List<String>, launcher: (() -> Unit) -> Unit,
onAllGranted: () -> Unit, onPermissionsNeeded: () -> Unit, ) {
val multiplePermissionsState = rememberMultiplePermissionsState(permissions)
val isAlreadyGranted = remember { mutableStateOf(false) }
launcher { multiplePermissionsState.launchMultiplePermissionRequest() }
val allGranted = multiplePermissionsState.allPermissionsGranted
if (allGranted && !isAlreadyGranted.value) {
@FireZenk
FireZenk / build.gradle
Created March 30, 2020 14:04
Obfuscate jar from java-library gradle projects
import proguard.gradle.ProGuardTask
import java.nio.file.Paths
dependencies {
[...]
// Required by @task::obfuscate
compileOnly 'org.jetbrains:annotations:13.0'
}
def jarNameWithoutExtension = jar.archiveName.with { it.take(it.lastIndexOf(".")) }
@FireZenk
FireZenk / Action.kt
Created February 25, 2020 12:14
MVI Android + Rxjava2
open class Action
@FireZenk
FireZenk / TimberExt.kt
Created March 29, 2019 09:48
Dumps the Android Intent content on Timber
import android.content.Intent
import timber.log.Timber
fun dumpIntent(intent: Intent) {
val bundle = intent.extras
if (bundle != null) {
val keys = bundle.keySet()
val it = keys.iterator()
Timber.e("------->>> Dumping Intent start")
while (it.hasNext()) {
@FireZenk
FireZenk / TextViewExt.kt
Created November 15, 2018 13:55
A collection of TexView extension functions (justify fun for now)
import android.text.Spanned
import android.text.style.ImageSpan
import android.graphics.drawable.ColorDrawable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.widget.TextView
import java.util.concurrent.atomic.AtomicBoolean
fun TextView.justify() {
@FireZenk
FireZenk / gzip.kts
Last active November 22, 2018 15:09 — forked from sgdan/gzip.kts
Kotlin code to compress/uncompress a string with gzip
import java.io.ByteArrayOutputStream
import java.io.File
import java.nio.charset.StandardCharsets.UTF_8
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream
fun String.gzip(): ByteArray {
val bos = ByteArrayOutputStream()
GZIPOutputStream(bos).bufferedWriter(UTF_8).use { it.write(this) }
return bos.toByteArray()