Skip to content

Instantly share code, notes, and snippets.

Avatar

Héctor de Isidro hrules6872

View GitHub Profile
@hrules6872
hrules6872 / CircleView.kt
Created Jan 12, 2022
Circle View for Android in Kotlin
View CircleView.kt
class CircleView @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 } }
@ColorInt
var color: Int = Color.TRANSPARENT
set(value) {
field = value
invalidate()
}
@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
}
}
@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 / WindowInsetsFrameLayout.kt
Created Dec 2, 2019
A fragment container enabling the use of android:fitsSystemWindows in fragment layouts
View WindowInsetsFrameLayout.kt
class WindowInsetsFrameLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
init {
setOnHierarchyChangeListener(object : OnHierarchyChangeListener {
override fun onChildViewAdded(
parent: View,
@hrules6872
hrules6872 / LineView.kt
Last active Dec 10, 2019
Dash/LineView custom view for Android
View LineView.kt
class LineView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
companion object {
const val ORIENTATION_VERTICAL = 0
const val ORIENTATION_HORIZONTAL = 1
private const val DEFAULT_ORIENTATION = ORIENTATION_HORIZONTAL
@hrules6872
hrules6872 / KotlinDsl.kt
Last active Dec 10, 2019
Kotlin DSL example
View KotlinDsl.kt
@DslMarker
annotation class HorizontalSelectorGroups
interface HorizontalSelectorHelper {
fun groups(block: GroupsBuilder.() -> Unit): List<Pair<Header, List<Item>>> = GroupsBuilder().apply(block).build()
@HorizontalSelectorGroups
class GroupsBuilder {
private val groups = mutableListOf<Group>()
@hrules6872
hrules6872 / StickyHeaderItemDecoration.kt
Last active Sep 11, 2019
ItemDecorator for sticky headers in Kotlin
View StickyHeaderItemDecoration.kt
import android.graphics.*
import android.view.*
import android.view.MotionEvent.ACTION_DOWN
import androidx.recyclerview.widget.RecyclerView
class StickyHeaderItemDecoration(
parent: RecyclerView,
private val isHeader: (position: Int) -> Boolean
) : RecyclerView.ItemDecoration() {
@hrules6872
hrules6872 / delete-darktable-rejected.sh
Created Aug 18, 2019
Delete all Darktable rejected and low-rated pictures
View delete-darktable-rejected.sh
#!/bin/sh
if [ -z "$1" ]; then
echo Delete all Darktable rejected pictures
echo
echo "Usage:"
echo $0 [source] --onestar to delete also all pictures with one star rating
exit 1
fi
@hrules6872
hrules6872 / ClazzesExt.kt
Last active Aug 12, 2019
Reduce two (Data) Classes in Kotlin
View ClazzesExt.kt
inline infix fun <reified T : Any> T?.reduce(other: T?): T? = when {
this == null && other == null -> null
this != null && other == null -> this
this == null && other != null -> other
else -> {
val nameToProperty = T::class.declaredMemberProperties.associateBy { property -> property.name }
val primaryConstructor = T::class.primaryConstructor!!
val args = primaryConstructor.parameters.associate { parameter ->
val property = nameToProperty[parameter.name]!!