Skip to content

Instantly share code, notes, and snippets.

View fluidsonic's full-sized avatar

Marc Knaup fluidsonic

View GitHub Profile
@fluidsonic
fluidsonic / NestedScrollView.kt
Last active October 21, 2023 09:32
A RecyclerView subclass for Android which makes descendent NestedScrollViews scrollable
package com.github.fluidsonic.nestedrecyclerview
import android.content.Context
import android.support.v4.view.NestedScrollingParent
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
@fluidsonic
fluidsonic / example.js
Created September 20, 2018 12:13
Move cursor in contenteditable HTML element to the end
const contenteditableElement = …
const selection = window.getSelection()
selection.selectAllChildren(contenteditableElement)
selection.collapseToEnd()
@fluidsonic
fluidsonic / QueryConsideringContentNegotiation.kt
Created November 9, 2018 11:30
ContentNegotiation feature for ktor which passing the entity as query parameter for requests where clients can't pass an entity in the body
import io.ktor.application.ApplicationCallPipeline
import io.ktor.application.ApplicationFeature
import io.ktor.application.call
import io.ktor.features.ContentNegotiation
import io.ktor.features.UnsupportedMediaTypeException
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.HttpStatusCodeContent
import io.ktor.http.content.OutgoingContent
import io.ktor.http.content.transformDefaultContent
@fluidsonic
fluidsonic / StringBuilder.kt
Created May 20, 2019 13:48
StringBuilder.replace
actual fun StringBuilder.replace(start: Int, end: Int, replacement: String) = apply {
check(start in 0 .. length) { "start must be in 0 .. $length: $start" }
check(end in start .. length) { "end must be in $start .. $length: $end" }
when {
start == end -> Unit
start == 0 ->
when (end) {
length -> {
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty0
import kotlin.reflect.jvm.isAccessible
class SomeClass {
var someProperty by observable(1)
}
@fluidsonic
fluidsonic / Merge.ts
Created January 9, 2020 11:59
Merges a union type into a type with properties of all union components
type Merge<Union> = AddOptionalProperties<Union, keyof UnionToIntersection<Union>>
type AddOptionalProperties<Union, Properties extends keyof any> =
Union extends (infer Component)
? { [P in keyof Union]: Union[P] } & { [P in Exclude<Properties, keyof Union>]?: never }
: never
// https://stackoverflow.com/a/50375286/1183577
type UnionToIntersection<Union> =
(Union extends any ? (_: Union) => void : never) extends ((_: infer Component) => void) ? Component : never
interface Foo {
a: number; // no b
}
interface Bar {
a: number; b: string
}
function logB(value: Foo | Bar) {
console.log(value.b) // We cannot access .b as it isn't defined on Foo.
interface Foo {
a: number; // no b
}
interface Bar {
a: number; b: string
}
function logB(value: Merge<Foo | Bar>) {
console.log(value.b) // .b is now an optional 'string' property
// same as Merge<Foo | Bar>
interface MergeOfFooAndBar {
a: number
b?: string
}
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.sync.*
import kotlin.coroutines.*
@Suppress("EXPERIMENTAL_API_USAGE")
suspend fun main() {
coroutineScope {