Skip to content

Instantly share code, notes, and snippets.

@rharter
rharter / Scoped.kt
Last active August 9, 2022 14:58
A kotlin property delegate to scope any object to an android lifecycle. Blog post at https://ryanharter.com/blog/easy-android-scopes/
import androidx.lifecycle.get
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
/**
* Returns a property delegate to access the wrapped value, which will be retained for the
* duration of the lifecycle of this [ViewModelStoreOwner].
*
@rharter
rharter / StoreRepository.kt
Created August 19, 2020 14:50
Extension function to convert a store, which emits a stream of **events**, to a repository which emits a stream of **states**. This hides implementation details, like which datasource is providing the data, from consumers.
sealed class Resource<T> {
data class Loading<T>(val data: T? = null, val message: Int? = null) : Resource<T>()
data class Success<T>(val data: T) : Resource<T>()
data class Error<T>(val throwable: Throwable?, val data: T? = null) : Resource<T>()
}
fun <T> Resource<T>.dataOrNull(): T? = when (this) {
is Resource.Loading -> data
is Resource.Success -> data
is Resource.Error -> data
@rharter
rharter / make_launcher.sh
Created September 23, 2020 16:08
Using this script you can create a tiny macOS app that will always launch the latest version of any JetBrains Toolbox installed IDE. Simply pass the target IDE application as the first argument and the script will create a launcher that you can add to your dock that will always point to the latest installed version of the IDE.
#!/bin/bash
APP_DIR=$1
APP_SCRIPT_NAME="$(ls "${APP_DIR}/Contents/MacOS/")"
APP_FILE_NAME="$(basename "${APP_DIR}")"
CHANNEL_DIR="$(dirname "$(dirname "$1")")"
CHANNEL_NAME="$(basename "$CHANNEL_DIR")"
TOOLBOX_APP_NAME="$(basename "$(dirname "$CHANNEL_DIR")")"
@rharter
rharter / RoundedRect.kt
Created September 20, 2022 03:17
A simple demonstration of rectangles with extra large corner radii. From https://ryanharter.com/blog/2022/09/rounded-corners/
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.Slider
import androidx.compose.material.Surface
import androidx.compose.material.Text