Skip to content

Instantly share code, notes, and snippets.

View ElianFabian's full-sized avatar

Elián Fabián ElianFabian

View GitHub Profile
@ElianFabian
ElianFabian / AndroidExt.kt
Created November 9, 2024 18:50
Android extensions
import android.content.Intent
import android.os.Bundle
@Suppress("DEPRECATION")
fun Bundle.contentToString(): String {
return "Bundle(${
keySet().joinToString { key ->
when (val value = get(key)) {
is Bundle -> "$key=${value.contentToString()}"
is Array<*> -> "$key=${value.contentToString()}"
@ElianFabian
ElianFabian / ExtendedCardView.kt
Last active September 22, 2024 20:32
A CardView that allows setting the corner radius as a percentage. It also fix the issue of the corner radius exceeding the 50% limit on API 22 and below which causes the card to look weird.
package yourpackage
import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.view.View
import androidx.cardview.widget.CardView
import yourpackage.R
/**
@ElianFabian
ElianFabian / LanguageContextWrapper.kt
Last active September 20, 2024 17:03
ContextWrapper to propperly update the locale of your application. The default implementation takes the first supported locale, otherwise takes the default locale.. Gist to generate the supported and default locale: Gist to generate the supported locales: https://gist.github.com/ElianFabian/e94071f348998c24bedaa959ddb8df40
import android.annotation.SuppressLint
import android.app.Application
import android.content.ComponentCallbacks
import android.content.Context
import android.content.ContextWrapper
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import android.os.LocaleList
import androidx.core.os.ConfigurationCompat
/**
* Source: https://github.com/JuliaMath/LambertW.jl/blob/master/src/LambertW.jl
*/
object LambertW {
// Lambert W function for real z
fun lambertW(z: Double, k: Int = 0, maxIts: Int = 1000): Double {
return _lambertW(z, k, maxIts)
}
using System;
// From the Kotlin standard library
public class XorWowRandom
{
private int x, y, z, w, v, addend;
private XorWowRandom() { }
public static XorWowRandom FromSeed(int seed)
@ElianFabian
ElianFabian / RandomExt.kt
Created July 12, 2024 17:06
Set seed of XorWowRandom in Kotlin.
import kotlin.random.Random
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.jvm.isAccessible
fun main() {
val random = Random(42)
println(random.nextInt())
println(random.nextInt())
println(random.nextInt())
println(random.nextInt())
@ElianFabian
ElianFabian / PariGp.ps1
Created July 11, 2024 19:17
Example of how to use PARI/GP inside PowerShell
function Get-PrimeNumber([ulong] $position) {
return [System.UInt128] ("prime($position)" | gp.exe -q)
}
import kotlin.experimental.and
import kotlin.experimental.or
import kotlin.experimental.xor
fun UByte.toFixedBinaryString(): String {
return toString(2).padStart(UByte.SIZE_BITS, '0')
}
fun UShort.toFixedBinaryString(byteSeparator: String = ""): String {
val binaryString = toString(2).padStart(UShort.SIZE_BITS, '0').also { binaryString ->
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.ViewGroup
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DimenRes
import androidx.annotation.Dimension
import androidx.annotation.Px
import androidx.core.content.ContextCompat
@ElianFabian
ElianFabian / AsyncResource.kt
Last active September 30, 2024 19:26
Network utilities for Android.
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.sync.*
interface AsyncResource<out T> {
sealed interface State<out T> {
data object NotStarted : State<Nothing>
data class Loading<out T>(val currentResource: Completed<T>?) : State<T>
sealed interface Completed<out T> : State<T> {