Skip to content

Instantly share code, notes, and snippets.

View Takhion's full-sized avatar

Eugenio Marletti Takhion

View GitHub Profile
@Takhion
Takhion / ConvertJavaFilesToKotlin.kt
Created May 11, 2019 20:06
Convert Java files to Kotlin without the IDE
// adapted from https://github.com/JetBrains/kotlin/blob/0103c0d2fd8c9ae9e7f41898064407a6278c5831/j2k/tests/org/jetbrains/kotlin/j2k/AbstractJavaToKotlinConverterForWebDemoTest.kt
package me.eugeniomarletti
import com.intellij.codeInsight.ContainerProvider
import com.intellij.codeInsight.NullabilityAnnotationInfo
import com.intellij.codeInsight.NullableNotNullManager
import com.intellij.codeInsight.runner.JavaMainMethodProvider
import com.intellij.core.CoreApplicationEnvironment
import com.intellij.core.JavaCoreApplicationEnvironment
def buildConfigAndResStringField(variant, name, value) {
variant.resValue 'string', name.toLowerCase(), value
variant.buildConfigField 'String', name, "\"$value\""
}
afterEvaluate {
android.applicationVariants.all { variant ->
variant.resValue 'string', 'application_id', variant.applicationId
buildConfigAndResStringField variant, "ACCOUNT_TYPE", variant.applicationId
buildConfigAndResStringField variant, "CONTENT_AUTHORITY", variant.applicationId + ".provider"
@Takhion
Takhion / PairNestedDestructuring.kt
Last active December 20, 2022 09:12
Kotlin `Pair` nested destructuring
@file:Suppress("NonAsciiCharacters")
package me.eugeniomarletti.util.destructuring.pair.nested
object NoDestructuring
typealias PairA<A> = Pair<A, *>
typealias PairB<B> = Pair<*, B>
//region nesting level: 1
@Takhion
Takhion / demo.dart
Last active August 11, 2022 11:09
[Flutter] TextSelectionLayout (see https://stackoverflow.com/a/46244263/1306903)
// animated GIF: https://i.stack.imgur.com/mHOIc.gif
// see https://stackoverflow.com/a/46244263/1306903
import 'package:flutter/material.dart';
import 'ink_text_selection_layout.dart';
class Demo extends StatelessWidget {
@override
Widget build(context) => inkTextSelectionLayout(
@Takhion
Takhion / ArcUtils.java
Last active April 11, 2022 02:29
Collection of methods to achieve better circular arc drawing, as Canvas.drawArc() is unreliable. See the related article: https://medium.com/p/9155f49166b8
/**
* ArcUtils.java
*
* Copyright (c) 2014 BioWink GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@Takhion
Takhion / ExhaustiveWhen.kt
Last active July 28, 2021 19:19
Exhaustive `when` mapping in Kotlin
@file:Suppress("PackageDirectoryMismatch") // root package looks great when importing, ie. `import exhaustive`
/**
* Allows requiring an exhaustive mapping in a `when` block when used with the [rangeTo] operator.
* @sample [exhaustive_when_example]
*/
@Suppress("ClassName") // lowercase because it should look like a keyword
object exhaustive {
@Takhion
Takhion / MultiReceiver.kt
Created July 8, 2018 15:52
Multiple receivers in Kotlin
val sample: (X, Y, Z) -> Int = multiReceiver { { { x + y + z } } }
fun <A, B, C, R> multiReceiver(f: A.() -> B.() -> C.() -> R) = { a: A, b: B, c: C -> f(a)(b)(c) }
class X(val x: Int)
class Y(val y: Int)
class Z(val z: Int)
@Takhion
Takhion / RxFilterIsInstance.kt
Last active June 29, 2020 17:04 — forked from rock3r/RxFilterIsInstance.kt
Port Kotlin's awesome filterIsInstance() to RxJava — one call to filter by type and map-cast to that type
package me.seebrock3r.extensions.reactivex
import io.reactivex.Flowable
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
@Suppress("UNCHECKED_CAST") // The cast happens after we filter by type so it's safe
inline fun <reified R> Flowable<*>.filterIsInstance(): Flowable<R> =
filter { it is R }
@Takhion
Takhion / build.gradle
Last active May 28, 2020 11:13 — forked from dmarcato/strip_play_services.gradle
Gradle task to strip unused packages on Google Play Services library, so you don't have to use necessarily Proguard or MultiDex
apply from: 'strip_play_services.gradle'