Skip to content

Instantly share code, notes, and snippets.

Avatar

Louis CAD LouisCAD

View GitHub Profile
@LouisCAD
LouisCAD / Document.kt
Created May 24, 2021
Kotlin extensions for javax.xml (package org.w3c.dom). Feel free to copy paste.
View Document.kt
import org.w3c.dom.Document
import org.w3c.dom.Text
import java.io.StringWriter
import javax.xml.transform.OutputKeys
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult
fun Document.toText(): String {
val domSource = DOMSource(this)
@LouisCAD
LouisCAD / CharSequenceGetLineAndColumn.kt
Created Mar 24, 2021
Get the line and the column of an index in a given CharSequence.
View CharSequenceGetLineAndColumn.kt
/**
* Returns the line and the column of [index] in the given CharSequence.
*/
fun CharSequence.getLineAndColumn(index: Int): LocationInText {
if (index !in 0..lastIndex) {
throw IndexOutOfBoundsException(index)
}
var lineNumber = 1
var columnNumber = 1
forEachIndexed { currentIndex, c ->
View MutableLazy.kt
import java.io.Serializable
import kotlin.reflect.KProperty
interface MutableLazy<T> : Lazy<T> {
override var value: T
}
fun <T> mutableLazyOf(value: T): MutableLazy<T> = InitializedLazyImpl(value)
fun <T> mutableLazy(initializer: () -> T): MutableLazy<T> = SynchronizedLazyImpl(initializer)
@LouisCAD
LouisCAD / Companion object Powers example.kt
Last active May 3, 2020
Demonstration of the powers of companion objects when it comes to discoverability. Great alternative to java-style "factories".
View Companion object Powers example.kt
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 / Flow <-> LiveData.kt
Created Oct 23, 2019
Convert a Flow to a LiveData and use LiveData as a Flow (from kotlinx.coroutines).
View Flow <-> LiveData.kt
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 / FusedLocationFlow.kt
Last active Dec 15, 2020
Create a Flow of location updates on Android (using kotlinx.coroutines), backed by Fused Location Provider from Google Play Services.
View FusedLocationFlow.kt
/*
* 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 / !README.md
Created Feb 28, 2019 — forked from Ribesg/!README.md
How to use an iOS Framework in a Kotlin MPP library published to Maven
View !README.md

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].

View ConflatedBroadcastChannelDelegate.kt
@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 May 9, 2021
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
View LifecycleCoroutines.kt
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 Feb 14, 2019 — forked from npryce/layout.kt
Kotlin layout hack, up to the right margin (100 chars)
View Lol.kt
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