Skip to content

Instantly share code, notes, and snippets.

View L-Briand's full-sized avatar

Lionel Briand L-Briand

  • Orandja
  • Bordeaux
View GitHub Profile
@L-Briand
L-Briand / roboto.xml
Last active March 14, 2018 16:16
Roboto android xml font-family
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:font="@font/roboto_thin"
app:fontStyle="normal"
app:fontWeight="100" />
<font
app:font="@font/roboto_thinitalic"
app:fontStyle="italic"
app:fontWeight="100" />
<font
@L-Briand
L-Briand / SizedView.kt
Last active December 9, 2019 20:20
A simple class to extend when creating a custom view that has some width and height requirement.
import android.content.Context
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import androidx.annotation.Nullable
import kotlin.math.min
abstract class SizedView @JvmOverloads constructor(
context: Context,
@Nullable attrs: AttributeSet? = null,
@L-Briand
L-Briand / IsLeftToRight.kt
Created July 18, 2021 09:10
Android View.isLeftToRight()
val View.isLeftToRight: Boolean
@SuppressLint("SwitchIntDef")
get() = when (ViewCompat.getLayoutDirection(this)) {
View.LAYOUT_DIRECTION_LTR -> true
View.LAYOUT_DIRECTION_RTL -> false
View.LAYOUT_DIRECTION_INHERIT -> (parent as? View)?.isLeftToRight ?: true
View.LAYOUT_DIRECTION_LOCALE -> TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) == View.LAYOUT_DIRECTION_LTR
else -> throw IllegalStateException()
}
@L-Briand
L-Briand / build_vdtool.sh
Last active August 9, 2021 19:24 — forked from michpohl/build_vdtool.sh
Bash script to build vd-tool from Android sources to batch convert SVG to VectorDrawables via terminal
#!/bin/bash
# inspiration for this script comes from here: https://www.androiddesignpatterns.com/2018/11/android-studio-svg-to-vector-cli.html
DESIRED_PATH="$1"
if [ -z "$1" ]
then
echo "No path provided.Please specify a path to clone the source files into."
echo -e "\nUsage: $ ./build_vdtool.sh [my-desired-path]\n"
echo "It is suggested to place these files outside of your dbx source in order to not mess up the project files"
@L-Briand
L-Briand / ThemeResolver.kt
Created February 18, 2022 16:28
Android: Resolve drawable theme attribute from context
inline fun <reified T> TypedArray.autoRecycle(block: TypedArray.() -> T): T {
val result = block()
recycle()
return result
}
fun resolveDrawable(context: Context, @AttrRes attr: Int): Drawable? =
context.theme.obtainStyledAttributes(intArrayOf(attr)).autoRecycle { getDrawable(0) }
@L-Briand
L-Briand / ComposeActivityViewModel.kt
Created June 1, 2022 08:21
Get an Activity ViewModel in compose
/** Try to fetch a viewModel in [store] */
@Composable
inline fun <reified T : ViewModel, S : ViewModelStoreOwner> viewModelInStore(store: S): Result<T> =
runCatching {
var result: Result<T>? = null
CompositionLocalProvider(LocalViewModelStoreOwner provides store) {
result = runCatching { viewModel(T::class.java) }
}
result!!.getOrThrow()
private inline fun <R> ByteArray.reduce(
offset: Int, size: Int, default: R,
reducer: (acc: R, element: Byte, offsetIdx: Int) -> R
): R {
var acc = default
val upperBound = offset + size
if (0 > offset && upperBound >= size) throw RuntimeException("Selected range not in bound. ${offset until upperBound} !in $indices")
for (i in offset until upperBound) {
acc = reducer(acc, get(i), i - offset)
@L-Briand
L-Briand / Either.kt
Last active May 17, 2023 07:17
Utility Optionated classes
sealed class Either<out L, out R> {
abstract val left: L
abstract val right: R
abstract fun invert(): Either<R, L>
}
class Left<out L>(override val left: L) : Either<L, Nothing>() {
companion object {
@JvmStatic
inline fun <reified T : Enum<T>> enumGetBy(default: T, predicate: (T) -> Boolean): T =
enumGetByOrNull(predicate) ?: default
inline fun <reified T : Enum<T>> enumGetByOrNull(predicate: (T) -> Boolean): T? =
enumValues<T>().find(predicate)
inline fun <reified T : Enum<T>> enumGetByName(name: String, default: T): T =
enumGetBy(default) { it.name == name }
inline fun <reified T : Enum<T>> enumGetByNameOrNull(name: String): T? =
@L-Briand
L-Briand / #Observable.kt
Last active May 4, 2023 13:33
Observable pattern with kotlinx coroutine
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ClosedReceiveChannelException
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import java.lang.ref.WeakReference