Skip to content

Instantly share code, notes, and snippets.

View LouisCAD's full-sized avatar

Louis CAD LouisCAD

View GitHub Profile
@LouisCAD
LouisCAD / AndroidLocationFlow.kt
Last active September 1, 2021 06:50
Exposes Location updates from Android's LocationManager as a Flow. Also see https://gist.github.com/LouisCAD/0a648e2b49942acd2acbb693adfaa03a
import android.location.Criteria
import android.location.Location
import android.location.LocationListener
import android.Manifest
import android.os.Bundle
import androidx.annotation.RequiresApi
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import splitties.systemservices.locationManager // See https://splitties.louiscad.com/modules/systemservices/
@LouisCAD
LouisCAD / !README.md
Created February 28, 2019 11:31 — forked from Ribesg/!README.md
How to use an iOS Framework in a Kotlin MPP library published to Maven

This gist demonstrates how to build a Kotlin MPP library so that the iOS sourceSet depends on and uses an iOS Framework as a dependency.

Key ideas:

  • We use [Carthage] to retrieve/build the iOS Framework.
  • We use [cinterop] to create bindings allowing us to use the iOS Framework from Kotlin
  • We build and publish the library using ./gradlew publishToMavenLocal
  • We build and publish [klib] artifacts for both the arm64 and x86_64 architectures, you can easily add arm32 if you need.
  • Note that the publish process also publishes a cinterop klib artifact, allowing dependents to also know about the iOS Framework headers.

You can find a gist explaining how to use such library in an iOS app [here][ios-app].

@Suppress("NOTHING_TO_INLINE")
inline operator fun <E> ConflatedBroadcastChannel<E>.getValue(
thisRef: Any?,
prop: KProperty<*>
): E = value
@Suppress("NOTHING_TO_INLINE")
inline operator fun <E> ConflatedBroadcastChannel<E>.setValue(
thisRef: Any?,
prop: KProperty<*>,
@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 / Lol.kt
Last active February 14, 2019 06:21 — forked from npryce/layout.kt
Kotlin layout hack, up to the right margin (100 chars)
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
@LouisCAD
LouisCAD / UsbPermissions.kt
Last active November 30, 2017 20:12 — forked from bj0/UsbPermissions.kt
Requesting permission to open UsbDevice in Kotlin using coroutines
package com.my.app
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbManager
import kotlinx.coroutines.experimental.CompletableDeferred
@LouisCAD
LouisCAD / GooglePlayServices.kt
Created November 23, 2017 16:19
Allows using Google Play Services Task API in Kotlin Coroutines, plus Play Services availability check made easier.
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.tasks.Task
import splitties.init.appCtx
import kotlin.coroutines.experimental.suspendCoroutine
val googleApiAvailability = GoogleApiAvailability.getInstance()!!
inline val playServicesAvailability get() = googleApiAvailability.isGooglePlayServicesAvailable(appCtx)
@JvmName("awaitVoid")
suspend fun Task<Void>.await() = suspendCoroutine<Unit> { continuation ->
@LouisCAD
LouisCAD / gmail_email.py
Created October 8, 2017 19:08 — forked from erans/gmail_email.py
Check if an Email address is Gmail or Google Apps for your domain
import sys
import re
import dns.resolver # Requires dnspython
email_host_regex = re.compile(".*@(.*)$")
gmail_servers_regex = re.compile("(.google.com.|.googlemail.com.)$", re.IGNORECASE)
def is_gmail(email):
""" Returns True if the supplied Email address is a @gmail.com Email or is a Google Apps for your domain - hosted Gmail address
Checks are performed by checking the DNS MX records """
@LouisCAD
LouisCAD / TextWatcher.kt
Last active June 4, 2018 13:00
EditText TextWatcher, without the override me again ceremony
import android.text.Editable
import android.widget.EditText
import splitties.collections.forEachByIndex
import android.text.TextWatcher as AndroidTextWatcher
interface TextWatcher : AndroidTextWatcher {
override fun afterTextChanged(s: Editable) = Unit
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
}
@LouisCAD
LouisCAD / JobDispatcherReschedulerWorkaroundService.kt
Last active January 9, 2018 06:11
A fix for Firebase Job Dispatcher issue #6 (https://github.com/firebase/firebase-jobdispatcher-android/issues/6). You need to extend PersistedJobService instead of JobService for this to work, and call Jobs.schedulePresisted(tag)
import com.google.android.gms.gcm.GcmTaskService
import com.google.android.gms.gcm.TaskParams
import com.example.androidapp.jobs.Jobs
import com.example.androidapp.jobs.tagsOfScheduledJobs
import timber.log.Timber
/**
* See [this issue](https://github.com/firebase/firebase-jobdispatcher-android/issues/6).
*/
class JobDispatcherReschedulerWorkaroundService : GcmTaskService() {