Skip to content

Instantly share code, notes, and snippets.

View timfreiheit's full-sized avatar

Tim Freiheit timfreiheit

  • Smart Mobile Factory
  • Berlin, Germany
View GitHub Profile
@timfreiheit
timfreiheit / MultiTypeDiffItemCallback.kt
Created June 14, 2019 11:17
MultiTypeDiffItemCallback
class MultiTypeDiffItemCallback<T: Any> : DiffUtil.ItemCallback<T>() {
private val itemCallbacks = mutableMapOf<Any, DiffUtil.ItemCallback<T>>()
fun <Subclass: T> register(clazz: Class<Subclass>, callback: DiffUtil.ItemCallback<Subclass>) {
@Suppress("UNCHECKED_CAST")
itemCallbacks[clazz] = callback as DiffUtil.ItemCallback<T>
}
override fun areItemsTheSame(oldItem: T, newItem: T): Boolean {
@timfreiheit
timfreiheit / DynamicIntending.kt
Created February 6, 2018 13:08
DynamicIntending.kt
import android.app.Activity
import android.app.Instrumentation.ActivityResult
import android.content.Intent
import android.support.test.espresso.intent.Intents
import android.support.test.espresso.intent.Intents.intending
import android.support.test.espresso.matcher.ViewMatchers.assertThat
import org.hamcrest.CoreMatchers.allOf
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.StringDescription
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import com.f2prateek.rx.preferences.Preference;
/**
* Caches an result in memory to avoid fetching and parsing it for every call
* IMPORTANT: the value will not update automatically if you change the preferences in another way than using
* {@link Preference#set(Object)}
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
@timfreiheit
timfreiheit / GeoCoderWrapper.java
Created July 28, 2015 12:09
GeoCoderWrapper in case that the normal GeoCoder is not present !GeoCoder.isPresent()
/*
* uses MyGeoCoder if !android.location.Geocoder.isPresent()
*/
public class GeoCoderWrapper {
Geocoder geo;
public GeoCoderWrapper(Context context){
if(Geocoder.isPresent()){
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
/**
* unified wrapper of {@link Log} to make logging more consistent throughout the app
*
* automatically logs called filename, method and linenumber
* if you are not called the methods with a custom tag
*
* D/FILENAME﹕ [METHOD: LINENUMBER] MSG
*
* Created by timfreiheit on 26.07.15.
*/
@timfreiheit
timfreiheit / JSPromise
Last active October 23, 2022 12:26
Android JavascriptInterface Promise
import android.os.Build;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import com.google.gson.Gson;
import java.util.concurrent.Callable;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public abstract class HeaderFooterRecyclerViewAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
private static final int VIEW_TYPE_MAX_COUNT = 1000;
private static final int HEADER_VIEW_TYPE_OFFSET = 0;
private static final int FOOTER_VIEW_TYPE_OFFSET = HEADER_VIEW_TYPE_OFFSET + VIEW_TYPE_MAX_COUNT;
private static final int CONTENT_VIEW_TYPE_OFFSET = FOOTER_VIEW_TYPE_OFFSET + VIEW_TYPE_MAX_COUNT;
private int headerItemCount;
private int contentItemCount;
private int footerItemCount;