Skip to content

Instantly share code, notes, and snippets.

View LouisCAD's full-sized avatar

Louis CAD LouisCAD

View GitHub Profile
@LouisCAD
LouisCAD / FusedLocationFlow.kt
Last active January 31, 2024 20:56
Create a Flow of location updates on Android (using kotlinx.coroutines), backed by Fused Location Provider from Google Play Services.
/*
* Copyright 2019 Louis Cognault Ayeva Derman. Use of this source code is governed by the Apache 2.0 license.
*/
import android.location.Location
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationResult
import com.google.android.gms.location.LocationServices
import kotlinx.coroutines.CancellationException
@LouisCAD
LouisCAD / Companion object Powers example.kt
Last active January 21, 2024 07:45
Demonstration of the powers of companion objects when it comes to discoverability. Great alternative to java-style "factories".
package i_love_companion_objects
suspend fun main() {
// Discoverability is easy with the companion object
// (although autocomplete could use some performance improvements...)
doStuff(SomeInterface.createWithDefaults())
val someInstance: SomeInterface = SomeInterface.create(
parameterOne = 0,
parameterTwo = 1
)
@LouisCAD
LouisCAD / ModalBottomSheet.kt
Last active November 20, 2023 09:16
Put this in its own Gradle module to have ModalBottomSheet in Material3 (or even without Material Design at all).
package com.louiscad.splitties.eap.bottomsheet
import android.app.Activity
import android.view.ViewGroup
import androidx.activity.compose.BackHandler
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetLayout
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
@LouisCAD
LouisCAD / Flow <-> LiveData.kt
Created October 23, 2019 15:35
Convert a Flow to a LiveData and use LiveData as a Flow (from kotlinx.coroutines).
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.liveData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.*
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.seconds
@LouisCAD
LouisCAD / BitFlags.kt
Last active December 24, 2022 16:28
Allows simple bit flags operation on int values in kotlin. Now available in Splitties: https://github.com/LouisCAD/Splitties/tree/master/modules/bitflags Inspired by: http://stackoverflow.com/a/40588216/4433326
@file:Suppress("NOTHING_TO_INLINE")
import kotlin.experimental.and // Used for Byte
import kotlin.experimental.inv // Used for Byte
import kotlin.experimental.or // Used for Byte
inline fun Int.hasFlag(flag: Int) = flag and this == flag
inline fun Int.withFlag(flag: Int) = this or flag
inline fun Int.minusFlag(flag: Int) = this and flag.inv()
@LouisCAD
LouisCAD / Flow.collectInParallel.kt
Last active December 13, 2022 16:05
Collect a Flow in parallel
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
suspend fun <T> Flow<T>.collectInParallel(
concurrency: Int,
collector: suspend (T) -> Unit
) = coroutineScope {
val channel: Channel<T> = Channel()
repeat(concurrency) { launch { for (e in channel) collector(e) } }
@LouisCAD
LouisCAD / Flow.chunked.kt
Created December 7, 2022 18:25
Cut a Flow into chunks for batch processing.
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
fun <T> Flow<T>.chunked(size: Int): Flow<List<T>> = flow {
val elements = ArrayList<T>(size)
collect {
elements.add(it)
if (elements.size == size) {
emit(elements.toList())
@LouisCAD
LouisCAD / LifecycleCoroutines.kt
Last active July 3, 2022 11:47
CoroutineScope and Job integration with Lifecycle for Android. Meant to be used for your coroutines in lifecycle aware components. OUTDATED. See up to date implementation here: https://github.com/LouisCAD/Splitties/tree/master/modules/lifecycle-coroutines
import android.arch.lifecycle.GenericLifecycleObserver
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.Lifecycle.Event.ON_DESTROY
import android.arch.lifecycle.LifecycleOwner
import kotlinx.coroutines.experimental.CoroutineScope
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.android.Main
fun Lifecycle.createJob(cancelEvent: Lifecycle.Event = ON_DESTROY): Job {
@LouisCAD
LouisCAD / AppInitProvider.kt
Created January 23, 2017 10:08
Use ContentProvider to init your application. This example sets up Timber logging and a "crash shield" that can do what you want 5 seconds after an uncaught exception.
import android.app.AlarmManager
import android.app.AlarmManager.ELAPSED_REALTIME_WAKEUP
import android.app.PendingIntent
import android.content.Context
import android.os.SystemClock
import android.support.annotation.IntRange
import com.google.firebase.provider.FirebaseInitProvider
import org.jetbrains.anko.alarmManager
import timber.log.Timber
@LouisCAD
LouisCAD / SystèmeInternationalUnits_draft.kt
Created September 14, 2021 10:25
A partial double precision floating point based implementation of the Système International of Units.
@file:Suppress("experimental_is_not_enabled")
@file:OptIn(ExperimentalTime::class)
import kotlin.math.sqrt
import kotlin.time.Duration
import kotlin.time.DurationUnit
import kotlin.time.ExperimentalTime
//// Metrics