Skip to content

Instantly share code, notes, and snippets.

View saket's full-sized avatar

Saket Narayan saket

View GitHub Profile
import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Parcel
import android.os.Parcelable
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.LocalSaveableStateRegistry
data class BlurHashModel(
val hash: String,
val width: Int,
val height: Int,
)
@saket
saket / mirroringLifecycleOwner.kt
Last active November 19, 2024 15:11
A `LifecycleOwner` that can switch between mirroring of its parent's state and an overridden state
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.remember
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.compose.LocalLifecycleOwner
@saket
saket / AnimatedVideoFrameDecoder.kt
Last active September 4, 2023 19:01
A Coil decoder for generating animated previews of videos
import android.graphics.drawable.AnimationDrawable
import coil.ImageLoader
import coil.decode.DecodeResult
import coil.decode.Decoder
import coil.decode.VideoFrameDecoder
import coil.fetch.SourceResult
import coil.request.Options
import coil.request.Parameters
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
@saket
saket / Present.swift
Last active July 30, 2023 16:25
SwiftUI with Reaktive presenters written with Kotlin Multiplatform
import SwiftUI
import Combine
import Common // Code shared through Kotlin Multiplaform.
import CombineExt // https://github.com/CombineCommunity/CombineExt
/// A convenience pass-through View to hide away the verbosity
/// of subscribing to a presenter's stream. Usage:
///
/// struct FooView: View {
/// let presenter: FooPresenter
@saket
saket / maven_terminology.md
Last active February 6, 2021 00:18
Maven terminology

Maven: a build automation tool maintained by Apache

Maven repository: a host for maven artifacts

Maven Central: the biggest host, maintained by Sonatype


JFrog: a company

@saket
saket / RxPreferencesEnumTypeAdapter.java
Last active May 22, 2018 07:27
A generic enum type converter for RxPreferences library
import com.f2prateek.rx.preferences2.Preference;
public class RxPreferencesEnumTypeAdapter<T extends Enum<T>> implements Preference.Converter<T> {
private final Class<T> enumClass;
public RxPreferencesEnumTypeAdapter(Class<T> enumClass) {
this.enumClass = enumClass;
}
@saket
saket / StoreLruFileSystem.java
Created March 18, 2018 04:35
A FileSystem for NYT-Store that uses DiskLruCache
import com.jakewharton.disklrucache.DiskLruCache;
import com.nytimes.android.external.fs3.filesystem.FileSystem;
import com.nytimes.android.external.store3.base.RecordState;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
@saket
saket / Pair.java
Last active February 3, 2018 08:34
import android.support.annotation.Nullable;
import com.google.auto.value.AutoValue;
/**
* Copied from {@link android.support.v4.util.Pair} to remove all @Nullable annotations.
* <p>
* Container to ease passing around a tuple of two objects.
*/
public abstract class Pair<F, S> {
private void foo() {
String imageUrl = "https://i1.wp.com/saket.me/wp-content/uploads/2017/08/notification-progress-issue-2.png?w=1200";
Single
.<Drawable>create(emitter -> {
Glide.with(this)
.load(imageUrl)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {