Skip to content

Instantly share code, notes, and snippets.

@Artur-Sulej
Artur-Sulej / HTTP_Error
Created August 4, 2015 10:54
Convenient function for creating http errors in JavaScript.
function HTTP_Error(msg, status) {
var error = new Error(msg);
error.status = status;
return error;
}
@Artur-Sulej
Artur-Sulej / ListStorage.java
Last active December 3, 2015 01:22
Generic classes for storing objects and list of objects serialized to JSON in Android.
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
@Artur-Sulej
Artur-Sulej / ListViewAdapter.java
Last active March 30, 2016 13:14
Generic adapter for feeding ListViews with custom view loaded with data.
public abstract class ListViewAdapter <T> extends BaseAdapter {
protected ListView listView;
private ArrayList<T> objects = new ArrayList<T>();
public void updateData(ArrayList<T> objects) {
this.objects = objects;
notifyDataSetChanged();
}
@Artur-Sulej
Artur-Sulej / layout.xml
Created November 24, 2015 23:31
Attributes of a listView making its dividers disappear.
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animationCache="false"
android:cacheColorHint="@color/transparent"
android:divider="@color/transparent"
android:dividerHeight="0dp"
android:listSelector="@color/transparent"
android:scrollingCache="false"/>
@Artur-Sulej
Artur-Sulej / HttpStatus.java
Created November 24, 2015 23:55
RequestCallback - a subclass of Retrofit's Callback class. Makes things simpler by taking care of showing and hiding progress view (for example animated spinner) and providing higher level of abstraction with its callbacks.
public enum HttpStatus {
UNAUTHORIZED(401),
FORBIDDEN(403),
NOT_FOUND(404),
OTHER_ERROR(null);
private static HashMap<Integer, HttpStatus> map;
private Integer code;
@Artur-Sulej
Artur-Sulej / GcmUtil.java
Created November 29, 2015 12:55
Utility class which allows you to synchronously get GCM token on Android.
public class GcmUtil {
public static String getToken(Context context) throws IOException {
InstanceID instanceID = InstanceID.getInstance(context);
return instanceID.getToken(context.getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
}
public static String getTokenOnThread(final Context context) {
try {
@Artur-Sulej
Artur-Sulej / index.html
Created November 30, 2015 11:18
CSS class making image fit screen width
<style type="text/css">
.img_fitted {
background-position: center;
background-size:cover;
width: 100%;
color: #fff;
}
.img_fitted img {
max-width: 100%;
@Artur-Sulej
Artur-Sulej / UniqueIdUtility.java
Last active December 3, 2015 01:22
Util class providing unique IDs - dead simple but helpful in case of for example PendingIntents in notifications.
import java.util.Random;
public class UniqueIdUtility {
static private int id;
static {
id = Integer.MIN_VALUE + new Random().nextInt(Integer.MAX_VALUE);
}
@Artur-Sulej
Artur-Sulej / logStackTrace.java
Created December 2, 2015 15:56
Probably best way to print stacktrace.
protected void logStackTrace() {
(new Throwable()).printStackTrace();
}
@Artur-Sulej
Artur-Sulej / facebook_login_helper.rb
Created May 11, 2016 09:26
Module for signing in users with Facebook. It uses Koala gem to access Graph API. It is meant to be used with devise_token_auth gem. Code parially comes from OmniauthCallbacksController class (from devise_token_auth).
module FacebookLoginHelper
def login_with_facebook
return unless fetch_fb_profile
setup_user_by_fb
create_token_info
set_token_on_user
if User.devise_modules.include?(:confirmable)