Skip to content

Instantly share code, notes, and snippets.

View nwellis's full-sized avatar

nwellis nwellis

  • Minneapolis, MN
View GitHub Profile
@nwellis
nwellis / ApiCallback.java
Created December 30, 2016 20:27
Helper callback class that builds on top of Retrofit 2's callback interface
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* <p>
* Builds upon the Retrofit {@link Callback} class. Useful when each HTTP code group has
* specific functionality. This lacks some of the flexibility of {@link Callback} though.
* </p>
*/
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.annotation.ColorRes;
import android.support.annotation.IdRes;
import android.support.v4.content.ContextCompat;
@nwellis
nwellis / RetroCallStack.java
Created December 30, 2016 21:42
Simple helper class to monitor the Retrofit 2 calls
import android.support.annotation.Nullable;
import java.util.Stack;
import retrofit2.Call;
/**
* Simple helper class to monitor the Retrofit 2 calls. Biggest use case is to cancel all pending calls.
*/
public class RetroCallStack {
{"lastUpload":"2021-08-07T13:12:16.241Z","extensionVersion":"v3.4.3"}
@nwellis
nwellis / GsonBundler.java
Last active May 13, 2021 12:59
Custom bundler for Icepick which saves objects via GSON
public abstract class GsonBundler<T> implements Bundler<T> {
//Maximum bytes this fragment is allowed to save, os limit is 1 Mb
public static final int MAX_BUNDLE_BYTE_SIZE = 93750; //93750 bytes == .75 Mb
abstract Class<T> getObjClass();
@Override public void put(String s, T obj, Bundle bundle) {
putReturnSize(s, obj, bundle);
}
abstract class RealmObjBundler<T extends RealmObject> implements Bundler<T> {
abstract public String[] args(T obj);
@NonNull abstract public RealmQuery<T> query(Realm realm, String... args);
@Override public void put(String s, final T obj, Bundle bundle) {
if (obj == null) return;
ArrayList<String> args = new ArrayList<>();
// We need to know whether this was managed before to determine if we need to delete it afterwards
abstract class UiComponent(@LayoutRes val layoutId: Int,
val root: ViewGroup,
val activity: FragmentActivity)
{
val mainView: View = View.inflate(activity, layoutId, root)
lateinit protected var unbinder: Unbinder
open fun unbind() {
@nwellis
nwellis / SquareCutoutOverlay.kt
Created January 24, 2019 15:33
View to add a semitransparent overlay with a fully transparent square cutout
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import kotlin.math.max
import kotlin.math.min
class SquareCutoutOverlay(
context: Context,
@nwellis
nwellis / Resource.kt
Last active December 8, 2020 20:54
Resource wrappers in Kotlin and Typescript
sealed class Resource<T> {
object Loading: Resource<Nothing>()
data class Success<T>(val data: T): Resource<T>()
data class Error(val error: AppError): Resource<Nothing>()
}
import { useMemo, useEffect, useState } from 'react';
import { useDebounce } from 'use-debounce';
export default function(debounceWaitMs = 500) {
const [curWindowHeightPx, setWindowHeight] = useState(window.innerHeight);
const [windowHeightPx] = useDebounce(curWindowHeightPx, debounceWaitMs, {
leading: true
});
useEffect(() => {