Skip to content

Instantly share code, notes, and snippets.

@joaocsousa
joaocsousa / Logger.kt
Last active December 10, 2021 11:19
Java based logger with auto tagging for Android apps. No need to use any log library.
//region initializing the logger using the application object or any other way
class CoreApplication : Application() {
private val initializer by inject<LogInitializer>()
override fun onCreate() {
super.onCreate()
initializer.initialize()
}
}
interface LogInitializer {
@joaocsousa
joaocsousa / contador_ano_novo.py
Created December 14, 2019 13:54
Codigo Python que faz contagem decrescente para o ano novo
import datetime
import time
ano = datetime.date.today().year
proxinoAno = ano + 1
while (True):
agora = datetime.datetime.now()
primeiroDiaProximoAno = datetime.datetime(proxinoAno, 1, 1, 0, 0, 0)
@joaocsousa
joaocsousa / Main.kt
Last active September 12, 2019 07:00
// how to use
SafeZip.zip(single1, single2, ::merge).subscribe(...)
@joaocsousa
joaocsousa / Main.java
Last active January 29, 2018 09:37
Shared observable that will run a task. Every subscriber that subscribes while the task is running will share the same observable and receive the same result. When the tasks finishes the next subscriber will trigger a new task.
public class Main {
public static final long START_TIME = System.currentTimeMillis();
public static void main(String[] args) throws InterruptedException {
SingletonTask singletonTask = singletonTask();
singletonTask.getObservable()
.subscribeOn(Schedulers.newThread())
@joaocsousa
joaocsousa / MainActivity.java
Created October 13, 2017 06:00
LayoutInflater.Factory sample with text translations
package joaocsousa.github.com.myapplication;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.LayoutInflater;
@joaocsousa
joaocsousa / CacheObservable.java
Created September 28, 2017 21:16
CacheObservable that allows you to cache an emission for a certain period of time
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.Observer;
import io.reactivex.functions.Consumer;
public static class CacheObservable<T> implements ObservableTransformer<T, T> {
private final long timeout;
@joaocsousa
joaocsousa / SortCodeView.java
Created August 17, 2016 09:40
Android EditText that automatically formats a UK sort code as the user types it in.
import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.util.AttributeSet;
@joaocsousa
joaocsousa / ImageUtils.java
Last active August 29, 2015 14:07
Android snippet to load images and downscale if needed
import static android.graphics.BitmapFactory.decodeResource;
import static com.google.common.base.Optional.fromNullable;
public static final Double MAX_MEMORY_ALLOCATION = 6E6;
/**
* This method loads a bitmap from the resources respecting the max memory an image should take in memory
* @param imageResource
* @param context
* @return
@joaocsousa
joaocsousa / ArgbEvaluator.java
Last active October 29, 2017 21:11
ArgbEvaluator compatible with JELLY_BEAN (API Level 16, Android 4.1) and lower
/*
* Copyright (C) 2010 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