Skip to content

Instantly share code, notes, and snippets.

@hector6872
hector6872 / KotlinDsl.kt
Last active December 10, 2019 20:49
Kotlin DSL example
@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>()
@hector6872
hector6872 / LineView.kt
Last active December 10, 2019 20:48
Dash/LineView custom view for Android
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
@hector6872
hector6872 / WindowInsetsFrameLayout.kt
Created December 2, 2019 10:03
A fragment container enabling the use of android:fitsSystemWindows in fragment layouts
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,
@hector6872
hector6872 / ComposeStore.kt
Last active February 22, 2023 09:49
Redux implementation for Kotlin :)
private val LocalStore: ProvidableCompositionLocal<Store<*>> = compositionLocalOf { error("Store not provided") }
@Composable
fun <STATE : State> StoreProvider(store: Store<STATE>, content: @Composable Store<STATE>.() -> Unit) {
CompositionLocalProvider(LocalStore provides store) {
store.content()
}
}
@Composable
@hector6872
hector6872 / Modules.kt
Last active April 23, 2025 12:23
Yet another Koin killer :)
/*
* Copyright (c) 2022. Héctor de Isidro - hector6872
*
* 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
@hector6872
hector6872 / FixedColumnsHorizontalGridLayoutManager.kt
Created January 27, 2021 18:32
Android FixedColumnsHorizontalGridLayoutManager
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))
@hector6872
hector6872 / TimeAgo.kt
Created November 26, 2021 15:27
Destructuring a Date into Years, Months, Weeks, Days, Hours, Minutes and Seconds in Kotlin
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
}
}
@hector6872
hector6872 / TriangleView.kt
Last active January 12, 2022 18:15
Triangle View for Android in Kotlin
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()
}
@hector6872
hector6872 / CircleView.kt
Created January 12, 2022 17:57
Circle View for Android in Kotlin
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()
}
@hector6872
hector6872 / ZipManager.kt
Created April 16, 2022 17:43
Zip/Unzip functions in Kotlin
object ZipManager {
fun zip(files: List<String>, outputStream: OutputStream): Boolean = try {
ZipOutputStream(BufferedOutputStream(outputStream)).use { stream ->
for (file in files) {
BufferedInputStream(FileInputStream(File(file))).use { origin ->
stream.putNextEntry(ZipEntry(file.substring(file.lastIndexOf("/") + 1)))
origin.copyTo(stream)
}
}
}