Skip to content

Instantly share code, notes, and snippets.

View Artur-Sulej's full-sized avatar
🟣
Meet me at Code BEAM America

Artur Sulej Artur-Sulej

🟣
Meet me at Code BEAM America
View GitHub Profile
@Artur-Sulej
Artur-Sulej / benchmark.sql
Created August 28, 2018 10:21
Average total time and total cost of a given SQL query run 10k times.
CREATE OR REPLACE FUNCTION benchmark(
IN query text,
OUT total_cost double precision,
OUT runtime double precision
)
RETURNS record
LANGUAGE plpgsql
STRICT AS
$$DECLARE
j json;
@Artur-Sulej
Artur-Sulej / facebook_login_helper.rb
Created May 11, 2016 09:38
PushTokensHelper module - utility for storing data for sending mobile pushes and for sending push notifications. Sending them should be performed on a separate thread.
require 'net/http'
module PushTokensHelper
def send_pushes(user, data)
PushToken.where(user: user).to_a.each do |push_token|
send_push_to_device(push_token, data)
end
end
@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)
@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 / 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 / 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 / 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 / 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 / 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 / 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();
}