Skip to content

Instantly share code, notes, and snippets.

View benvium's full-sized avatar

Ben Clayton benvium

  • www.calvium.com
  • Bristol, UK
View GitHub Profile
@benvium
benvium / search-array.swift
Last active March 29, 2024 22:29
searching arrays in apple swift. Search for matching strings and strings containing other strings.
class Person {
var name = ""
var age = 0
init(name: String, age:Int) {
self.name = name
self.age = age
}
}
@benvium
benvium / simple-sorting
Created August 30, 2014 11:25
Simple sorting in Swift. Sort array of int, array of string, and by a property in instances of a class
// sort int
var ar = [3,2,5,6]
sort(&ar, <)
// or
ar = ar.sorted(<)
print(ar);
@benvium
benvium / retrofit-custom-error-handling.java
Created August 29, 2014 19:45
Fairly simply Retrofit custom error handling example. Is set up so that you don't need to do much work in the 'failure' handler of a retrofit call to get the user-visible error message to show. Works on all endpoints. There's lots of exception handling as our server folks like to keep us on our toes by sending all kinds of random stuff..!
// on error the server sends JSON
/*
{ "error": { "data": { "message":"A thing went wrong" } } }
*/
// create model classes..
public class ErrorResponse {
Error error;
@benvium
benvium / RRDrawable.java
Created August 29, 2014 19:25
Programatically create a drawable with rounded corners
public class RRDrawable extends Drawable {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
public RRDrawable(int color) {
paint.setColor(color);
paint.setStyle(Paint.Style.FILL);
}
@Override
public void draw(Canvas canvas) {
@benvium
benvium / TextViewUtils.java
Created August 29, 2014 10:49
Remove underlines from TextView links. Based on stackoverflow code.
package com.util.util;
import android.text.Spannable;
import android.text.TextPaint;
import android.text.style.URLSpan;
import android.widget.TextView;
public class TextViewUtils {
private static final String TAG = TextViewUtils.class.getSimpleName();
@benvium
benvium / proguard-rules.txt
Last active January 5, 2018 07:57
Android proguard file to remove log messages. This is tested and actually works with Android Studio 0.8.8 You need to add some lines to the build.gradle file (see notes below) Includes library-specific rules for Guava, Crashlytics, OkHttp, Retrofit, Otto
######################################################
# Proguard file to remove debug logs and NOT kill the application
#
# @benclayton github.com/benvium 15-12-2014
#
# https://gist.github.com/benvium/8995326bc944f47f2c64
######################################################
# To use this file, your project's build.gradle 'buildTypes' section should look like this:
#
@benvium
benvium / 0_reuse_code.js
Created August 21, 2014 11:01
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@benvium
benvium / TopCropImageView.java
Created January 23, 2014 13:18
Android ImageView subclass that (1) fills the image to the width of the image view (2) aligns to the bottom edge (3) crops off the top
/**
* Align image to bottom, fill width. and crop top if needed.
*
* http://stackoverflow.com/questions/6330084/imageview-scaling-top-crop
* https://gist.github.com/arriolac/3843346
*/
import android.content.Context;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.widget.ImageView;
@benvium
benvium / resizeImagesFromXHDPI.sh
Last active October 19, 2016 09:49
Android app script for Android Studio / Android SDK. This script resizes android xhdpi-sized png image assets into the ldpi, mdpi, hdpi, xhdpi folders. Now just maintain one folder of images rather than 4 or 5. Updated to create directories if needed and also to separately convert images in the mipmap folder (used for app icons)
#!/bin/bash -e
# Ensure we're running in location of script.
cd "`dirname $0`"
function fail {
echo "FAILED with $1"
exit 1
}
@benvium
benvium / VideoEnabledWebChromeClient.java
Created July 2, 2013 08:09
Playing HTML5 video on fullscreen in android webview. Note: I did not write this, it has been taken from http://stackoverflow.com/questions/15768837/playing-html5-video-on-fullscreen-in-android-webview
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebChromeClient;
import android.widget.FrameLayout;
import android.widget.VideoView;