Skip to content

Instantly share code, notes, and snippets.

View gtomek's full-sized avatar

Tomek Giszczak gtomek

  • Zurich, CH
View GitHub Profile
@gtomek
gtomek / IoUtils.java
Last active October 20, 2015 15:41
IoUtils helper methods
import android.content.Context;
import com.squareup.okhttp.internal.io.FileSystem;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import okio.BufferedSource;
import okio.Okio;
@gtomek
gtomek / GetOkHttpCache.java
Last active October 20, 2015 15:43
Get saved OkHttp cache
/**
* Returns response cached by OkHttp.
*
* @param url URL used to make original request
*/
@Nullable
private List<PostModel> getCachedDataIfExist(final String url) {
// OkHttpClient saves cache using DiskLruCache, the cached data is stored in the cache dir in the files
// which names are created by md5 hash function, e.g. for
// http://www.yourserver.com/posts?filter[posts_per_page]=10&page=1
@gtomek
gtomek / Truss.java
Created December 17, 2015 10:10 — forked from JakeWharton/Truss.java
Extremely simple wrapper around SpannableStringBuilder to make the API more logical and less awful. Apache 2 licensed.
import android.text.SpannableStringBuilder;
import java.util.ArrayDeque;
import java.util.Deque;
import static android.text.Spanned.SPAN_INCLUSIVE_EXCLUSIVE;
/** A {@link SpannableStringBuilder} wrapper whose API doesn't make me want to stab my eyes out. */
public class Truss {
private final SpannableStringBuilder builder;
private final Deque<Span> stack;
public class DeviceDimensionsHelper {
// DeviceDimensionsHelper.getDisplayWidth(context) => (display width in pixels)
public static int getDisplayWidth(Context context) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return displayMetrics.widthPixels;
}
// DeviceDimensionsHelper.getDisplayHeight(context) => (display height in pixels)
public static int getDisplayHeight(Context context) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
@gtomek
gtomek / CurlLoggingInterceptor.java
Created January 17, 2016 19:46 — forked from jgilfelt/CurlLoggingInterceptor.java
An OkHttp interceptor that logs requests as curl shell commands
/*
* Copyright (C) 2016 Jeff Gilfelt.
*
* 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
@gtomek
gtomek / FontFaceUtils.java
Created February 15, 2016 14:14
Andorid fonts face utils
/**
* Utility class for fonts manipulations.
*/
public final class FontFaceUtils {
public static final LruCache<String, Typeface> sCache = new LruCache<>(6);
private FontFaceUtils() {
}
@gtomek
gtomek / RxErrorHandler.java
Created October 28, 2016 13:16
Rx custom error handling (stack level default=4)
@NonNull private static Action1<Throwable> handleError(@NonNull final Context context,
final boolean isExpected, int stackLevel) {
final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
final StackTraceElement stackTraceElement = stackTrace[stackLevel];
final String identifyingString =
String.format(Locale.getDefault(), "%s : %s (%d)", stackTraceElement.getClassName(),
stackTraceElement.getMethodName(), stackTraceElement.getLineNumber());
return new Action1<Throwable>() {
@Override public void call(Throwable throwable) {
@gtomek
gtomek / StringSharedPreference.java
Created March 28, 2017 14:31
String shared preference for android
import android.content.SharedPreferences;
/**
* Stores {@link String} value in {@link SharedPreferences}.
*/
public class StringPreference {
private final SharedPreferences mPreferences;
private final String mKey;
private final String mDefaultValue;