Skip to content

Instantly share code, notes, and snippets.

Avatar

Darren Mason darnmason

  • Seventh Beam
  • Melbourne
View GitHub Profile
@darnmason
darnmason / OkHttpCachingTest
Created Jun 18, 2018
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.
View OkHttpCachingTest
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 Mar 20, 2018
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.
View CircularShadowView.kt
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 Mar 25, 2020
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.
View SwipeView.kt
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 Sep 4, 2017
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.
View AnimationUtils.java
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 Sep 7, 2021
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…
View MultiSelectItemAnimator.java
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 Jun 7, 2021
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
View CircleTextSpan.java
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 Mar 21, 2020
RecyclerView adapter designed to wrap an existing adapter allowing the addition of header views and footer views.
View HeaderViewRecyclerAdapter.java
/*
* 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