Skip to content

Instantly share code, notes, and snippets.

View saket's full-sized avatar

Saket Narayan saket

View GitHub Profile
@saket
saket / mirroringLifecycleOwner.kt
Created December 27, 2023 21:27
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.Immutable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.compose.currentStateAsState
@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 / 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 / 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 / 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) {
package me.saket.dank.widgets;
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
@saket
saket / GlidePaddingTransformation.java
Last active July 20, 2017 05:27
Adds empty spaces to inflate the height of images
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapResource;