Skip to content

Instantly share code, notes, and snippets.

View felipecsl's full-sized avatar
⚒️
Building

Felipe Lima felipecsl

⚒️
Building
View GitHub Profile
@felipecsl
felipecsl / FakeResourceHttpURLConnection.kt
Last active July 11, 2020 16:51
Kotlin snippets for return a canned response from a resource file whenever a URL is request is fired
// Based on https://stackoverflow.com/questions/565535/mocking-a-url-in-java
package com.example
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL
class FakeResourceHttpURLConnection(
url: URL,
@felipecsl
felipecsl / NoOverscrollSwipeRefreshLayout.java
Last active January 7, 2020 05:54
Modified Android SwipeRefreshLayout that does not move down the content view on swipe down (no overscroll)
/*
* Copyright (C) 2013 The Android Open Source Project
*
* 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
@felipecsl
felipecsl / keymap.cson
Created June 12, 2017 06:23
Atom keymap
# Your keymap
#
# Atom keymaps work similarly to style sheets. Just as style sheets use
# selectors to apply styles to elements, Atom keymaps use selectors to associate
# keystrokes with events in specific contexts. Unlike style sheets however,
# each selector can only be declared once.
#
# You can create a new keybinding in this file by typing "key" and then hitting
# tab.
#
@felipecsl
felipecsl / GifDecoder.java
Last active November 1, 2019 02:53
Android custom ImageView that handles animated GIFs.
/**
* Copyright (c) 2013 Xcellent Creations, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
@felipecsl
felipecsl / ExceptionParser.java
Last active January 28, 2019 12:42
Helper class to parse error response body on Retrofit 2
public static class ExceptionParser {
private final ResponseBody body;
private final String bodyString;
private final Converter.Factory converterFactory;
public ExceptionParser(Response response, Converter.Factory converterFactory) {
this.converterFactory = converterFactory;
this.body = cloneResponseBody(response.errorBody());
this.bodyString = getBodyAsString(body);
}
@felipecsl
felipecsl / MutableLazy.kt
Created October 22, 2017 23:30
A Kotlin lazy that can be set to override the initializer value
private fun <T> mutableLazy(initializer: () -> T) = Delegate(lazy(initializer))
class Delegate<T>(private val lazy: Lazy<T>) {
private var value: T? = null
operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
return value ?: lazy.getValue(thisRef, property)
}
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
@felipecsl
felipecsl / phone-number-combinations.rs
Created April 15, 2018 01:02
My first Rust program - Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
// https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
use std::collections::HashMap;
use std::char;
use std::iter::FromIterator;
fn main() {
let mappings = init_map();
find_combinations("23", mappings)
}
def length_of_longest_substring(str)
if str.empty?
return 0
end
map = {}
curr_start = 0
max_length = 0
found_start = 0
found_end = 0
i = 0
@felipecsl
felipecsl / unless.kt
Last active December 24, 2017 13:12
Kotlin unless like Ruby just for fun
inline fun <T> unless(stmt: () -> Boolean, block: () -> T, noinline elseBlock: (() -> T)? = null) =
if (!stmt.invoke()) {
block()
} else {
elseBlock?.invoke()
}
// Use like:
unless({ foo == bar }, {
@felipecsl
felipecsl / incrementVersionCode
Created December 10, 2013 19:20
Increments the android versionCode automatically on each debug or release build
task('increaseVersionCode') << {
def manifestFile = file("src/main/AndroidManifest.xml")
def pattern = Pattern.compile("versionCode=\"(\\d+)\"")
def matcher = pattern.matcher((CharSequence)manifestFile.getText())
matcher.find()
def versionCode = Integer.parseInt(matcher.group(1))
manifestFile.write(matcher.replaceAll("versionCode=\"" + ++versionCode + "\""))
}
assembleDebug.dependsOn 'increaseVersionCode'