Skip to content

Instantly share code, notes, and snippets.

View darnmason's full-sized avatar

Darren Mason darnmason

  • Seventh Beam
  • Melbourne
View GitHub Profile
@darnmason
darnmason / OkHttpCachingTest
Created June 18, 2018 14:08
Demonstration of caching issue where OkHttpClient doesn't check either the modified date or the Etag on subsequent requests, it just serves from the cache. Comment out the last-modified header and the test passes as the Etag is checked.
import junit.framework.Assert;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
@darnmason
darnmason / CircularShadowView.kt
Created March 20, 2018 23:44
A custom Android view to render a circular shadow. The radius of the gradient intersects both edges of the view and the startColor stop is the first visible point of the gradient. Customisable depth and gravity via xml.
import android.content.Context
import android.graphics.Color
import android.graphics.RadialGradient
import android.graphics.Shader
import android.graphics.drawable.PaintDrawable
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.RectShape
import android.util.AttributeSet
import android.view.Gravity.*
import android.widget.FrameLayout
@darnmason
darnmason / SwipeView.kt
Last active February 19, 2024 17:10
A custom Android ViewGroup that contains a single child and allows you to swipe it to the left, with a callback once the swipe is complete. For example to dismiss a view. Works well in a LinearLayout with animateLayoutChanges=true when setting the SwipeView visibility to GONE on swipe.
import android.content.Context
import android.support.v4.view.ViewCompat
import android.support.v4.widget.ViewDragHelper
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.FrameLayout
class SwipeView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr) {
@darnmason
darnmason / AnimationUtils.java
Last active September 4, 2017 10:48
Animation utility for showing and hiding a View with a slide animation, used for Views that are anchored to the bottom of the screen overlaying the main content, usually confirmation buttons.
public class AnimationUtils {
private static final int SLIDE_DURATION = 150;
private static final int FADE_DURATION = 100;
private static final ArrayMap<View, ObjectAnimator> showing = new ArrayMap<>();
private static final ArrayMap<View, ObjectAnimator> hiding = new ArrayMap<>();
private AnimationUtils() {
}
@darnmason
darnmason / MultiSelectItemAnimator.java
Last active September 7, 2021 09:37
ItemAnimator for RecyclerView that animates changes between 2 states, edit and standard which is useful for multi select using check boxes. In edit mode a checkBox slides in from the left and an icon slides off screen to the right. In between an avatar and name view must adjust their positions. All 4 views are in a horizontal LinearLayout with w…
public class MultiSelectItemAnimator extends DefaultItemAnimator {
private static final long DURATION = 150;
private final ArrayMap<RecyclerView.ViewHolder, AnimatorInfo> animatorMap = new ArrayMap<>();
private final Animator.AnimatorListener resetListener = new ViewObjectAnimatorListener() {
@Override
public void onAnimationEnd(Animator animation, View view) {
view.setTranslationX(0f);
}
@darnmason
darnmason / CircleTextSpan.java
Last active June 7, 2021 11:17
A span with a circle background and different color text, intended for one or 2 characters as more will affect the height of the span
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.style.ReplacementSpan;
public class CircleTextSpan extends ReplacementSpan {
private final int backgroundColor;
private final int textColor;
@darnmason
darnmason / HeaderViewRecyclerAdapter.java
Last active March 21, 2020 17:19
RecyclerView adapter designed to wrap an existing adapter allowing the addition of header views and footer views.
/*
* Copyright (C) 2014 darnmason
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software