Skip to content

Instantly share code, notes, and snippets.

Avatar

Héctor de Isidro hrules6872

View GitHub Profile
@hrules6872
hrules6872 / TextViewDrawableSize.java
Last active Apr 27, 2022
TextViewDrawableSize - CompoundDrawable size
View TextViewDrawableSize.java
public class TextViewDrawableSize extends TextView {
private static final int DEFAULT_COMPOUND_DRAWABLE_SIZE = -1;
private int compoundDrawableWidth;
private int compoundDrawableHeight;
public TextViewDrawableSize(Context context) {
this(context, null);
}
public TextViewDrawableSize(Context context, AttributeSet attrs) {
@hrules6872
hrules6872 / Store.kt
Last active Apr 17, 2022
The Redux/Kotlin Killer :)
View Store.kt
interface Action
typealias Middleware<STATE> = (STATE, Action, Dispatcher, NextMiddleware<STATE>) -> Action
typealias NextMiddleware<STATE> = (STATE, Action, Dispatcher) -> Action
typealias Reducer<STATE> = (STATE, Action) -> STATE
interface State
typealias Binder<STATE> = (STATE, STATE) -> Boolean
@hrules6872
hrules6872 / ZipManager.kt
Created Apr 16, 2022
Zip/Unzip functions in Kotlin
View ZipManager.kt
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)
}
}
}
@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 }
@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 / 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 / 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 / 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 / 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 / Base64.kt
Last active May 31, 2021
Base64.kt for Kotlin
View Base64.kt
fun String.encodeBase64ToString(): String = String(this.toByteArray().encodeBase64())
fun String.encodeBase64ToByteArray(): ByteArray = this.toByteArray().encodeBase64()
fun ByteArray.encodeBase64ToString(): String = String(this.encodeBase64())
fun String.decodeBase64(): String = String(this.toByteArray().decodeBase64())
fun String.decodeBase64ToByteArray(): ByteArray = this.toByteArray().decodeBase64()
fun ByteArray.decodeBase64ToString(): String = String(this.decodeBase64())
fun ByteArray.encodeBase64(): ByteArray {
val table = (CharRange('A', 'Z') + CharRange('a', 'z') + CharRange('0', '9') + '+' + '/').toCharArray()