Skip to content

Instantly share code, notes, and snippets.

Avatar

Héctor de Isidro hrules6872

View GitHub Profile
@hrules6872
hrules6872 / TriangleView.kt
Last active Jan 12, 2022
Triangle View for Android in Kotlin
View TriangleView.kt
class TriangleView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {
private val paint by lazy { Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL } }
private val path: Path = Path()
@ColorInt
var color: Int = Color.TRANSPARENT
set(value) {
field = value
invalidate()
}
@hrules6872
hrules6872 / TimeAgo.kt
Created Nov 26, 2021
Destructuring a Date into Years, Months, Weeks, Days, Hours, Minutes and Seconds in Kotlin
View TimeAgo.kt
object TimeAgo {
fun toRelative(millis: Long, spans: List<Span>): Map<Span, Long> {
var millisMutable = millis
return spans.sortedBy(Span::order).associateWith { span ->
val timeDelta: Long = millisMutable / span.millis
if (timeDelta.isGreaterThanZero()) millisMutable -= span.millis * timeDelta
timeDelta
}
}
@mwolfson
mwolfson / Color.kt
Last active Jan 27, 2022
Material Design Color Resources for Jetpack Compose
View Color.kt
import androidx.compose.ui.graphics.Color
// Material Color Resources
val amber_050 = Color(0xFFfff8e1) // Use with black text
val amber_100 = Color(0xFFffecb3) // Use with black text
val amber_200 = Color(0xFFffe082) // Use with black text
val amber_300 = Color(0xFFffd54f) // Use with black text
val amber_400 = Color(0xFFffca28) // Use with black text
val amber_500 = Color(0xFFffc107) // Use with black text
@hrules6872
hrules6872 / FixedColumnsHorizontalGridLayoutManager.kt
Created Jan 27, 2021
Android FixedColumnsHorizontalGridLayoutManager
View FixedColumnsHorizontalGridLayoutManager.kt
class FixedColumnsHorizontalGridLayoutManager(context: Context?, spanCountVertical: Int, private var columns: Int, orientation: Int, reverseLayout: Boolean) :
GridLayoutManager(context, spanCountVertical, orientation, reverseLayout) {
private val safeWidth: Int
get() = width - paddingRight - paddingLeft
override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams = spanLayoutSize(super.generateDefaultLayoutParams())
override fun generateLayoutParams(context: Context, attrs: AttributeSet): RecyclerView.LayoutParams = spanLayoutSize(super.generateLayoutParams(context, attrs))
override fun generateLayoutParams(layoutParams: ViewGroup.LayoutParams): RecyclerView.LayoutParams = spanLayoutSize(super.generateLayoutParams(layoutParams))
@hrules6872
hrules6872 / Modules.kt
Last active Feb 16, 2022
The Koin Killer :)
View Modules.kt
private val data = submodule {
/* ... */
}
private val usecases = submodule {
/* ... */
}
val locator = locator {
single<Application> { App.get() }
single<Context> { get<Application>().applicationContext }
View git-stats.sh
for name in $(git log --pretty="%ae%n%cn" | sort | uniq); do
git log \
--author "$name" \
--pretty=tformat: --numstat |
awk -v name="$name" '{
add += $1; subs += $2; loc += $1 - $2
} END {
if (loc != 0)
printf "[%s] Lines: +\033[32m%s\033[0m -\033[31m%s\033[0m; Total: %s\n", name, add, subs, loc
}'
@DRSchlaubi
DRSchlaubi / zip.kt
Last active Apr 16, 2022
Kotlin zip utils
View zip.kt
/*
* Copyright 2020 Michael Rittmeister
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
View AbsolutefitLayoutManager.java
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class AbsolutefitLayourManager extends GridLayoutManager {
@Mun0n
Mun0n / CircleEdgeTreatment
Created Mar 20, 2020
Custom edge treatment to create a circle edge in a shape
View CircleEdgeTreatment
import com.google.android.material.shape.EdgeTreatment
import com.google.android.material.shape.ShapePath
import kotlin.math.atan
import kotlin.math.sqrt
class CircleEdgeTreatment
(
private val radius: Float,
private val cradleRoundedCornerRadius: Float
) :
@faruktoptas
faruktoptas / debounce.kt
Created Mar 5, 2020
Kotlin coroutine debounce for EditText
View debounce.kt
fun <T> debounce(
waitMs: Long = 300L,
scope: CoroutineScope,
destinationFunction: (T) -> Unit
): (T) -> Unit {
var debounceJob: Job? = null
return { param: T ->
debounceJob?.cancel()
debounceJob = scope.launch {
delay(waitMs)