Skip to content

Instantly share code, notes, and snippets.

View dylangrijalva's full-sized avatar
🏆
Chill

Dylan Grijalva dylangrijalva

🏆
Chill
  • Managua, Nicaragua
  • 08:27 (UTC -06:00)
View GitHub Profile
@dylangrijalva
dylangrijalva / AndroidNetworkModule.kt
Last active March 20, 2025 21:17
Helper classes to get the network state using built-in android APIs.
class AndroidNetworkModule(
private val context: Context,
private val dispatchers: CoroutineDispatchers,
) : NetworkModule {
private val connectivityManager: ConnectivityManager
get() = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
override fun stream(): Flow<NetworkState> {
return callbackFlow {
val callback = object : ConnectivityManager.NetworkCallback() {
@dylangrijalva
dylangrijalva / BounceEdgeEffectFactory.kt
Last active January 1, 2025 15:56
This code snippet defines a custom "Bounce Edge Effect" for a RecyclerView in Android, replacing the default edge glow effect with a bounce animation.
import android.graphics.Canvas
import android.widget.EdgeEffect
import androidx.dynamicanimation.animation.SpringAnimation
import androidx.dynamicanimation.animation.SpringForce
import androidx.recyclerview.widget.RecyclerView
/** The magnitude of translation distance while the list is over-scrolled. */
private const val OVERSCROLL_TRANSLATION_MAGNITUDE = 0.5f
/** The magnitude of translation distance when the list reaches the edge on fling. */
@dylangrijalva
dylangrijalva / colors.xml
Created November 18, 2024 15:48
Android XML Geist Colors
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="geist_light_background_color_1">#FFFFFF</color>
<color name="geist_light_background_color_2">#FAFAFA</color>
<color name="geist_light_gray_100">#F2F2F2</color>
<color name="geist_light_gray_200">#EBEBEB</color>
<color name="geist_light_gray_300">#E5E5E5</color>
<color name="geist_light_gray_400">#EBEBEB</color>
<color name="geist_light_gray_500">#C9C9C9</color>
<color name="geist_light_gray_600">#A8A8A8</color>
@dylangrijalva
dylangrijalva / colors.xml
Created March 9, 2024 03:19
Vercel Geist Design System Colors
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
<color name="dark_background_1">#0A0A0A</color>
<color name="dark_background_2">#000000</color>
<color name="dark_gray_100">#1A1A1A</color>
<color name="dark_gray_200">#1F1F1F</color>
<color name="dark_gray_300">#292929</color>
<color name="dark_gray_400">#2E2E2E</color>
@dylangrijalva
dylangrijalva / GoogleSignInHandler.kt
Created March 15, 2023 05:11
One Tap Sign In Handler
class GoogleSignInHandler(fragment: Fragment) {
private val oneTapClient = Identity.getSignInClient(fragment.requireContext())
private var callback: ActivityResultCallback<ActivityResult> = ActivityResultCallback {
fragment.toast("Hello!")
}
private val launcher: ActivityResultLauncher<IntentSenderRequest> =
fragment.registerForActivityResult(
ActivityResultContracts.StartIntentSenderForResult(),
callback
@dylangrijalva
dylangrijalva / Result.kt
Created January 9, 2023 19:20
Either like type in kotlin for handling errors
sealed class Result<out R> {
data class Success<out T>(val data: T) : Result<T>()
data class Error(val message: String, val errorCode: ErrorCode = ErrorCode.UNKNOWN) :
Result<Nothing>() {
companion object {
private const val DEFAULT_ERROR_MESSAGE = "An unknown error has occurred"
fun fromException(exception: Exception): Error {
return Error(exception.message ?: DEFAULT_ERROR_MESSAGE, ErrorCode.UNKNOWN)
@dylangrijalva
dylangrijalva / Option.kt
Created January 9, 2023 19:19
Option<A> is a container for an optional value of type A. If the value of type A is present, the Option<A> is an instance of Some<A>, containing the present value of type A. If the value is absent, the Option<A> is an instance of None.
sealed interface Option<out A> {
data class Some<out B>(val value: B) : Option<B>
object None : Option<Nothing>
}
fun <A> some(value: A): Option<A> {
return Option.Some(value)
}
@dylangrijalva
dylangrijalva / StateExtensions.kt
Created October 21, 2022 13:57
Creates a queryable state which allows to reuse filter logic in a state with any type of list
typealias QueryPredicate<T> = (String, T) -> Boolean
data class QueryableState<T>(
private val filteredState: StateFlow<List<T>>,
val setQuery: (String) -> Unit
) {
fun state() = filteredState
}
fun <T> ViewModel.createQueryableState(
@dylangrijalva
dylangrijalva / alacritty.tml
Created September 27, 2022 08:40
Alacritty configuration
window:
dimensions:
columns: 100
lines: 30
color:
import:
- ~/.alacritty-colorscheme/themes/atom_one_light.yaml
font:
normal:
family: Cascadia Code
@dylangrijalva
dylangrijalva / Dialog.kt
Last active September 27, 2022 08:39
Creates a dialog which allows to set a custom width and height.
fun createDialog(context: Context, contentView: View): Dialog {
return Dialog(context, R.style.App.Theme.Dialog).apply {
requestWindowFeature(Window.FEATURE_NO_TITLE)
setContentView(contentView)
val layoutParams = WindowManager.LayoutParams()
layoutParams.copyFrom(window?.attributes)
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT