Skip to content

Instantly share code, notes, and snippets.

View nimaiwalsh's full-sized avatar

Nimai Walsh nimaiwalsh

View GitHub Profile
@nimaiwalsh
nimaiwalsh / kotlin-notes.md
Last active August 8, 2024 23:07
kotlin-notes

Notes from Effective Kotlin: Best Practice

Safety

Limit mutability

The general rule is that one should not create unnecessary ways to mutate a state. Every way to mutate a state is a cost. Every mutation point needs to be understood and maintained. We prefer to limit mutability. General rules:

  • Prefer val over var.
@nimaiwalsh
nimaiwalsh / ActivityExt.kt
Created January 9, 2024 03:28
Android bind view extension function
fun <T : View> Activity.bindView(viewId: Int) =
lazy { this.findViewById<T>(viewId) }
@nimaiwalsh
nimaiwalsh / Modifiers.kt
Last active August 29, 2023 04:45
Composable helper extensions
/**
* Modifies a layout to ignore irs parents horizontal padding. This is useful if you have an item in a column, such as
* a divider, that does not want to be constrained by the parents padding
*/
fun Modifier.ignoreHorizontalParentPadding(horizontal: Dp): Modifier {
return this.layout { measurable, constraints ->
val overriddenWidth = constraints.maxWidth + 2 * horizontal.roundToPx()
val placeable = measurable.measure(constraints.copy(maxWidth = overriddenWidth))
layout(placeable.width, placeable.height) {
placeable.place(0, 0)
@nimaiwalsh
nimaiwalsh / git_commands.bash
Last active May 11, 2023 05:02
GiT commands
# Remove directories and files that aren’t in git from your own local working copy you can do so by running
git clean -d -f -x
@nimaiwalsh
nimaiwalsh / DateFormatter.kt
Last active July 6, 2022 04:22
DateFormatter - Custom date and time formatter implementing custom formatting rules
import android.content.Context
import java.time.temporal.ChronoField.DAY_OF_YEAR
import java.time.temporal.ChronoField.INSTANT_SECONDS
import java.time.temporal.ChronoField.YEAR
import java.time.temporal.TemporalAccessor
import java.util.Locale
import javax.inject.Inject
/**
* Custom date and time formatter implementing custom formatting rules.
@nimaiwalsh
nimaiwalsh / AndroidStringsProvider.kt
Last active July 6, 2022 04:23
Android ViewModel StringProvider. Used to provide strings in a ViewModel without injecting context into the ViewModel.
import android.content.Context
import androidx.annotation.PluralsRes
import androidx.annotation.StringRes
/** Provides access to localized strings and text using the resources from the application context. */
class AndroidStringsProvider @Inject constructor(
private val context: Context
): StringsProvider {
override fun get(@StringRes resId: Int): String {
@nimaiwalsh
nimaiwalsh / ViewExtensions.kt
Created August 12, 2021 23:27
Safe delay in Android Views with Coroutines
/**
*
* Safely run some delayed tasks in a view as they will be tied to the lifecycle.
*
* @param durationInMillis delay task in millisecons
* @param dispatcher CoroutineDispatcher, defaults to Dispatchers.Main as view work is done on Main thread
* @param block the block of work to be done
*
* example:
*