Skip to content

Instantly share code, notes, and snippets.

package com.tonal.trainer.anvilcompilers
import com.google.auto.service.AutoService
import com.squareup.anvil.annotations.ContributesTo
import com.squareup.anvil.compiler.api.AnvilContext
import com.squareup.anvil.compiler.api.CodeGenerator
import com.squareup.anvil.compiler.api.GeneratedFile
import com.squareup.anvil.compiler.api.createGeneratedFile
import com.squareup.anvil.compiler.internal.asClassName
import com.squareup.anvil.compiler.internal.buildFile
@lub0s
lub0s / vector_color_path.kt
Created February 21, 2022 13:02
Coloring vector paths by name in the jetpack compose
fun ImageVector.colorPath(pathName: String, color: Color): ImageVector {
val path = root.findPath(pathName)
val f = VectorPath::class.java.getDeclaredField("fill")
f.isAccessible = true
f.set(path, SolidColor(color))
return this
}
@lub0s
lub0s / fast_extensions.kt
Created February 11, 2022 10:53
Fast variants of List extensions for Kotlin
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
@OptIn(ExperimentalContracts::class)
internal inline fun <T> List<T>.fastForEach(action: (T) -> Unit) {
contract { callsInPlace(action) }
for (index in indices) {
val item = get(index)
action(item)
}
@lub0s
lub0s / compose_color_shaddows.kt
Created January 22, 2022 21:20
Color shadows hack by translating background
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
@lub0s
lub0s / compositions_count.kt
Last active January 26, 2022 09:25
Count the number of successful recompositions
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.remember
class Ref(var value: Int)
@Composable
inline fun LogRecompositions(composableTag: String) {
val ref = remember { Ref(0) }
@lub0s
lub0s / MaterialConversions.kt
Created January 14, 2022 09:56 — forked from hvisser/MaterialConversions.kt
Using both Material 3 and material components in Compose within the same theme
// conversions based on https://material.io/blog/migrating-material-3, deprecated colors set to Colors.Red
@Composable
fun fromMaterial3Theme(isLight: Boolean): Colors {
val scheme = MaterialTheme.colorScheme
return Colors(
primary = scheme.primary,
primaryVariant = Color.Red,
secondary = scheme.secondary,
secondaryVariant = Color.Red,
background = scheme.background,
@lub0s
lub0s / scale_video_ffmpeg
Created November 30, 2020 14:18
Scaling down a video with ffmpeg while preserving the aspect ratio
$ ffmpeg ... -vf "scale=w=min(iw\,1280):h=-2" ...
@lub0s
lub0s / debounce.kt
Last active November 30, 2020 13:58
Debounce with Android Handler
fun <Param> View.debounce(debounceMillis: Long = 300, block: (Param) -> Unit): (Param) -> Unit {
val handler = handler
var runnable: Runnable? = null
androidx.core.view.doOnDetach { runnable?.let(handler::removeCallbacks) }
return {
runnable?.let(handler::removeCallbacks)
val newRunnable = Runnable { block(it) }
handler.postDelayed(newRunnable, debounceMillis)
runnable = newRunnable
}
@lub0s
lub0s / viewbinding.kt
Created January 26, 2020 17:53
ViewBinding extension to inflate ViewBinding object based on requested type
inline fun <reified VB: ViewBinding> viewBinding(crossinline f: VB.() -> Unit) = { activity: Activity ->
val vbclass = VB::class.java
val inflate = vbclass.getMethod("inflate", LayoutInflater::class.java)
val binding = inflate.invoke(null, activity.layoutInflater) as VB
f(binding)
activity.setContentView(binding.root)
}
@lub0s
lub0s / testing-annotation.kt
Last active January 19, 2020 22:06
content test case
@Test
fun `validate file content for FooSummable`() {
val kotlinSource = SourceFile.kotlin(
"file2.kt", """
package com.tests.summable
import com.codegen.sample.IntSummable
@IntSummable
data class FooAlsoSummable(